hdu-4348-To the moon-主席树在线区间更新
来源:互联网 发布:免费书籍阅读软件 编辑:程序博客网 时间:2024/06/05 19:52
To the moon
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 2937 Accepted Submission(s): 576
Problem Description
Background
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 {Ai | 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 {Ai | l <= i <= r}.
3. H l r t: Querying a history sum of {Ai | 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 ≤ 105, |A[i]| ≤ 109, 1 ≤ l ≤ r ≤ N, |d| ≤ 104 .. 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.
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 {Ai | 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 {Ai | l <= i <= r}.
3. H l r t: Querying a history sum of {Ai | 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 ≤ 105, |A[i]| ≤ 109, 1 ≤ l ≤ r ≤ N, |d| ≤ 104 .. 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
A1 A2 ... An
... (here following the m operations. )
A1 A2 ... An
... (here following the m operations. )
Output
... (for each query, simply print the result. )
Sample Input
10 51 2 3 4 5 6 7 8 9 10Q 4 4Q 1 10Q 2 4C 3 6 3Q 2 42 40 0C 1 1 1C 2 2 -1Q 1 2H 1 2 1
Sample Output
45591501
Author
HIT
Source
2012 Multi-University Training Contest 5
题目大意:给你一个长度为n的数组,总共四种操作,其中还有一个时间T,每次更新区间的时候时间T+1,更新操作就是把lr区间全部加上d查询操作是一个查询当前时间lr的和,另一个是在第t秒的时候,查询lr区间的和,最后一种操作是把时间退回到第t秒,并且不能再询问t秒以后的时间。刚开始的时候T等于0.
思路:一开始的时候初始化一颗线段树,就普通线段树一样,现在主要的问题是怎么进行区间更新,因为主席树的思想就是要利用前面的树,并且不能修改,只能引用,所以按普通的区间更新的话,每次更新就相当于要建一颗r-l+1长的线段树,空间肯定不够,考虑普通的线段树的down操作是把懒惰标记往下传,并更新子树,那么可不可以不更新子树了,当然是可以的,直接把当前区间的懒惰标记用一个参数传下去,然后找到要求和的区间时,直接把从上到下的懒惰标记累加和乘以区间长度再加上这段区间原本的和就可以了。然后这时候主席树里面每颗线段树的sum记录的就是这段区间跟新过得和,因为在新建的一颗主席树上更新了l-r区间,所以所有有覆盖l-r的区间的sum都是可以跟新的因为没有用到前面一颗树的sum,是另开的空间,所以保证了查询的时候只查到这一段就可以直接返回sum了。至于add当然就是等于前面一颗树的add加上C。另外当时间回到t的时候可以回收空间,就是t到T的这一段内存已经用不到了,这时可以直接把tot = T[t+1],不写这句话内存用了5w+,写了只用了1w+。
#include <cstdio>#include <cstring>#include <cmath>#include <algorithm>#include <iostream>#include <cstdlib>#include <queue>typedef __int64 LL;using namespace std;const int maxn = 100005;int n,m;int T[maxn];int lson[maxn*30],rson[maxn*30];LL sum[maxn*30],add[maxn*30];int tot = 0;int build(int l,int r){ int root = tot++; add[root] = 0; if(l < r) { int mid = (l + r) >> 1; lson[root] = build(l,mid); rson[root] = build(mid + 1,r); } else { scanf("%I64d",&sum[root]); return root; } sum[root] = sum[lson[root]] + sum[rson[root]]; return root;}int A,B;LL C;void up(int o,int len){ sum[o] = sum[lson[o]] + sum[rson[o]] + add[lson[o]] * (len - len/2) + add[rson[o]] * (len/2);}int update(int root,int l,int r){ int newroot = tot++; add[newroot] = add[root]; if(A <= l && r <= B) { sum[newroot] = sum[root]; add[newroot] = add[root] + C; lson[newroot] = lson[root]; rson[newroot] = rson[root]; return newroot; } int mid = (l + r) >> 1; if(A <= mid) lson[newroot] = update(lson[root],l,mid); else lson[newroot] = lson[root]; if(B > mid) rson[newroot] = update(rson[root],mid+1,r); else rson[newroot] = rson[root]; up(newroot,r - l + 1); return newroot;}LL qurry(int root,int l,int r,LL add1){ if(A <= l && r <= B) { return sum[root] + (add1 + add[root]) * (r - l + 1); } LL ans = 0; int mid = (l + r) >>1; if(A <= mid) ans += qurry(lson[root],l,mid,add[root] + add1); if(B > mid) ans += qurry(rson[root],mid + 1,r,add[root] + add1); return ans;}int main(){ int ca = 0; while(scanf("%d %d",&n,&m) != EOF) { if(ca) printf("%d\n"); ca++; int i; tot = 0; int t = 0; T[0] = build(1,n); char op[10]; while(m--) { scanf("%s",op); if(op[0] == 'Q') { scanf("%d %d",&A,&B); printf("%I64d\n",qurry(T[t],1,n,0)); } else if(op[0] == 'C') { scanf("%d %d %I64d",&A,&B,&C); T[t+1] = update(T[t],1,n); t++; } else if(op[0] == 'H') { scanf("%d %d %I64d",&A,&B,&C); printf("%I64d\n",qurry(T[C],1,n,0)); } else { scanf("%d",&A); t = A; tot = T[t+1]; } } } return 0;}
0 0
- hdu-4348-To the moon-主席树在线区间更新
- HDU 4348 To the moon(主席树区间更新)
- HDU 4348To the moon 主席树 + 区间更新
- HDU 4348 To the moon 【主席树+区间修改】
- HDU 4348 To the moon [主席树 区间修改]
- hdu-4348-To the moon-离线处理 or 主席树区间更新
- hdu 4348 To the moon(主席树,区间更新节省内存,经典)
- hdu-4348-To the moon(主席树更新,区间更新,区间和查询,保存历史版本返回)
- hdu 4348 To the moon (主席树)
- HDU-4348 To the moon (主席树)
- HDU 4348 To the moon 主席树成段更新
- HDU 4348 / SPOJ TTM To the moon [主席树]
- HDU-4348 To the moon(主席树)
- SPOJ TTM To The Moon 主席树的区间更新与查询
- SPOJ TTM To the moon(主席树+区间操作)
- vjudge: spoj--to the moon(主席树区间修改)
- 【HDU4348】To The Moon-主席树(可持久化线段树)区间修改+区间询问
- hdu4348 To the moon(区间修改,区间查询的主席树)
- 原码、反码、补码
- java学习笔记:利用TreeSet排序时Comparator接口和Comparable接口
- muduo::EventLoop分析
- 关于AR学院
- Swift入门(七)——结构体(Struct)
- hdu-4348-To the moon-主席树在线区间更新
- 应用限流接入手册
- 微信HTML5页面设计建议
- 问题:浮动影响<a>标签点击?
- 如此抄袭Apps之OscHub(二)
- 没有onFling,找不到onFling()
- 外网访问自己的tomcat
- Java心得5
- dxqdzhiag