HDU 4348 / SPOJ TTM To the moon [主席树]

Description

You‘ve been given N integers A[1], A[2],..., A[N]. On these integers, you need to implement the following operations:

  • C l r d: Adding a constant d for every {Ai | l <= i <= r}, and increase the timestamp by 1, this is the only operation that will cause the timestamp increase. 
  • Q l r: Querying the current sum of {Ai | l <= i <= r}.
  • H l r t: Querying a history sum of {Ai | l <= i <= r} in time t.
  • 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. 


题意:给出N个数的序列,有四种操作:

1.成段区间加D,此时的序列有一个时间T来表示它(每次操作1之后T+1)

2.询问当前序列区间和

3.询问T时刻序列的区间和

4.回到T时刻(这里比较坑,题意不是很清楚,下次T+1是从回到的时间开始,WA了无数遍)


解法:主席树可写,时空复杂度为NlogNlogN,算是模板题了,不赘述。考虑了一下,其实还可以线段树离线处理询问直接搞,时间复杂度为NlogN,空间为N,将T时刻的询问先存起来,等到了T时刻的时候直接把相关询问处理完(需要区分是第几次到T时刻),返回T时刻的时候,将前面做过的区间加重新减回去即可,因为每个相反的操作最多重新做一遍,所以复杂度依旧是NlogN。

主席树代码:

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<math.h>
#include<iostream>
#include<stdlib.h>
#include<set>
#include<map>
#include<queue>
#include<vector>
#include<bitset>
#pragma comment(linker, "/STACK:1024000000,1024000000")
template <class T>
bool scanff(T &ret){ //Faster Input
    char c; int sgn; T bit=0.1;
    if(c=getchar(),c==EOF) return 0;
    while(c!='-'&&c!='.'&&(c<'0'||c>'9')) c=getchar();
    sgn=(c=='-')?-1:1;
    ret=(c=='-')?0:(c-'0');
    while(c=getchar(),c>='0'&&c<='9') ret=ret*10+(c-'0');
    if(c==' '||c=='\n'){ ret*=sgn; return 1; }
    while(c=getchar(),c>='0'&&c<='9') ret+=(c-'0')*bit,bit/=10;
    ret*=sgn;
    return 1;
}
#define inf 1073741823
#define llinf 4611686018427387903LL
#define PI acos(-1.0)
#define lth (th<<1)
#define rth (th<<1|1)
#define rep(i,a,b) for(int i=int(a);i<=int(b);i++)
#define drep(i,a,b) for(int i=int(a);i>=int(b);i--)
#define gson(i,root) for(int i=ptx[root];~i;i=ed[i].next)
#define tdata int testnum;scanff(testnum);for(int cas=1;cas<=testnum;cas++)
#define mem(x,val) memset(x,val,sizeof(x))
#define mkp(a,b) make_pair(a,b)
#define findx(x) lower_bound(b+1,b+1+bn,x)-b
#define pb(x) push_back(x)
using namespace std;
typedef long long ll;
typedef pair<int,int> pii;

#define NN 100100
#define MM 2500000

int n,qn,ql,qr;
int tot;
int lson[MM],rson[MM];
ll c[MM],add[MM];
ll a[NN];
int t[NN];
int build(int l,int r){
    int x=++tot;
    if(l!=r){
        int m=(l+r)>>1;
        lson[x]=build(l,m);
        rson[x]=build(m+1,r);
        c[x]=c[lson[x]]+c[rson[x]];
    }
    else c[x]=a[l];
    return x;
}
void update(int l,int r,int pre,int cur,ll val){
    lson[cur]=lson[pre];
    rson[cur]=rson[pre];
    c[cur]=c[pre];
    add[cur]=add[pre];
    if(l>=ql&&r<=qr){
        add[cur]+=val;
        if(l==r)c[cur]=a[l]+add[cur]*(r-l+1);
        else c[cur]=c[lson[cur]]+c[rson[cur]]+add[cur]*(r-l+1);
        return ;
    }
    int m=(l+r)>>1;
    if(m>=ql)update(l,m,lson[pre],lson[cur]=++tot,val);
    if(m<qr)update(m+1,r,rson[pre],rson[cur]=++tot,val);
    c[cur]=c[lson[cur]]+c[rson[cur]]+add[cur]*(r-l+1);

}
ll query(int l,int r,int x,ll d){
    if(l>=ql&&r<=qr)return c[x]+(r-l+1)*d;
    int m=(l+r)>>1;
    ll sum=0;
    if(m>=ql)sum+=query(l,m,lson[x],d+add[x]);
    if(m<qr)sum+=query(m+1,r,rson[x],d+add[x]);
    return sum;
}

char op[111];
int main(){
    scanff(n);
    scanff(qn);
    rep(i,1,n)scanff(a[i]);
    build(1,n);
    t[0]=1;
    int pre=0;
    int cur=0;
    rep(i,1,qn){
        scanf("%s",op);
        if(op[0]=='C'){
            ll val;
            scanff(ql);
            scanff(qr);
            scanff(val);
            t[++cur]=++tot;
            update(1,n,t[pre],t[cur],val);
            pre=cur;
        }
        if(op[0]=='B'){
            scanff(pre);
            cur=pre;
        }
        if(op[0]=='Q'){
            scanff(ql);
            scanff(qr);
            printf("%lld\n",query(1,n,t[pre],0));
        }
        if(op[0]=='H'){
            int k;
            scanff(ql);
            scanff(qr);
            scanff(k);
            printf("%lld\n",query(1,n,t[k],0));
        }
    }

}
/*
10 1000
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
*/






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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值