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

题目大意
大概就是给出一个 n 个元素的序列,进行 m 次操作。(提前声明,多组的)
操作有四类:
(格式为S X1 X2…S表示操作名称,也是一个英文字母,后面若干个数字是操作要用到的)
C L R x,给第 L 到第 R 个元素每个增加 x。
B t,返回第 t 次C操作后的状态(第 i 次之前的状态就不要了)。
Q L R,查询当前 Ri=LA[i] ∑ i = L R A [ i ]
H L R t,查询第 t 次C操作后的 Ri=LA[i] ∑ i = L R A [ i ] (只查询,不破坏当前状态)。

题解
表示裸的主席树。

代码

#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;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值