HDU 4348 To the moon

HDU 4348 To the moon

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

一.解题思路及要点分析

  1. 题目大意:给定一个数组进行四种操作:

    1. 将一个区间的所有数加上d
    2. 对一个区间求和
    3. 求一个区间在时间t时的和
    4. 返回到t时刻
  2. 这道题的关键在于 update 操作

    由于标记的存在,传统的线段树是有 pushup 和 pushdown 操作的。
    但是如果对于可持久化线段树,每次更新都 pushdown,就要开多很多个节点,会使空间和时间大大增加。
    于是我们用到标记永久化思想!
    就是,我们每次将标记覆盖区间后,再也不下传了。
    每次查询的时候,对查询的区间加上查询路径上标记的贡献就可以了。(因为查询路径上的标记对区间的和是有贡献的)

二.代码

#include<string.h>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn=1e5+5;
int n,m,cnt,t,a,b,c;
int tag[maxn*25],ls[maxn*25],rs[maxn*25],root[maxn*25];//注意要开25倍,20倍的话会出现非法访问内存
long long sum[maxn*25];
char ch[2];//最好用字符串数组,别用字符因为读取输入时可能会写入空白字符导致出错
void build(int l,int r,int &rt )
{
    rt=++cnt;
    if(l==r)
    {
        scanf("%lld",&sum[rt]);//注意是lld而不是d,小心long long与int分别进行匹配
        return ;
    }
    int mid=(l+r)>>1;
    build(l,mid,ls[rt]);
    build(mid+1,r,rs[rt]);
    sum[rt]=sum[ls[rt]]+sum[rs[rt]];
    return ;
}

void update(int l,int r,int &rt ,int a,int b,int d,int pre)
{
    rt=++cnt;
    sum[rt]=sum[pre]+d*(min(r,b)-max(a,l)+1);//每次都更新公共区间
    tag[rt]=tag[pre];
    ls[rt]=ls[pre];
    rs[rt]=rs[pre];
    if(l>=a&&r<=b) 
    {
        tag[rt]+=d;
        return ;
    }
    int mid=(l+r)>>1;
    if(a<=mid)update(l,mid,ls[rt],a,b,d,ls[pre]);
    if(b>mid)update(mid+1,r,rs[rt],a,b,d,rs[pre]);
}

long long query(int l,int r,int rt ,int a,int b)
{
    if(l>=a&&r<=b) return sum[rt];
    int mid=(l+r)>>1;
    long long ans=tag[rt]*(min(b,r)-max(a,l)+1);
    if(a<=mid)ans+=query(l,mid,ls[rt],a,b);
    if(b>mid)ans+=query(mid+1,r,rs[rt],a,b);
    return ans;
}

int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        //注意初始化
        cnt=0;
        t=0;
        memset(sum,0,sizeof(sum));
        memset(ls,0,sizeof(ls));
        memset(rs,0,sizeof(rs));
        memset(root,0,sizeof(root));
        memset(tag,0,sizeof(tag));
        build(1,n,root[0]);
        for(int i=1;i<=m;i++)
        {
            scanf("%s",ch);
            if(ch[0]=='B')
            {
                scanf("%d",&c);
                t=c;//将时间设置为c
            }
            else if(ch[0]=='Q')
            {
                scanf("%d%d",&a,&b);
                printf("%lld\n",query(1,n,root[t],a,b));
            }
            else if(ch[0]=='H')
            {
                scanf("%d%d%d",&a,&b,&c);
                printf("%lld\n",query(1,n,root[c],a,b));
            }
            else if(ch[0]=='C')
            {
                scanf("%d%d%d",&a,&b,&c);
                t++;//注意修改操作时时间应该往前推
                update(1,n,root[t],a,b,c,root[t-1]);
            }
        }
    }
}
  • 1
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值