hdu-4348 To the moon

To the moon
题目链接
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 7202 Accepted Submission(s): 1675

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
2012 Multi-University Training Contest 5

The gist of the title
is probably to give a sequence of n elements and perform m operations. There are four types of operations (declared in advance, multi-group)
:
(The format is S X1 X2...S represents the name of the operation, which is also an English letter, and the following numbers are used for the operation)
CLR x, for the Lth to the Rth Each element is incremented by x.
B t, return to the state after the t-th C operation (the state before the i-th time is not needed).
QLR, query current i = L R A [ i ] .
HLR t, query after the t-th C operation i = L R A [ i ] (Only query, do not destroy the current state).

Problem Solution
Represents a bare chairperson tree.

code

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>
#define LL long long
using namespace std;
const int maxn=1e5+5,maxm=30*maxn;
int n,m,tot,now,a[maxn],rot[maxn],L[maxm],R[maxm],la[maxm];
LL sum[maxm];
//a表示初始的序列,rot根节点,sum当前子树上的权值,L和R表示左右儿子,la表示懒惰标记 
int read()
{
    int ret=0,f=1;char ch=getchar();
    while (ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=getchar();}
    while (ch>='0'&&ch<='9') ret=ret*10+ch-'0',ch=getchar();
    return ret*f;
}
char gtc(){char ch=getchar();while(ch!='C'&&ch!='Q'&&ch!='H'&&ch!='B')ch=getchar();return ch;}
void updata(int x,int len){sum[x]=(LL)la[x]*len+sum[L[x]]+sum[R[x]];}
int built(int l,int r)
{
    int newrot=++tot;
    la[newrot]=0;
    if (l==r) {sum[tot]=a[l];return tot;}
    int mid=l+r>>1;
    L[newrot]=built(l,mid);
    R[newrot]=built(mid+1,r);
    updata(newrot,r-l+1);
    return newrot;
}
int addtre(int rot,int l,int r,int ll,int rr,int x)
{
    int newrot=++tot;
    la[newrot]=la[rot];
    if (ll<=l&&r<=rr)
    {
        L[newrot]=L[rot];
        R[newrot]=R[rot];
        la[newrot]=la[rot]+x;
        sum[newrot]=sum[rot]+(LL)x*(r-l+1);
        return newrot;
    }
    int mid=l+r>>1;
    if (rr<=mid)
    {
        L[newrot]=addtre(L[rot],l,mid,ll,rr,x);
        R[newrot]=R[rot];
    }else if (ll>mid)
    {
        L[newrot]=L[rot];
        R[newrot]=addtre(R[rot],mid+1,r,ll,rr,x);
    }else
    {
        L[newrot]=addtre(L[rot],l,mid,ll,mid,x);
        R[newrot]=addtre(R[rot],mid+1,r,mid+1,rr,x);
    }
    updata(newrot,r-l+1);
    return newrot;
}
LL query(int rot,int l,int r,int ll,int rr,LL lst)
{
    if (ll<=l&&r<=rr) return sum[rot]+lst*(r-l+1);
    lst+=la[rot];
    int mid=l+r>>1;
    if (rr<=mid) return query(L[rot],l,mid,ll,rr,lst);else
    if (ll>mid)  return query(R[rot],mid+1,r,ll,rr,lst);else
    return query(L[rot],l,mid,ll,mid,lst)+query(R[rot],mid+1,r,mid+1,rr,lst);
}
int main()
{
    while (cin>>n>>m)
    {
        tot=now=0;
        for (int i=1;i<=n;i++) a[i]=read();
        rot[now]=built(1,n);
        int le,ri,x;
        for (int i=1;i<=m;i++)
        {
            switch (gtc())
            {
                case 'B':now=read();break;
                case 'C':le=read();ri=read();x=read();now++;rot[now]=addtre(rot[now-1],1,n,le,ri,x);break;
                case 'Q':le=read();ri=read();printf("%lld\n",query(rot[now],1,n,le,ri,0));break;
                case 'H':le=read();ri=read();x=read();printf("%lld\n",query(rot[x],1,n,le,ri,0));break;
            }
        }
    }
    return 0;
}

Guess you like

Origin http://43.154.161.224:23101/article/api/json?id=324810874&siteId=291194637