HDU 4348 To the moon 可持久化线段树

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 {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
 
 
题意:
   
  给你一个数组,让你维护,m次操作
  询问当前时刻一个区间的和
  询问在t时刻的一个区间的和
  回到t时刻
  时间+1,在此时刻+1下更新一个区间的值
 
题解:
  
  函数式线段树的裸体
 
#include <iostream>
#include <cstdio>
#include <cmath>
#include <cstring>
#include <algorithm>
using namespace std;
const int N = 5e6+10, M = 1e3+10, mod = 1000000, inf = 1e9+1000;
typedef long long ll;

int n,m;
int l[N],r[N],root[N],add[N],tot = 0;
ll sum[N];
int build(int s,int t) {
    int now = ++tot;
    add[now] = 0;
    if(s==t) {
        scanf("%lld",&sum[now]);
        l[now] = r[now] = 0;
        return now;
    }
    int mid = (s+t)>>1;
    l[now] = build(s,mid);
    r[now] = build(mid+1,t);
    sum[now] = sum[l[now]]+sum[r[now]];
    return now;
}
//查询在以t为根内[ll,rr]区间的值
ll query(int k,int lll,int rr,int s,int t) {
    ll ans = (add[k]*(rr-lll+1));
    if(lll==s&&rr==t) return sum[k];
    int mid = (s+t)>>1;
    if(rr<=mid) ans+=query(l[k],lll,rr,s,mid);
    else if(lll>mid) ans+=query(r[k],lll,rr,mid+1,t);
    else {
        ans+=query(l[k],lll,mid,s,mid);
        ans+=query(r[k],mid+1,rr,mid+1,t);
    }
    return ans;
}
int update(int k,int lll,int rr,int d,int s,int t) {
    int now = ++tot;
    l[now] = l[k];
    r[now] = r[k];
    add[now] = add[k];
    sum[now] = sum[k];
    sum[now]+=(ll) (d*(rr-lll+1));
    if(lll==s&&rr==t) {
        add[now]+=d;
        return now;
    }
    int mid = (s+t)>>1;
    if(rr<=mid) l[now] = update(l[k],lll,rr,d,s,mid);
    else if(lll>mid) r[now] = update(r[k],lll,rr,d,mid+1,t);
    else {
        l[now] = update(l[k],lll,mid,d,s,mid);
        r[now] = update(r[k],mid+1,rr,d,mid+1,t);
    }
    return now;
}
void solve() {
    tot = 0;
    root[0] = build(1,n);
    int now = 0;
    for(int i=1;i<=m;i++) {
        char ch[3];
        scanf("%s",ch);
        if(ch[0]=='Q') {
                int a,b;
            scanf("%d%d",&a,&b);
            printf("%lld\n",query(root[now],a,b,1,n));
        }
        else if(ch[0]=='C') {
                int a,b,d;
            scanf("%d%d%d",&a,&b,&d);
            root[now+1] = update(root[now],a,b,d,1,n);
            now++;
        }
        else if(ch[0]=='H') {
                int a,b,t;
            scanf("%d%d%d",&a,&b,&t);
            printf("%lld\n",query(root[t],a,b,1,n));
        }
        else scanf("%d",&now);
    }
}
int main() {
    while(scanf("%d%d",&n,&m)!=EOF) {
        solve();
    }
    return 0;
}

 

posted @ 2016-05-07 21:41  meekyan  阅读(189)  评论(0编辑  收藏  举报