To the moon

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 5117    Accepted Submission(s): 1152


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.
 

 

Input
n m
A1 A2 ... An
... (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
 题意:对于一个长度为n的序列   执行4种操作
C l r d  区间[l,r]的数全部增加d 并且当前时刻增加一
Q l r    求区间[l,r]的和
H l r t  求第t个时刻
B t       返回第t个时刻
题解:据说是主席树区间更新入门题目。
  1 #pragma comment(linker, "/STACK:1024000000,1024000000")
  2 #include <iostream>
  3 #include <cstdio>
  4 #include <cstdlib>
  5 #include <cstring>
  6 #include <algorithm>
  7 #include <stack>
  8 #include <queue>
  9 #include <cmath>
 10 #include <map>
 11 #define ll  __int64
 12 #define mod 1000000007
 13 #define dazhi 2147483647
 14 #define N 530005
 15 using namespace  std;
 16 ll a[N];
 17 char s[10];
 18 struct chairmantree
 19 {
 20     int rt[20*N],ls[20*N],rs[20*N];
 21     ll sum[N*20],lazy[20*N];
 22     int tot;
 23     void init()
 24     {
 25         tot=0;
 26     }
 27     void pushup(int l,int r,int pos)
 28     {
 29         sum[pos]=sum[ls[pos]]+sum[rs[pos]]+1LL*(r-l+1)*lazy[pos];
 30     }
 31     void buildtree(int l,int r,int &pos)
 32     {
 33         pos=++tot;
 34         lazy[pos]=0;
 35         sum[pos]=0;
 36         if(l==r)
 37         {
 38             sum[pos]=a[l];
 39             return ;
 40         }
 41         int mid=(l+r)>>1;
 42         buildtree(l,mid,ls[pos]);
 43         buildtree(mid+1,r,rs[pos]);
 44         pushup(l,r,pos);
 45     }
 46     void update(int L,int R,ll c,int pre,int l,int r,int &pos)
 47     {
 48         pos=++tot;
 49         ls[pos]=ls[pre];
 50         rs[pos]=rs[pre];
 51         sum[pos]=sum[pre];
 52         lazy[pos]=lazy[pre];
 53         if(L==l&&R==r)
 54         {
 55             sum[pos]+=1LL*(r-l+1)*c;
 56             lazy[pos]+=c;
 57             return ;
 58         }
 59         int mid=(l+r)>>1;
 60         if(R<=mid)
 61             update(L,R,c,ls[pre],l,mid,ls[pos]);
 62         else
 63         {
 64             if(L>mid)
 65                 update(L,R,c,rs[pre],mid+1,r,rs[pos]);
 66             else
 67             {
 68                 update(L,mid,c,ls[pre],l,mid,ls[pos]);
 69                 update(mid+1,R,c,rs[pre],mid+1,r,rs[pos]);
 70             }
 71         }
 72         pushup(l,r,pos);
 73     }
 74     ll query(int L,int R,int l,int r,int pos)
 75     {
 76         if(L==l&&R==r)
 77             return sum[pos];
 78         int mid=(l+r)>>1;
 79         ll ans=1LL*lazy[pos]*(R-L+1);
 80         if(R<=mid)
 81             ans+=query(L,R,l,mid,ls[pos]);
 82         else
 83         {
 84             if(L>mid)
 85                 ans+=query(L,R,mid+1,r,rs[pos]);
 86             else
 87             {
 88                 ans+=query(L,mid,l,mid,ls[pos]);
 89                 ans+=query(mid+1,R,mid+1,r,rs[pos]);
 90             }
 91         }
 92         return ans;
 93     }
 94 } tree;
 95 int main()
 96 {
 97     int n,m;
 98     while(scanf("%d %d",&n,&m)!=EOF)
 99     {
100         for(int i=1; i<=n; i++)
101             scanf("%I64d",&a[i]);
102         tree.init();
103         tree.buildtree(1,n,tree.rt[0]);
104         int now=0;
105         while(m--)
106         {
107             scanf("%s",s);
108             if(s[0]=='C')
109             {
110                 int l,r;
111                 ll c;
112                 scanf("%d%d%I64d",&l,&r,&c);
113                 tree.update(l,r,c,tree.rt[now],1,n,tree.rt[now+1]);
114                 now++;
115             }
116             if(s[0]=='Q')
117             {
118                 int l,r;
119                 scanf("%d %d",&l,&r);
120                 printf("%I64d\n",tree.query(l,r,1,n,tree.rt[now]));
121             }
122             if(s[0]=='H')
123             {
124                 int l,r,t;
125                 scanf("%d %d %d",&l,&r,&t);
126                 printf("%I64d\n",tree.query(l,r,1,n,tree.rt[t]));
127             }
128             if(s[0]=='B')
129             {
130                 int x;
131                 scanf("%d",&x);
132                 now=x;
133             }
134         }
135     }
136     return 0;
137 }