To the moon HDU - 4348 (主席树,区间修改)
To The Moon is a independent game released in November 2011, it is a role-playing adventure game powered by RPG Maker.
The premise of To The Moon is based around a technology that allows us to permanently reconstruct the memory on dying man. In this problem, we'll give you a chance, to implement the logic behind the scene.
You‘ve been given N integers A [1], A [2],..., A [N]. On these integers, you need to implement the following operations:
1. C l r d: Adding a constant d for every {A i | l <= i <= r}, and increase the time stamp by 1, this is the only operation that will cause the time stamp increase.
2. Q l r: Querying the current sum of {A i | l <= i <= r}.
3. H l r t: Querying a history sum of {A i | l <= i <= r} in time t.
4. B t: Back to time t. And once you decide return to a past, you can never be access to a forward edition anymore.
.. N, M ≤ 10 5, |A [i]| ≤ 10 9, 1 ≤ l ≤ r ≤ N, |d| ≤ 10 4 .. the system start from time 0, and the first modification is in time 1, t ≥ 0, and won't introduce you to a future state.
Input
n m
A 1 A 2 ... A n
... (here following the m operations. )
Output
... (for each query, simply print the result. )
Sample Input
10 5
1 2 3 4 5 6 7 8 9 10
Q 4 4
Q 1 10
Q 2 4
C 3 6 3
Q 2 4 2 4
0 0
C 1 1 1
C 2 2 -1
Q 1 2
H 1 2 1
Sample Output
4
55
9
15 0
1 给出 n 个数,m次操作,操作一共分成 4 种
1、[l, r]内的值增加 d, 并且 time + 1
2、查询当前 time 时 [l, r]内的和
3、查询time = t 时 [l, r]内的和
4、把 time 退回到 t
然后对于2、3操作,输出对应的答案。
因为有4操作,所以要用主席树来保留每一个 time 的线段树。
对于区间修改操作,像线段树一样去更新,对于区间的sum,直接更新当前区间内的sum值,但是 update 的时候并不能保证 update 到的区间都被给出的区间所包含,所以需要用(min(r, pr) - max(l, pl) + 1)来确定有几个要更新的点。对于lazy,只有当 update 到的区间完全被给出的区间所包含的时候,才更新 lazy, 这样保证了 sum 不会多加, lazy 不会多标记。
对于区间查询操作,由于在之前更新的时候就已经把当前区间的 sum 求出来了,所以当某个区间完全被查询区间包含,只要直接加上这个区间的 sum 值就可以了。对于lazy,有 lazy 标记的区间,区间内的每个点都要加上 lazy 的值,所以如果我 query 到的区间有 lazy 标记,那么说明我这个区间内的每个点都应该多加 lazy ,给这个区间内需要加 lazy 的值的个数也是 (min(r, pr) - max(l, pl) + 1)
/*
.
';;;;;.
'!;;;;;;!;`
'!;|&#@|;;;;!:
`;;!&####@|;;;;!:
.;;;!&@$$%|!;;;;;;!'.`:::::'.
'!;;;;;;;;!$@###&|;;|%!;!$|;;;;|&&;.
:!;;;;!$@&%|;;;;;;;;;|!::!!:::;!$%;!$%` '!%&#########@$!:.
;!;;!!;;;;;|$$&@##$;;;::'''''::;;;;|&|%@$|;;;;;;;;;;;;;;;;!$;
;|;;;;;;;;;;;;;;;;;;!%@#####&!:::;!;;;;;;;;;;!&####@%!;;;;$%`
`!!;;;;;;;;;;!|%%|!!;::;;|@##%|$|;;;;;;;;;;;;!|%$#####%;;;%&;
:@###&!:;;!!||%%%%%|!;;;;;||;;;;||!$&&@@%;;;;;;;|$$##$;;;%@|
;|::;;;;;;;;;;;;|&&$|;;!$@&$!;;;;!;;;;;;;;;;;;;;;;!%|;;;%@%.
`!!;;;;;;;!!!!;;;;;$@@@&&&&&@$!;!%|;;;;!||!;;;;;!|%%%!;;%@|.
%&&$!;;;;;!;;;;;;;;;;;|$&&&&&&&&&@@%!%%;!||!;;;;;;;;;;;;;$##!
!%;;;;;;!%!:;;;;;;;;;;!$&&&&&&&&&&@##&%|||;;;!!||!;;;;;;;$&:
':|@###%;:;;;;;;;;;;;;!%$&&&&&&@@$!;;;;;;;!!!;;;;;%&!;;|&%.
!@|;;;;;;;;;;;;;;;;;;|%|$&&$%&&|;;;;;;;;;;;;!;;;;;!&@@&'
.:%#&!;;;;;;;;;;;;;;!%|$$%%&@%;;;;;;;;;;;;;;;;;;;!&@:
.%$;;;;;;;;;;;;;;;;;;|$$$$@&|;;;;;;;;;;;;;;;;;;;;%@%.
!&!;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;|@#;
`%$!;;;;;;;;;;;$@|;;;;;;;;;;;;;;;;;;;;;;;;!%$@#@|.
.|@%!;;;;;;;;;!$&%||;;;;;;;;;;;;;;;;;!%$$$$$@#|.
;&$!;;;;;;;;;;;;;;;;;;;;;;;;;;;;;;%#####|.
|##$|!;;;;;;::'':;;;;;;;;;;;;;!%$$$@#@;
;@&|;;;;;;;::'''''':;;;;;;;|$&@###@|`
.%##@|;;;;:::''''''''''::;!%&##$'
`$##@$$@@&|!!;;;:'''''::::;;;;;|&#%.
;&@##&$%!;;;;;;::''''''''::;!|%$@#@&@@:
.%@&$$|;;;;;;;;;;:'''':''''::;;;%@#@@#%.
:@##@###@$$$$$|;;:'''':;;!!;;;;;;!$#@@#$;`
`%@$$|;;;;;;;;:'''''''::;;;;|%$$|!!&###&'
|##&%!;;;;;::''''''''''''::;;;;;;;!$@&:`!'
:;!@$|;;;;;;;::''''''''''':;;;;;;;;!%&@$: !@#$'
|##@@&%;;;;;::''''''''':;;;;;;;!%&@#@$%: '%%!%&;
|&%!;;;;;;;%$!:''''''':|%!;;;;;;;;|&@%||` '%$|!%&;
|@%!;;!!;;;||;:'''''':;%$!;;;;!%%%&#&%$&: .|%;:!&%`
!@&%;;;;;;;||;;;:''::;;%$!;;;;;;;|&@%;!$; `%&%!!$&:
'$$|;!!!!;;||;;;;;;;;;;%%;;;;;;;|@@|!$##; !$!;:!$&:
|#&|;;;;;;!||;;;;;;;;!%|;;;;!$##$;;;;|%' `%$|%%;|&$'
|&%!;;;;;;|%;;;;;;;;$$;;;;;;|&&|!|%&&; .:%&$!;;;:!$@!
`%#&%!!;;;;||;;;;;!$&|;;;!%%%@&!;;;!!;;;|%!;;%@$!%@!
!&!;;;;;;;;;||;;%&!;;;;;;;;;%@&!;;!&$;;;|&%;;;%@%`
'%|;;;;;;;;!!|$|%&%;;;;;;;;;;|&#&|!!||!!|%$@@|'
.!%%&%'`|$; :|$#%|@#&;%#%.
*/
#include <map>
#include <set>
#include <list>
#include <ctime>
#include <cmath>
#include <stack>
#include <queue>
#include <string>
#include <vector>
#include <cstdio>
#include <bitset>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <algorithm>
#define lowbit(x) x & (-x)
#define mes(a, b) memset(a, b, sizeof a)
#define fi first
#define se second
#define pii pair<int, int>
#define INOPEN freopen("in.txt", "r", stdin)
#define OUTOPEN freopen("out.txt", "w", stdout) typedef unsigned long long int ull;
typedef long long int ll;
const int maxn = 1e5 + ;
const int maxm = 1e5 + ;
const ll mod = 1e9 + ;
const ll INF = 1e18 + ;
const int inf = 0x3f3f3f3f;
const double pi = acos(-1.0);
const double eps = 1e-;
using namespace std; int n, m;
int cas, tol, T; struct Node{
int l, r;
ll sum, lazy;
} node[maxn * ];
ll a[maxn];
int rt[maxn]; void init() {
tol = ;
mes(a, );
mes(rt, );
} void pushup(int rt) {
node[rt].sum = node[node[rt].l].sum + node[node[rt].r].sum;
} void build(int l, int r, int &rt) {
rt = ++tol;
node[rt].sum = node[rt].lazy = ;
if(l == r) {
node[rt].sum = a[l];
return ;
}
int mid = (l + r) >> ;
build(l, mid, node[rt].l);
build(mid+, r, node[rt].r);
pushup(rt);
} void update(int l, int r, int &x, int y, int pl, int pr, ll val) {
x = ++tol;
node[x] = node[y];
node[x].sum += 1ll * (min(r, pr) - max(l, pl) + ) * val;
if(pl <= l && r <= pr) {
node[x].lazy += val;
return ;
}
int mid = (l + r) >> ;
if(pl <= mid)
update(l, mid, node[x].l, node[y].l, pl, pr, val);
if(pr > mid)
update(mid+, r, node[x].r, node[y].r, pl, pr, val);
} ll query(int l, int r, int pl, int pr, int rt) {
if(pl <= l && r <= pr) {
return node[rt].sum;
}
int mid = (l + r) >> ;
ll ans = 1ll * (min(r, pr) - max(l, pl) + ) * node[rt].lazy;
if(pl <= mid)
ans += query(l, mid, pl, pr, node[rt].l);
if(pr > mid)
ans += query(mid+, r, pl, pr, node[rt].r);
return ans;
} int main() {
while(~scanf("%d%d", &n, &m)) {
init();
for(int i=; i<=n; i++) {
scanf("%lld", &a[i]);
}
T = ;
build(, n, rt[T]);
char s[];
while(m--) {
scanf("%s", s+);
if(s[] == 'C') {
int l, r;
ll d;
scanf("%d%d%lld", &l, &r, &d);
T++;
update(, n, rt[T], rt[T-], l, r, d);
} else if(s[] == 'Q') {
int l, r;
scanf("%d%d", &l, &r);
ll ans = query(, n, l, r, rt[T]);
printf("%lld\n", ans);
} else if(s[] == 'H') {
int l, r, t;
scanf("%d%d%d", &l, &r, &t);
ll ans = query(, n, l, r, rt[t]);
printf("%lld\n", ans);
} else if(s[] == 'B'){
int t;
scanf("%d", &t);
T = t;
}
}
}
return ;
}
To the moon HDU - 4348 (主席树,区间修改)的更多相关文章
- HDU 4348 主席树区间更新
To the moon Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total ...
- HDU 4348 To the moon(主席树区间修改)
题意 给你一个区间,支持如下操作: 在一段区间内加上一个值,并生成一个历史版本 查询某个版本下一段区间内的和 回到一个历史版本上并舍弃之后的版本 做法 这就是主席树区间修改裸题啦QwQ 上一篇博客我讲 ...
- hdu 5919 主席树(区间不同数的个数 + 区间第k大)
Sequence II Time Limit: 9000/4500 MS (Java/Others) Memory Limit: 131072/131072 K (Java/Others)Tot ...
- HDU 6278 主席树(区间第k大)+二分
Just h-index Time Limit: 6000/3000 MS (Java/Others) Memory Limit: 132768/132768 K (Java/Others)To ...
- HDU - 1698 线段树区间修改,区间查询
这就是很简单的基本的线段树的基本操作,区间修改,区间查询,对区间内部信息打上laze标记,然后维护即可. 我自己做的时候太傻逼了...把区间修改写错了,对给定区间进行修改的时候,mid取的是节点的左右 ...
- E - Just a Hook HDU - 1698 线段树区间修改区间和模版题
题意 给出一段初始化全为1的区间 后面可以一段一段更改成 1 或 2 或3 问最后整段区间的和是多少 思路:标准线段树区间和模版题 #include<cstdio> #include& ...
- hdu4348 - To the moon 可持久化线段树 区间修改 离线处理
法一:暴力! 让干什么就干什么,那么久需要可持久化线段树了. 但是空间好紧.怎么破? 不down标记好了! 每个点维护sum和add两个信息,sum是这段真实的和,add是这段整体加了多少,如果这段区 ...
- Hdu 1698(线段树 区间修改 区间查询)
In the game of DotA, Pudge's meat hook is actually the most horrible thing for most of the heroes. T ...
- HDU 3397 线段树区间修改
Sequence operation Time Limit: 10000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Othe ...
- hdu 1698 线段树 区间修改
#include <cstdio> #include <cstdlib> #include <cmath> #include <map> #includ ...
随机推荐
- 华为防火墙USG6000V使用总结
问题1.ge 1/0/0 的ip地址 20.0.0.2 ,从直连的对端20.0.0.1 无法ping. 但是从防火墙ping对端却是可以ping通? 原因: 华为新一代的防火墙,默认情况下,只有0口是 ...
- Android为TV端助力 反编译
http://blog.csdn.net/vipzjyno1/article/details/21039349/ apktool.bat d -f test.apk test 这条命令修改 ...
- django入门与实践 - 关于升级到django 3.7,三种模板超链接配置(编辑中)
第一种方法: 在myblog/urls.py模块中: from django.contrib import admin from django.urls import path, include ur ...
- unity can't add the script behaviour CallbackExecutor. The script needs ...
记一次Unity3D小问题 当我打开VS2017编辑完C#脚本,要将脚本拖到一个3D组件上时,发生了以下错误 unity can't add the script behaviour Callback ...
- Redis压缩列表
此篇文章是主要介绍Redis在数据存储方面的其中一种方式,压缩列表.本文会介绍1. 压缩列表(ziplist)的使用场景 2.如何达到节约内存的效果?3.压缩列表的存储格式 4. 连锁更新的问题 5 ...
- MAC MAMP 中安装配置使用 ThinkPHP
MAMP PRO 是Mac OS X 平台上经典的本地环境应用 MAMP 的专业版.专门为专业的Web开发人员和程序员轻松地安装和管理自己的开发环境. MAMP这几个首字母代表Mac OS X系统上的 ...
- Kubernetes 集群日志管理 - 每天5分钟玩转 Docker 容器技术(180)
Kubernetes 开发了一个 Elasticsearch 附加组件来实现集群的日志管理.这是一个 Elasticsearch.Fluentd 和 Kibana 的组合.Elasticsearch ...
- JS中简单的二级城市联动
代码奉上: <!DOCTYPE html><html><head> <meta charset="UTF-8"> < ...
- chrome打开收藏夹的网站在新的标签页
chrome浏览器在新的标签页打开收藏夹的网址,现在设置不了,而且右键,在新标签页中打开有点烦..下面说说直接打开的方式. 方法1: 鼠标滚轮,直接点击收藏夹的网址,即可 方法2: ctrl + 鼠标 ...
- Vim 宏
宏的概念 什么是宏呢?英文名:macro,代表一串命令的集合. 示例操作文本 SELECT * FROM `edu_ocr_task` WHERE ((`userId`=284871) AND (`u ...