hdu 4348 To the moon(主席树,区间更新节省内存,经典)
来源:互联网 发布:赌球软件哪个好 编辑:程序博客网 时间:2024/05/22 20:40
题目链接
To the moon
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 5038 Accepted Submission(s): 1134
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
参考题解
这一题和这个题是一样的,唯一区别就是内存限制变小了,当然还有输出时多组数据之间不用空行。
于是,学到了一种函数式线段树成段更新时节约内存的办法 。。。
先考虑朴素的仅支持成段加减的线段树,我们可以用方式解决:
1.正常的懒惰标记,当访问到带有懒惰标记节点的子区间时将标记下传;
2.不用下传的懒惰标记,我们用一个标记来记录当前节点的整段区间被累加了多少,当询问的时候我们在从根节点走到目标结点的过程中不断累加所经过节点上的标记值。。。
基于这两种思想,就有了函数式线段树的两种实现方式,第一种在pushdown的过程中会产生大量的结点,而第二种没有pushdown的过程,不会新建过多的节点
#include<iostream>#include<cstdio>#include<algorithm>#include<cstring>#include<vector>#include<queue>#include<stack>using namespace std;#define rep(i,a,n) for (int i=a;i<n;i++)#define per(i,a,n) for (int i=n-1;i>=a;i--)#define pb push_back#define fi first#define se secondtypedef vector<int> VI;typedef long long ll;typedef pair<int,int> PII;const int maxn=1e5+10;struct node{ int l,r; ll sum,add;}T[maxn*30];int root[maxn];int cnt;void pushup(int x){ T[x].sum=T[T[x].l].sum+T[T[x].r].sum;}int build(int l,int r){ int root=++cnt; T[root].add=0; if(l==r) { scanf("%lld",&T[root].sum); T[root].l=T[root].r=0; return root; } int m=(l+r)/2; T[root].l=build(l,m),T[root].r=build(m+1,r); pushup(root); return root;}void copy(int x,int y){ T[x].l=T[y].l; T[x].r=T[y].r; T[x].sum=T[y].sum; T[x].add=T[y].add;}int update(int root,int L,int R,int l,int r,int v){ int now=++cnt; copy(now,root); T[now].sum+=1ll*v*(r-l+1); if(L==l&&R==r) { T[now].add+=v; return now; } int m=(L+R)/2; if(r<=m) T[now].l=update(T[root].l,L,m,l,r,v); else if(l>m) T[now].r=update(T[root].r,m+1,R,l,r,v); else { T[now].l=update(T[root].l,L,m,l,m,v); T[now].r=update(T[root].r,m+1,R,m+1,r,v); } return now;}ll query(int root,int L,int R,int l,int r){ ll ans=0; if(L==l&&r==R) return T[root].sum; int m=(L+R)/2; ans+=(r-l+1)*T[root].add; if(r<=m) return ans+query(T[root].l,L,m,l,r); else if(l>m) return ans+query(T[root].r,m+1,R,l,r); else return ans+query(T[root].l,L,m,l,m)+query(T[root].r,m+1,R,m+1,r); return ans;}int main(){ int n,m; while(~scanf("%d%d",&n,&m)) { int now=0; cnt=0; root[now]=build(1,n); char op[5]; while(m--) { scanf("%s",op); int l,r,v; if(op[0]=='Q') { scanf("%d%d",&l,&r); printf("%lld\n",query(root[now],1,n,l,r)); } else if(op[0]=='C') { scanf("%d%d%d",&l,&r,&v); now++; root[now]=update(root[now-1],1,n,l,r,v); } else if(op[0]=='H') { scanf("%d%d%d",&l,&r,&v); printf("%lld\n",query(root[v],1,n,l,r)); } else if(op[0]=='B') scanf("%d",&v),now=v; } //puts(""); } return 0;}
0 0
- hdu 4348 To the moon(主席树,区间更新节省内存,经典)
- 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 / 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(区间修改,区间查询的主席树)
- 取自官方的实例pyautocadline
- springMvc传递参数
- PAT-B 1048. 数字加密(20)
- SHGetSpecialFolderPath函数
- JS数据类型
- hdu 4348 To the moon(主席树,区间更新节省内存,经典)
- c++ queue 使用方法(队列,大小根堆)
- Java编程风格学习(二)
- socket的初始化与IP地址的转换
- 多个线程之间共享数据的方式
- linux configure配置文件参数详解
- 《win10下搭建Apache+Mysql+PHP环境》
- MySQL用户管理
- 快