HDU4348

To the moon

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


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
 题解:一眼主席树? 是把...优化空间  永久标记 随便搞搞就行了 1A噢~~~
#include <bits/stdc++.h>
const int MAXN=1e5+10;
#define ll long long
using namespace std;
ll read(){
    ll x=0,f=1;char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')f=-1;ch=getchar();}
    while(isdigit(ch))x=x*10+ch-'0',ch=getchar();
    return x*f;
}
int n,m,Time,cnt;
ll a[MAXN];
typedef struct node{
    int l,r;ll sum,flag;
}node;
node d[MAXN*21*4];int rt[MAXN];
void built(int &x,int l,int r){
    if(!x)x=++cnt;
    if(l==r){d[x].sum=a[l];d[x].flag=0;return ;}
    int mid=(l+r)>>1;
    built(d[x].l,l,mid);
    built(d[x].r,mid+1,r);
    d[x].sum=d[d[x].l].sum+d[d[x].r].sum;
}
void update(int &x,int y,int l,int r,int ql,int qr,ll c){
    x=++cnt;d[x]=d[y];
    if(ql<=l&&r<=qr){d[x].sum+=1ll*(r-l+1)*c;d[x].flag+=c;return ;}
    int mid=(l+r)>>1;
    if(ql<=mid)update(d[x].l,d[y].l,l,mid,ql,qr,c);
    if(qr>mid)update(d[x].r,d[y].r,mid+1,r,ql,qr,c);
    d[x].sum=d[d[x].l].sum+d[d[x].r].sum+1ll*(r-l+1)*d[x].flag;
}
ll ans;
void querty(int x,int l,int r,int ql,int qr,ll t){
    if(!x){ans+=1ll*t*(min(r,qr)-max(l,ql)+1);return ;}
    if(ql<=l&&r<=qr){ans+=d[x].sum;ans+=1ll*(r-l+1)*t;return ;}
    int mid=(l+r)>>1;
    if(ql<=mid)querty(d[x].l,l,mid,ql,qr,t+d[x].flag);
    if(qr>mid)querty(d[x].r,mid+1,r,ql,qr,t+d[x].flag);
}
int main(){
    while(scanf("%d%d",&n,&m)==2){
	for(int i=1;i<=n;i++)a[i]=read();
	Time=0;cnt=0;memset(rt,0,sizeof(rt));
	built(rt[Time],1,n);
	char ch;int l,r,t;ll c;
	for(int i=1;i<=m;i++){
	    scanf(" %c",&ch);
	    if(ch=='C')Time++,l=read(),r=read(),c=read(),update(rt[Time],rt[Time-1],1,n,l,r,c);
	    else if(ch=='Q')l=read(),r=read(),ans=0,querty(rt[Time],1,n,l,r,0),printf("%lld\n",ans);
	    else if(ch=='H')l=read(),r=read(),t=read(),ans=0,querty(rt[t],1,n,l,r,0),printf("%lld\n",ans);
	    else t=read(),Time=t;
	}
    }
    return 0;
}

 

posted @ 2018-06-05 02:48  wang9897  阅读(129)  评论(0编辑  收藏  举报