HDU 4348 可持久化线段树

本文介绍了一个基于角色扮演的冒险游戏ToTheMoon中实现的记忆重构算法。该算法使用了主席树进行区间更新,实现了对序列的操作包括区间加值、求区间和、历史区间和及返回指定时刻的状态。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

http://www.elijahqi.win/2017/07/01/hdu-4348/
To the moon
Time Limit: 4000/2000 MS (Java/Others) Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 5117 Accepted Submission(s): 1152

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的序列 执行4种操作
C l r d 区间[l,r]的数全部增加d 并且当前时刻增加一
Q l r 求区间[l,r]的和
H l r t 求第t个时刻
B t 返回第t个时刻
题解:据说是主席树区间更新入门题目。

#include <cstdio>
#include <cstring>
#define N 530005
inline int read(){
    int x=0,f=1;char ch=getchar();
    while (ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=getchar();}
    while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return f*x;
}
inline long long read1(){
    long long x=0,f=1;char ch=getchar();
    while (ch<'0'||ch>'9') {if (ch=='-') f=-1;ch=getchar();}
    while (ch>='0'&&ch<='9'){x=x*10+ch-'0';ch=getchar();}
    return f*x;
}
struct node{
    int left,right;long long sum,add;
}tree[20*N];
int stamp,L,R,d,t,a[N],num,root[N];
void build(int &p,int l,int r){
    p=++num;tree[p].sum=tree[p].add=0;
    if (l==r){
        tree[p].sum=a[l];return;
    }
    int mid=(l+r)>>1;
    build(tree[p].left,l,mid);build(tree[p].right,mid+1,r);
    tree[p].sum=tree[tree[p].left].sum+tree[tree[p].right].sum;
}
void updata(int p,int l,int r){
    int l1=tree[p].left,r1=tree[p].right;
    tree[p].sum=tree[l1].sum+tree[r1].sum+(r-l+1)*tree[p].add;
}
void insert1(int L,int R,int &p,int l,int r,int val){
    tree[++num]=tree[p];p=num;
    if (l==L&&R==r) {
        tree[p].sum+=(r-l+1)*val;tree[p].add+=val;return;
    }
    int mid=(L+R)>>1;
    if (r<=mid) insert1(L,mid,tree[p].left,l,r,val);else{
        if (l>mid)insert1(mid+1,R,tree[p].right,l,r,val);else{
            insert1(L,mid,tree[p].left,l,mid,val);
            insert1(mid+1,R,tree[p].right,mid+1,r,val);
        }
    }
    updata(p,L,R);
}
long long getsum(int L,int R,int p,int l,int r){
    if (l==L&&r==R)return tree[p].sum;
    int mid=(L+R)>>1;long long tmp=(r-l+1)*tree[p].add;
    if (r<=mid) tmp+=getsum(L,mid,tree[p].left,l,r);else{
        if (l>mid)tmp+=getsum(mid+1,R,tree[p].right,l,r);else{
            tmp+=getsum(L,mid,tree[p].left,l,mid);
            tmp+=getsum(mid+1,R,tree[p].right,mid+1,r);
        }
    }
    /*if (l<=mid) tmp+=getsum(L,mid,tree[p].left,l,r);
    if (r>=mid+1) tmp+=getsum(mid+1,R,tree[p].right,l,r);*/
    return tmp;
}
void print(int l,int r,int p){
    int mid=(l+r)>>1;
    if (tree[p].left) print(l,mid,tree[p].left);
    printf("%d %d %d\n",l,r,tree[p].sum);
    if (tree[p].right) print(mid+1,r,tree[p].right);
}
int main(){
    freopen("hdu4348.in","r",stdin);
    freopen("hdu4348.out","w",stdout);
    int n,m;
    while (scanf("%d%d",&n,&m)>0){
        stamp=d=t=num=0;
        for (int i=1;i<=n;++i) a[i]=read();
        build(root[stamp],1,n);char str1[10];
        //print(1,n,root[stamp]);
        for (int i=1;i<=m;++i){
            scanf("%s",str1);
            if (str1[0]=='Q')  L=read(),R=read(),printf("%lld\n",getsum(1,n,root[stamp],L,R));
            if (str1[0]=='C')  L=read(),R=read(),d=read1(),root[++stamp]=root[stamp-1],insert1(1,n,root[stamp],L,R,d);
            if (str1[0]=='H')  L=read(),R=read(),t=read(),printf("%lld\n",getsum(1,n,root[t],L,R));
            if (str1[0]=='B')  t=read(),stamp=t;//,print(1,n,root[stamp]),printf("asdf\n");
            //print(1,n,root[stamp]);printf("asdf\n");
        }
        //print(1,n,root[1]);printf("asdff\n");print(1,n,root[2]);
        //printf("asdfadsf\n");
    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值