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 ...
随机推荐
- Linux知识体系之路径属性与目录
最近在看鸟哥的Linux私房菜,我觉得这本书还是很不错的.这里进行相关的总结. 1.Linux目录权限概念 Linux一般讲目录可存取的方式分为三个类别,分别是owner/group/other, ...
- apache 改变文档根目录www的位置
1.找到apache的安装目录,找到config/httpd.conf,找到DocumentRoot "D:/wamp/www/" 改成你想要的目录,例如:改成 DocumentR ...
- php生成文字水印和图片水印
生成文字水印 //文字水印 /*打开图片*/ //1.配置图片路径 $src = "4.jpg"; //2.获取图片的信息(得到图片的基本信息) $info = getimag ...
- Nginx的try_files指令和命名location使用实例
Nginx的配置语法灵活,可控制度非常高.在0.7以后的版本中加入了一个try_files指令,配合命名location,可以部分替代原本常用的rewrite配置方式,提高解析效率. 下面是一个使用实 ...
- angularjs 指令详解
一.指令定义 对于指令,可以把它简单的理解成在特定DOM元素上运行的函数,指令可以扩展这个元素的功能. 首先来看个完整的参数示例再来详细的介绍各个参数的作用及用法: <div my-direct ...
- git添加本地仓库与远程仓库连接
在本地建立一个文件夹,需要与远程git仓库进行连接,具体方法: <1>首先进入所在文件目录执行: git init 初始化git,紧接着 git add . git commit -m ...
- web1 - HTML&CSS
Brackets 编辑器的安装和使用 Emmet:HTML/CSS代码快速编写 HTML && CSS
- 论中国为什么造不出cpu和操作系统
为什么呢?不是中国人不聪明. 而是中国缺乏科学研究的氛围 中国错过了计算机理论研究的黄金时期 中国人对计算机的了解是一支半解 中国缺乏对应的产业基础 中国缺乏计算机基础研究的氛围 计算机所运用的各种科 ...
- MongoDB,分组,聚合
使用聚合,db.集合名.aggregate- 而不是find 管道在Unix和Linux中一般用于将当前命令的输出结果作为下一个命令的参数.MongoDB的聚合管道将MongoDB文档在一个管道处理完 ...
- Five nines
Five nines, commonly taken to mean "99.999%", may refer to: 高可用 High availability of serv ...