HDU
来源:互联网 发布:工艺软件有什么 编辑:程序博客网 时间:2024/06/01 09:50
To the moon
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)Total Submission(s): 6186 Accepted Submission(s): 1421
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的区间进行 4种操作 C 更新 [l,r]每个 数加d Q当前询问 区间 H 询问第t次更新的区间 B返回第t次更新时候的状态。
分析:直接上主席树
(自己A了之后看了一下别人的代码,空间比我少5倍~~当执行了B返回t状态时可以让cnt=root[t+1],因为后面的那些空间之后都不会访问了。)
AC代码:
#include<stdio.h>#include<string.h>#include<algorithm>#include<vector>using namespace std;const int maxn=1e5+50;struct Tree{long long num;int add,lson,rson;}tree[maxn<<5];int cnt,root[maxn<<5],t;void init(){cnt=0;t=0;}void pushup(int rt){tree[rt].num=tree[tree[rt].lson].num+tree[tree[rt].rson].num;}int bulidtree(int l,int r){int v=++cnt;tree[v].add=0;if(l==r){scanf("%lld",&tree[v].num);tree[v].lson=0,tree[v].rson=0;return v;}int mid=(l+r)>>1;tree[v].lson=bulidtree(l,mid);tree[v].rson=bulidtree(mid+1,r);pushup(v);return v;}int update(int p,int l,int r,int L,int R,int d){int v=++cnt;tree[v]=tree[p];tree[v].num+=(R-L+1)*d;if(L<=l&&R>=r){tree[v].add+=d;return v;}int mid=(l+r)>>1;if(R<=mid){tree[v].lson=update(tree[p].lson,l,mid,L,R,d);}else if(L>mid){tree[v].rson=update(tree[p].rson,mid+1,r,L,R,d);}else{tree[v].lson=update(tree[p].lson,l,mid,L,mid,d);tree[v].rson=update(tree[p].rson,mid+1,r,mid+1,R,d);}return v;}long long query(int p,int l,int r,int L,int R){long long ans=(R-L+1)*tree[p].add;if(L<=l&&R>=r){return tree[p].num;}int mid=(l+r)>>1;if(R<=mid){ans+=query(tree[p].lson,l,mid,L,R);}else if(L>mid){ans+=query(tree[p].rson,mid+1,r,L,R);}else{ans+=query(tree[p].lson,l,mid,L,mid);ans+=query(tree[p].rson,mid+1,r,mid+1,R);}return ans;}int main(){int n,m,flag=0;while(scanf("%d%d",&n,&m)==2){if(flag)printf("\n");flag=1;init();root[0]=bulidtree(1,n);for(int i=0;i<m;i++){char a[2];scanf("%s",a);if(a[0]=='C'){int l,r,d;scanf("%d%d%d",&l,&r,&d);root[++t]=update(root[t-1],1,n,l,r,d);//printf("! %d\n",tree[root[t]].num);}else if(a[0]=='Q'){int l,r;scanf("%d%d",&l,&r);printf("%lld\n",query(root[t],1,n,l,r));}else if(a[0]=='H'){int l,r,nt;scanf("%d%d%d",&l,&r,&nt);printf("%lld\n",query(root[nt],1,n,l,r));}else{int a;scanf("%d",&a);t=a;cnt=root[t+1]; //空间优化 }}}}
阅读全文
0 0
- hdu
- hdu
- HDU
- hdu ()
- hdu
- hdu
- HDU
- HDU
- hdu
- hdu
- HDU
- Hdu
- hdu
- hdu-
- hdu
- hdu
- hdu
- HDU
- CodeForce 687A 二分图 交叉染色
- ubantu16.04lts 创建python虚拟环境
- iOS UIScrollview 和侧滑手势冲突解决方法
- python库大全
- iOS UIScrollview 取消弹簧效果
- HDU
- HDU4355 三分
- 判断某个字符串是否是合法IP
- 拦截器,过滤器,监听器的区别
- 前馈神经网络,BP算法
- Angular 2 HostListener & HostBinding
- 【leetcode】129. Sum Root to Leaf Numbers
- Cookie, LocalStorage 与 SessionStorage
- 666666