HDU - 4348 To the moon(主席树)

xiaoxiao2021-02-28  144

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 {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   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]; //空间优化 } } } }
转载请注明原文地址: https://www.6miu.com/read-61202.html

最新回复(0)