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;
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章