hdu 4348 主席树的区间更新

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 11, A 22,…, A NN. 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 ii| ≤ 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

题意:

  一个长度为n的数组,4种操作 :

    (1)C l r d:区间[l,r]中的数都加1,同时当前的时间戳加1 。

    (2)Q l r:查询当前时间戳区间[l,r]中所有数的和 。

    (3)H l r t:查询时间戳t区间[l,r]的和 。

    (4)B t:将当前时间戳置为t 。

  所有操作均合法 。

很明显可以想到每个时间点是一颗线段树,由于有时间上的限制,所以用主席树,对于每棵线段树进行一般的区间操作即可,但是这里不能使用pushdown 因为节点很多(因为你更新到下一棵树的时候复制是上一棵树的如果pushdown要对很多的树进行pushdown,(因为查询的时间不一定),会很麻烦),所以是人工的更新lazy标记统计和,让其的值确定下来,成为一个静态的,对后面的树不再有影响。所以区间更新和询问的时候不能像一般线段树一样,而是要指定特定的区间,而且都是更新在不同是时间点上 例如 laz[now] 和 ls[now],rs[now]。查询的时候人工求和的过程是 找到指定的区间 每一层累加上 要找的区间*laz[now],还有不同的地方就是在pushup的地方要把(r-l+1)*laz 也统计上去sum除,这样查询到的最后的sum才是对的

#include <bits/stdc++.h>
using namespace std;
const int N = 1e5+100;
typedef long long ll;
ll a[N];
int ls[N*40],rs[N*40];
typedef long long ll;
ll sum[N*40],laz[N*40];
int root[N*40];
int tot;

void pushup(int l,int r,int now)
{
    sum[now]=sum[ls[now]]+sum[rs[now]]+1LL*(r-l+1)*laz[now];
}

int build(int l,int r)
{
    int now=++tot;
    sum[now]=0;
    laz[now]=0;
    if(l==r)
    {
        sum[now]=a[l];
        return now;
    }
    int mid=(l+r)>>1;
    ls[now]=build(l,mid);
    rs[now]=build(mid+1,r);
    pushup(l,r,now);
    return now;
}

void update(int pre,int &now,int L,int R,int l,int r,ll d)
{
    now=++tot;
    ls[now]=ls[pre];
    rs[now]=rs[pre];
    sum[now]=sum[pre];
    laz[now]=laz[pre];
    if(l==L&&R==r)
    {
        sum[now]+=1LL*(R-L+1)*d;
        laz[now]+=d;
        return ;
    }
    int mid=(l+r)>>1;
    if(R<=mid) update(ls[pre],ls[now],L,R,l,mid,d);
    else if(L>mid) update(rs[pre],rs[now],L,R,mid+1,r,d);
    else{
        update(ls[pre],ls[now],L,mid,l,mid,d);
        update(rs[pre],rs[now],mid+1,R,mid+1,r,d);
    }
    pushup(l,r,now);
}

ll query(int now,int L,int R,int l,int r)
{
    if(L==l&&R==r)
    {
        return sum[now];
    }
    ll res=1LL*(R-L+1)*laz[now];
    int mid=(l+r)>>1;
    if(R<=mid) res+=query(ls[now],L,R,l,mid);
    else if(L>mid) res+=query(rs[now],L,R,mid+1,r);
    else {
        res+=query(ls[now],L,mid,l,mid);
        res+=query(rs[now],mid+1,R,mid+1,r);
    }
    return res;
}

int main()
{
    int n,m;
    while(scanf("%d%d",&n,&m)!=EOF)
    {
    tot=0;
    int now=0;

    for(int i=1;i<=n;i++)
    {
        scanf("%lld",&a[i]);
    }
    root[0]=build(1,n);
    for(int i=1;i<=m;i++){
        char ch;
        scanf(" %c",&ch);
        if(ch=='C') {
            int l,r;
            ll d;
            scanf("%d%d%lld",&l,&r,&d);
            update(root[now],root[now+1],l,r,1,n,d);
            now++;
        }
        else if(ch=='Q')
        {
            int l,r;
            scanf("%d%d",&l,&r);
            printf("%lld\n",query(root[now],l,r,1,n));
        }
        else if(ch=='H')
        {
            int l,r,d;
            scanf("%d%d%d",&l,&r,&d);
            printf("%lld\n",query(root[d],l,r,1,n));
        }
        else{
            int d;
            scanf("%d",&d);
            now=d;
        }
    }
    }

}
posted @ 2017-09-19 21:18  黑码的博客  阅读(127)  评论(0编辑  收藏  举报