传送门

平衡树好题。

我仍然是用的fhqtreap,感觉速度还行。

维护也比线段树splay什么的写起来简单。

%%%非旋treap大法好。

代码:

#include<bits/stdc++.h>
#define N 500005
#define inf 0x3f3f3f3f
using namespace std;
queue<int>garbage;
typedef pair<int,int> res;
int n,m,rt=0,cnt=0,a[N],son[N][2],siz[N],rd[N],val[N],ls[N],rs[N],ms[N],sum[N],rev[N],cov[N];
inline int read(){
    int ans=0,w=1;
    char ch=getchar();
    while(!isdigit(ch)){if(ch=='-')w=-1;ch=getchar();}
    while(isdigit(ch))ans=(ans<<3)+(ans<<1)+(ch^48),ch=getchar();
    return ans*w;
}
inline int max(int a,int b){return a>b?a:b;}
inline int min(int a,int b){return a<b?a:b;}
inline void swap(int&a,int&b){a^=b,b^=a,a^=b;}
inline int newnode(int v){
    int x;
    if(!garbage.empty())x=garbage.front(),garbage.pop();
    else x=++cnt;
    son[x][0]=son[x][1]=rev[x]=0,cov[x]=inf,siz[x]=1,rd[x]=rand(),val[x]=sum[x]=ms[x]=v,ls[x]=rs[x]=max(v,0);
    return x;
}
inline void pushup(int p){
    if(!son[p][0]&&!son[p][1]){siz[p]=1,sum[p]=ms[p]=val[p],ls[p]=rs[p]=max(val[p],0);return;}
    siz[p]=siz[son[p][0]]+siz[son[p][1]]+1;
    sum[p]=sum[son[p][0]]+sum[son[p][1]]+val[p];
    if(son[p][0]&&son[p][1]){
        ms[p]=max(max(ms[son[p][0]],ms[son[p][1]]),rs[son[p][0]]+val[p]+ls[son[p][1]]);
        ls[p]=max(ls[son[p][0]],sum[son[p][0]]+val[p]+ls[son[p][1]]);
        rs[p]=max(rs[son[p][1]],sum[son[p][1]]+val[p]+rs[son[p][0]]);
        return;
    }
    if(son[p][0]){
        ms[p]=max(ms[son[p][0]],rs[son[p][0]]+val[p]);
        ls[p]=max(ls[son[p][0]],sum[son[p][0]]+val[p]);
        rs[p]=max(0,val[p]+rs[son[p][0]]);
        return;
    }
    ms[p]=max(ms[son[p][1]],ls[son[p][1]]+val[p]);
    rs[p]=max(rs[son[p][1]],sum[son[p][1]]+val[p]);
    ls[p]=max(0,val[p]+ls[son[p][1]]);
}
inline void pushrev(int p){swap(son[p][0],son[p][1]),swap(ls[p],rs[p]),rev[p]^=1;}
inline void pushcov(int p,int v){
    sum[p]=v*siz[p],val[p]=cov[p]=v;
    ls[p]=rs[p]=max(sum[p],0);
    ms[p]=max(sum[p],v);
}
inline void pushdown(int p){
    if(rev[p]){
        if(son[p][0])pushrev(son[p][0]);
        if(son[p][1])pushrev(son[p][1]);
        rev[p]=0;
    }
    if(cov[p]!=inf){
        if(son[p][0])pushcov(son[p][0],cov[p]);
        if(son[p][1])pushcov(son[p][1],cov[p]);
        cov[p]=inf;
    }
}
inline int build(int dat[],int len){
    int p,las=0;
    static int stk[N],top;
    for(int i=1;i<=len;++i){
        p=newnode(dat[i]),las=0;
        while(top&&rd[stk[top]]>rd[p])pushup(stk[top]),las=stk[top],stk[top--]=0;
        if(top)son[stk[top]][1]=p;
        son[p][0]=las,stk[++top]=p;
    }
    while(top)pushup(stk[top--]);
    return stk[1];
}
inline int merge(int a,int b){
    if(!a||!b)return a+b;
    pushdown(a),pushdown(b);
    if(rd[a]<rd[b]){son[a][1]=merge(son[a][1],b),pushup(a);return a;}
    son[b][0]=merge(a,son[b][0]),pushup(b);return b;
}
inline res split(int p,int k){
    if(!p)return res{0,0};
    pushdown(p);
    res ans,tmp;
    if(siz[son[p][0]]>=k){
        tmp=split(son[p][0],k);
        ans.first=tmp.first,son[p][0]=tmp.second,pushup(p),ans.second=p;
        return ans;
    }
    tmp=split(son[p][1],k-siz[son[p][0]]-1);
    ans.second=tmp.second,son[p][1]=tmp.first,pushup(p),ans.first=p;
    return ans;
}
inline void garbages(int p){
    if(!p)return;
    garbage.push(p),garbages(son[p][0]),garbages(son[p][1]);
}
inline void ins(){
    int pos=read(),len=read();
    static int dat[N];
    for(int i=1;i<=len;++i)dat[i]=read();
    int rtt=build(dat,len);
    res x=split(rt,pos);
    rt=merge(merge(x.first,rtt),x.second);
}
inline void del(){
    int pos=read(),len=read();
    res x=split(rt,pos-1),y=split(x.second,len);
    rt=merge(x.first,y.second);
    garbages(y.first);
}
inline void modify(){
    int pos=read(),len=read(),v=read();
    res x=split(rt,pos-1),y=split(x.second,len);
    pushcov(y.first,v);
    rt=merge(x.first,merge(y.first,y.second));
}
inline void reverse(){
    int pos=read(),len=read();
    res x=split(rt,pos-1),y=split(x.second,len);
    pushrev(y.first);
    rt=merge(x.first,merge(y.first,y.second));
}
inline void query(){
    int pos=read(),len=read();
    res x=split(rt,pos-1),y=split(x.second,len);
    printf("%d\n",sum[y.first]);
    rt=merge(x.first,merge(y.first,y.second));
}
int main(){
    srand(time(NULL));
    n=read(),m=read();
    for(int i=1;i<=n;++i)a[i]=read();
    rt=build(a,n);
    while(m--){
        char s[20];
        scanf("%s",s);
        if(s[0]=='I')ins();
        else if(s[0]=='D')del();
        else if(s[0]=='M'&&s[2]=='K')modify();
        else if(s[0]=='R')reverse();
        else if(s[0]=='G')query();
        else printf("%d\n",ms[rt]);
    }
    return 0;
}
04-16 14:49