SPOJ:To the moon
题面
Sol
主席树模板
# include <bits/stdc++.h>
# define RG register
# define IL inline
# define Fill(a, b) memset(a, b, sizeof(a))
using namespace std;
typedef long long ll;
const int _(1e5 + 5);
const int __(5e6);
IL int Input(){
RG int x = 0, z = 1; RG char c = getchar();
for(; c < '0' || c > '9'; c = getchar()) z = c == '-' ? -1 : 1;
for(; c >= '0' && c <= '9'; c = getchar()) x = (x << 1) + (x << 3) + (c ^ 48);
return x * z;
}
int rt[_], tot, ls[__], rs[__];
ll add[__], sum[__];
int n, m;
IL void Build(RG int &x, RG int l, RG int r){
x = ++tot;
if(l == r){
sum[x] = Input();
return;
}
RG int mid = (l + r) >> 1;
Build(ls[x], l, mid), Build(rs[x], mid + 1, r);
sum[x] = sum[ls[x]] + sum[rs[x]];
}
IL void Modify(RG int &x, RG int l, RG int r, RG int L, RG int R, RG ll ad){
ls[++tot] = ls[x], rs[tot] = rs[x], sum[tot] = sum[x], add[tot] = add[x];
x = tot;
RG int len = min(r, R) - max(l, L) + 1;
sum[x] += 1LL * ad * len;
if(L <= l && R >= r){
add[x] += ad;
return;
}
RG int mid = (l + r) >> 1;
if(L <= mid) Modify(ls[x], l, mid, L, R, ad);
if(R > mid) Modify(rs[x], mid + 1, r, L, R, ad);
}
IL ll Query(RG int x, RG int l, RG int r, RG int L, RG int R, RG ll ad){
if(L <= l && R >= r) return sum[x] + 1LL * ad * (r - l + 1);
ad += add[x];
RG int mid = (l + r) >> 1; RG ll ret = 0;
if(L <= mid) ret = Query(ls[x], l, mid, L, R, ad);
if(R > mid) ret += Query(rs[x], mid + 1, r, L, R, ad);
return ret;
}
int main(RG int argc, RG char* argv[]){
n = Input(), m = Input();
Build(rt[0], 1, n);
for(RG int i = 1, now = 0, x, y, z; i <= m; ++i){
RG char op; scanf(" %c", &op);
if(op == 'C'){
++now, rt[now] = rt[now - 1];
x = Input(), y = Input(), z = Input();
Modify(rt[now], 1, n, x, y, z);
}
else if(op == 'Q'){
x = Input(), y = Input();
printf("%lld\n", Query(rt[now], 1, n, x, y, 0));
}
else if(op == 'H'){
x = Input(), y = Input(), z = Input();
printf("%lld\n", Query(rt[z], 1, n, x, y, 0));
}
else now = Input();
}
return 0;
}
SPOJ:To the moon的更多相关文章
- 2018.08.04 spoj TTM to the moon(主席树)
spoj传送门 vjudge传送门 主席树板子题. 支持历史版本的区间和,区间和,区间修改和时光倒流. 其中新奇一点的也只有区间修改了,这个东西直接标记永久化就行了. 如果想下传标记的话也行,需要在p ...
- HDU 4348.To the moon SPOJ - TTM To the moon -可持久化线段树(带修改在线区间更新(增减)、区间求和、查询历史版本、回退到历史版本、延时标记不下放(空间优化))
To the moon Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- SPOJ:Free tour II (树分治+启发式合并)
After the success of 2nd anniversary (take a look at problem FTOUR for more details), this 3rd year, ...
- SPOJ:NPC2016A(数学)
http://www.spoj.com/problems/NPC2016A/en/ 题意:在一个n*n的平面里面,初始在(x,y)需要碰到每条边一次,然后返回(x,y),问最短路径是多长. 思路:像样 ...
- HDU 4348 SPOJ 11470 To the moon
Vjudge题面 Time limit 2000 ms Memory limit 65536 kB OS Windows Source 2012 Multi-University Training C ...
- HDU4348:To the moon
浅谈主席树:https://www.cnblogs.com/AKMer/p/9956734.html 浅谈标记永久化:https://www.cnblogs.com/AKMer/p/10137227. ...
- SPOJ:D-query(非常规主席树求区间不同数的个数)
Given a sequence of n numbers a1, a2, ..., an and a number of d-queries. A d-query is a pair (i, j) ...
- SPOJ:House Fence(分治&DP)
"Holiday is coming, holiday is coming, hurray hurray!" shouts Joke in the last day of his ...
- SPOJ:Lexicographically Smallest(并查集&排序)
Taplu and Abhishar loved playing scrabble. One day they thought of inventing a new game using alphab ...
随机推荐
- python[error] - mysql_config not found
具体报错信息: root@pts/4 $ pip install MySQL-python Collecting MySQL-python Using cached MySQL-python-1.2. ...
- TensorFlow实战之实现AlexNet经典卷积神经网络
本文根据最近学习TensorFlow书籍网络文章的情况,特将一些学习心得做了总结,详情如下.如有不当之处,请各位大拿多多指点,在此谢过. 一.AlexNet模型及其基本原理阐述 1.关于AlexNet ...
- 【Unity3D技术文档翻译】第1.8篇 AssetBundles 问题及解决方法
上一章:[Unity3D技术文档翻译]第1.7篇 AssetBundles 补丁更新 本章原文所在章节:[Unity Manual]→[Working in Unity]→[Advanced Deve ...
- core java
ConsoleTest,这个程序如果在IDE里运行就会因无法获得控制台而报错 import java.io.Console; public class ConsoleTest { public sta ...
- dubbox系列【四】——使用dubbo-monitor-x监控注册中心
1.下载源码,编译成war包 源码下载地址为:https://git.oschina.net/yjmyzz/dubbo-monitor.git 在pom.xml文件所在目录,直接执行mvn packa ...
- 洛谷P3375 - 【模板】KMP字符串匹配
原题链接 Description 模板题啦~ Code //[模板]KMP字符串匹配 #include <cstdio> #include <cstring> int cons ...
- 【spring-boot】spring aop 面向切面编程初接触
众所周知,spring最核心的两个功能是aop和ioc,即面向切面,控制反转.这里我们探讨一下如何使用spring aop. 1.何为aop aop全称Aspect Oriented Programm ...
- svn版本管理系统出现的问题解决办法
首先会出现下面的一个提示错误: cannot checkout from svn run 'cleanup' if it was interrupted 百度加谷歌找到的解决问题的办法各种各样,自己遇 ...
- ThinkPad W500 清灰记录
转载请注明出处:HateMath归来(http://www.cnblogs.com/hatemath/) 看型号就知道,这是一台英雄迟暮型的老电脑.到了夏天,启动后啥事不做,通风口都烫手.心情好,一时 ...
- linux 版本控制及rpm打包
版本控制 subversion:是一个自由/开源的版本控制系统,在subversion管理下,文件和目录可以超越时空subversion允许你数据恢复到早期版本,或者是检查数据修改历史许多人将版本控制 ...