hdu4348 To the moon

来源:互联网 发布:淘宝购物卖家没退运费 编辑:程序博客网 时间:2024/06/03 23:00

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 51 2 3 4 5 6 7 8 9 10Q 4 4Q 1 10Q 2 4C 3 6 3Q 2 42 40 0C 1 1 1C 2 2 -1Q 1 2H 1 2 1
 

Sample Output
45591501
正解:可持久化线段树

区间修改时新建一棵线段树,lazy数组不要下放,节省空间,回溯时上放就好。区间查询时查询root[i]就行,将查询答案加上经过的lazy。

//It is made by wfj_2048~#include <algorithm>#include <iostream>#include <cstring>#include <cstdlib>#include <cstdio>#include <vector>#include <cmath>#include <queue>#include <stack>#include <map>#include <set>#define inf 1<<30#define il inline#define RG register#define ll long long#define File(s) freopen(s".in","r",stdin),freopen(s".out","w",stdout)using namespace std;int ls[3000010],rs[3000010],lazy[3000010],root[100010],a[100010],n,m,sz,l,r,t,now;ll sum[3000010];char ch[3];il int gi(){    RG int x=0,q=0; RG char ch=getchar();    while ((ch<'0' || ch>'9') && ch!='-') ch=getchar(); if (ch=='-') q=1,ch=getchar();    while (ch>='0' && ch<='9') x=x*10+ch-48,ch=getchar(); return q ? -x : x;}il void build(RG int &x,RG int l,RG int r){    x=++sz; if (l==r){ sum[x]=a[l]; return; } RG int mid=(l+r)>>1;    build(ls[x],l,mid),build(rs[x],mid+1,r); sum[x]=sum[ls[x]]+sum[rs[x]]; return;}il void update(RG int last,RG int &now,RG int l,RG int r,RG int xl,RG int xr,RG int v){    now=++sz,ls[now]=ls[last],rs[now]=rs[last],sum[now]=sum[last],lazy[now]=lazy[last];    if (xl<=l && r<=xr){ sum[now]+=(r-l+1)*v,lazy[now]+=v; return; } RG int mid=(l+r)>>1;    if (xr<=mid) update(ls[last],ls[now],l,mid,xl,xr,v);    else if (xl>mid) update(rs[last],rs[now],mid+1,r,xl,xr,v);    else update(ls[last],ls[now],l,mid,xl,mid,v),update(rs[last],rs[now],mid+1,r,mid+1,xr,v);    sum[now]=sum[ls[now]]+sum[rs[now]]+lazy[now]*(r-l+1); return;}il ll query(RG int now,RG int l,RG int r,RG int xl,RG int xr,RG int la){    if (xl<=l && r<=xr) return sum[now]+la*(r-l+1); RG int mid=(l+r)>>1; RG ll res=0; la+=lazy[now];    if (xr<=mid) res=query(ls[now],l,mid,xl,xr,la);    else if (xl>mid) res=query(rs[now],mid+1,r,xl,xr,la);    else res=query(ls[now],l,mid,xl,mid,la)+query(rs[now],mid+1,r,mid+1,xr,la);    return res;}il void work(){    for (RG int i=1;i<=n;++i) a[i]=gi(); build(root[0],1,n);    for (RG int i=1;i<=m;++i){scanf("%s",ch);if (ch[0]=='C'){ l=gi(),r=gi(),t=gi(),now++; update(root[now-1],root[now],1,n,l,r,t); }if (ch[0]=='Q'){ l=gi(),r=gi(); printf("%lld\n",query(root[now],1,n,l,r,0)); }if (ch[0]=='H'){ l=gi(),r=gi(),t=gi(); printf("%lld\n",query(root[t],1,n,l,r,0)); }if (ch[0]=='B') t=gi(),now=t;    }    return;}int main(){    File("hdu4348");    while (scanf("%d%d",&n,&m)==2) sz=now=0,work();    return 0;}


0 0
原创粉丝点击