hdu4348 To the moon

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 {A i | 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 {A i | l <= i <= r}.
3. H l r t: Querying a history sum of {A i | 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 ≤ 10 5, |A [i]| ≤ 10 9, 1 ≤ l ≤ r ≤ N, |d| ≤ 10 4 .. 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
A 1 A 2 ... A n
... (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
正解:可持久化线段树

区间修改时新建一棵线段树,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
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值