HDU - 4348 To the moon(主席树)

题目链接:http://acm.hdu.edu.cn/showproblem.php?pid=4348点击打开链接

To the moon

Time Limit: 4000/2000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)
Total Submission(s): 6313    Accepted Submission(s): 1459


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

因为有历史版本询问。。所以主席树

#include <bits/stdc++.h>
using namespace std;
#define maxn 100100
struct xjy
{
    int left ;
    int right;
    long long int flag;
    long long int sum;
};
xjy tree[maxn*30];
int T[maxn];
long long int a[maxn];
int n,m;
int tot;
long long int ans;
int build(int left,int right)
{
    int root=tot++;
    if(left==right)
    {
        tree[root].sum=a[left];
        tree[root].flag=0;
        return root;
    }
    int mid=(left+right)>>1;
    tree[root].left=build(left,mid);
    tree[root].right=build(mid+1,right);
    tree[root].sum=tree[tree[root].left].sum+tree[tree[root].right].sum;
    return root;
}
long long int update(int root,int left,int right,int l,int r,int val)
{
    int now=tot++;
    tree[now].sum=tree[root].sum;
    tree[now].left=tree[root].left;
    tree[now].right=tree[root].right;
    tree[now].flag=tree[root].flag;
    tree[now].sum+=(long long int )(right-left+1)*(long long int )val;
    if(left==l&&right==r)
    {
        tree[now].flag+=(long long int )val;
        return now;
    }
    int mid=(l+r)>>1;
    if(right<=mid)
    {
        tree[now].right=tree[root].right;
        tree[now].left=update(tree[root].left,left,right,l,mid,val);
    }
    else if(left>mid)
    {
        tree[now].left=tree[root].left;
        tree[now].right=update(tree[root].right,left,right,mid+1,r,val);
    }
    else
    {
        tree[now].left=update(tree[root].left,left,mid,l,mid,val);
        tree[now].right=update(tree[root].right,mid+1,right,mid+1,r,val);
    }
    return now;
}

void query(int root,int left,int right,int l,int r)
{
    if(left==l&&right==r)
    {
        ans+=tree[root].sum;
        return ;
    }
    ans+=tree[root].flag*(long long int )(right-left+1);
    int mid=(l+r)>>1;
    if(right<=mid)
    {
        query(tree[root].left,left,right,l,mid);
    }
    else if(left>mid)
    {
        query(tree[root].right,left,right,mid+1,r);
    }
    else
    {
        query(tree[root].left,left,mid,l,mid);
        query(tree[root].right,mid+1,right,mid+1,r);
    }
}
int main()
{
    while(~scanf("%d%d",&n,&m))
    {
        int time=0;
        for(int i=1;i<=n;i++)
            scanf("%lld",&a[i]);
        T[time]=build(1,n);
        for(int i=1;i<=m;i++)
        {
            char c;
            scanf(" %c",&c);
            if(c=='Q')
            {
                int l,r;
                scanf("%d%d",&l,&r);
                ans=0;
                query(T[time],l,r,1,n);
                printf("%lld\n",ans);
            }
            else if(c=='C')
            {
                int l,r,val;
                scanf("%d%d%lld",&l,&r,&val);
                time++;
                T[time]=update(T[time-1],l,r,1,n,val);
            }
            else if(c=='H')
            {
                int l,r,t;
                scanf("%d%d%d",&l,&r,&t);
                ans=0;
                query(T[t],l,r,1,n);
                printf("%lld\n",ans);
            }
            else
            {
                int t;
                scanf("%d",&t);
                time=t;
            }
        }
    }
}




  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值