hdu4348 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

题意:有一个由n个数组成的序列,有4中操作:

1.C l r d [l,r]这段区间都加上d

2.Q l r 询问[l,r]这段区间的和

3.H l r t 询问之前t时间[l,r]的区间和

4.B t 回到t时间,且下一秒的时间从t开始

思路:有两种,一种是在线写法,即用主席树写,按时间建立主席树,主席树上的每一棵线段树维护[1,n]这段序列的信息,这里成段更新的时候要注意,以往写线段树的时候,都是把lazy标记向下传,但是写主席树的时候每一次下传,那么新的节点数就会非常多,会爆内存,所以我们不把lazy操作下传,只是在询问的时候,最后累加的答案加上每一个父亲节点上的lazy值。

还有一种是离线写法,我们先把要询问历史区间和的问题都保存下来,并标记下这个问题是在第几次操作后被问的,这里"C l r d","B t"都算是一次操作,然后再记录每次操作中所问的历史区间和的时间。那么我们每一次操作,就先把这个时间可能被后面问到的历史区间和的问题都回答完,用nas[id]记录id这个问题的答案,然后我们把这个操作中问的问题都删除,因为之后这些问题都不会问了。

在线做法:

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<string>
#include<bitset>
#include<algorithm>
using namespace std;
#define lth th<<1
#define rth th<<1|1
typedef long long ll;
typedef long double ldb;
#define inf 99999999
#define pi acos(-1.0)
#define maxn 100010
#define M 3000000
int lson[M],rson[M],add[M],th,T[M];
ll a[maxn];
ll sum[M];
int n;
int build(int L,int R)
{
    int mid;
    int newroot=++th;
    add[newroot]=0;
    if(L==R){
        sum[newroot]=a[L];
        return newroot;
    }
    mid=(L+R)/2;
    lson[newroot]=build(L,mid);
    rson[newroot]=build(mid+1,R);
    sum[newroot]=sum[lson[newroot] ]+sum[rson[newroot] ];
    return newroot;
}


int update(int root,int L,int R,int l,int r,int value)
{
    int newroot=++th;
    int tmp=newroot;
    int i,j;
    if(L==l && R==r){
        lson[newroot]=lson[root];
        rson[newroot]=rson[root];
        sum[newroot]=sum[root]+(r-l+1)*value;
        add[newroot]=add[root]+value;
        return tmp;
    }
    add[newroot]=add[root];
    sum[newroot]=sum[root]+(r-l+1)*value;
    int mid=(L+R)/2;
    if(r<=mid){
        lson[newroot]=update(lson[root],L,mid,l,r,value );
        rson[newroot ]=rson[root];
    }
    else if(l>mid){
        lson[newroot]=lson[root];
        rson[newroot]=update(rson[root],mid+1,R,l,r,value );
    }
    else{
        lson[newroot]=update(lson[root],L,mid,l,mid,value );
        rson[newroot]=update(rson[root],mid+1,R,mid+1,r,value );
    }
    return tmp;
}

ll question(int root,int L,int R,int l,int r)
{
    int i,j;
    ll ans=0;
    if(L==l && R==r){
        return sum[root];
    }
    ans+=(ll)add[root]*(ll)(r-l+1);
    int mid=(L+R)/2;
    if(r<=mid)ans+=question(lson[root],L,mid,l,r);
    else if(l>mid)ans+=question(rson[root],mid+1,R,l,r);
    else{
        ans+=question(lson[root],L,mid,l,mid);
        ans+=question(rson[root],mid+1,R,mid+1,r);
    }
    return ans;


}



int main()
{
    int m,i,j,c,d,e;
    char str[5];
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(i=1;i<=n;i++){
            scanf("%lld",&a[i]);
        }
        th=0;
        T[0]=build(1,n);
        int tm=0;
        for(i=1;i<=m;i++){
            scanf("%s",str);
            if(str[0]=='C'){
                scanf("%d%d%d",&c,&d,&e);
                tm++;
                T[tm]=update(T[tm-1],1,n,c,d,e);
            }
            else if(str[0]=='Q'){
                scanf("%d%d",&c,&d);
                printf("%lld\n",question(T[tm],1,n,c,d));
            }
            else if(str[0]=='H'){
                scanf("%d%d%d",&c,&d,&e);
                printf("%lld\n",question(T[e],1,n,c,d));
            }
            else if(str[0]=='B'){
                scanf("%d",&e);
                tm=e;
            }
        }

    }
    return 0;
}

离线做法:

#include<iostream>
#include<stdio.h>
#include<stdlib.h>
#include<string.h>
#include<math.h>
#include<vector>
#include<map>
#include<set>
#include<string>
#include<bitset>
#include<algorithm>
using namespace std;
#define lth th<<1
#define rth th<<1|1
typedef long long ll;
typedef long double ldb;
#define inf 99999999
#define pi acos(-1.0)
#define maxn 100010
struct edge{
    int l,r,d,f;
}q[maxn];

int a[maxn];
struct node{
    int l,r,add;
    ll sum;
}b[4*maxn];

void build(int l,int r,int th)
{
    int mid;
    b[th].l=l;b[th].r=r;
    b[th].add=0;
    if(l==r){
        b[th].sum=a[l];return;
    }
    mid=(l+r)/2;
    build(l,mid,lth);
    build(mid+1,r,rth);
    b[th].sum=b[lth].sum+b[rth].sum;
}

void pushdown(int th)
{
    if(b[th].add){
        b[lth].add+=b[th].add;
        b[lth].sum+=(ll)b[th].add*(ll)(b[lth].r-b[lth].l+1);
        b[rth].add+=b[th].add;
        b[rth].sum+=(ll)b[th].add*(ll)(b[rth].r-b[rth].l+1);
        b[th].add=0;
    }

}

void update(int l,int r,int num,int th)
{
    int mid;
    if(b[th].l==l && b[th].r==r){
        b[th].add+=num;
        b[th].sum+=(ll)num*(ll)(r-l+1);
        return;
    }
    pushdown(th);
    mid=(b[th].l+b[th].r)/2;
    if(r<=mid)update(l,r,num,lth);
    else if(l>mid)update(l,r,num,rth);
    else{
        update(l,mid,num,lth);
        update(mid+1,r,num,rth);
    }
    b[th].sum=b[lth].sum+b[rth].sum;
}

ll question(int l,int r,int th)
{
    int mid;
    if(b[th].l==l && b[th].r==r){
        return b[th].sum;
    }
    pushdown(th);
    mid=(b[th].l+b[th].r)/2;
    if(r<=mid)return question(l,r,lth);
    else if(l>mid)return question(l,r,rth);
    else{
        return question(l,mid,lth)+question(mid+1,r,rth);
    }
}

struct node1{
    int l,r,idx,caozuo;
};
bool operator<(node1 a,node1 b){
    return a.caozuo<b.caozuo;
}
multiset<node1>tm[maxn];    //时间为t的历史询问
multiset<node1>::iterator it;

ll ans[maxn];
vector<int>cz[maxn];    //操作为i时的可能历史询问的时间
vector<int>::iterator p;

int chuli[maxn][3];

int main()
{
    int n,m,i,j,cishu,t;
    char str[5];
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        tm[0].clear();cz[0].clear();
        for(i=1;i<=n;i++){
            scanf("%d",&a[i]);
            tm[i].clear();
            cz[i].clear();
        }
        build(1,n,1);
        node1 temp;
        cishu=0;t=0;
        for(i=1;i<=m;i++){
            scanf("%s",str);
            if(str[0]=='C'){
                cishu++;t++;
                q[i].f=1;
                scanf("%d%d%d",&q[i].l,&q[i].r,&q[i].d);
            }
            else if(str[0]=='Q'){
                q[i].f=2;
                scanf("%d%d",&q[i].l,&q[i].r);
            }
            else if(str[0]=='H'){
                q[i].f=3;
                scanf("%d%d%d",&q[i].l,&q[i].r,&q[i].d);
                if(q[i].d==0){
                    ans[i]=question(q[i].l,q[i].r,1);
                    continue;
                }

                temp.l=q[i].l;temp.r=q[i].r;temp.idx=i;temp.caozuo=cishu;
                tm[q[i].d].insert(temp);

                cz[cishu].push_back(q[i].d);
            }
            else if(str[0]=='B'){
                cishu++;
                q[i].f=4;
                scanf("%d",&q[i].d);
                t=q[i].d;
            }
        }


        cishu=0;t=0;

        int shijian;
        for(i=1;i<=m;i++){
            if(q[i].f==1){
                cishu++;t++;
                chuli[t ][0]=q[i].l;chuli[t][1]=q[i].r;chuli[t][2]=q[i].d;
                update(q[i].l,q[i].r,q[i].d,1);
                for(it=tm[t].begin();it!=tm[t].end();it++){
                    temp=*it;
                    ans[temp.idx ]=question(temp.l,temp.r,1);
                }

                for(j=0;j<cz[cishu].size();j++){
                    shijian=cz[cishu][j];
                    for(it=tm[shijian].begin();it!=tm[shijian].end() && (*it).caozuo==cishu; ){
                        tm[shijian].erase(it++);
                    }
                }
            }
            else if(q[i].f==2){
                ans[i]=question(q[i].l,q[i].r,1);
            }
            else if(q[i].f==3){
                continue;
            }
            else if(q[i].f==4){
                cishu++;
                while(t!=q[i].d){
                    update(chuli[t][0],chuli[t][1],-chuli[t][2],1);
                    t--;
                }
                for(j=0;j<cz[cishu].size();j++){
                    shijian=cz[cishu][j];
                    for(it=tm[shijian].begin();it!=tm[shijian].end() && (*it).caozuo==cishu; ){
                        tm[shijian].erase(it++);
                    }
                }
            }
        }
        for(i=1;i<=m;i++){
            if(q[i].f==2 || q[i].f==3){
                printf("%lld\n",ans[i]);
            }
        }
    }
    return 0;
}


posted @ 2016-04-11 20:15  Herumw  阅读(179)  评论(0编辑  收藏  举报