bzoj 1895: Pku3580 supermemo (splay)

题目描述

传送门

题目大意:给出一个初始序列a1,a2,….,a3,
要求你编写程序支持如下操作: 1. ADD x y D:给子序列ax… ay的每个元素都加上D。
2. REVERSE x y:将子序列ax… ay翻转。
3. REVOLVE x y T:将子序列ax… ay旋转T个单位。
4. INSERT x P:在Ax后插入P。
5. DELETE x:删去Ax。
6. MIN x y:查询子序列ax… ay中的最小元素。

题解

splay裸题。注意旋转的时候需要对区间的长度取模。
找前驱的时候,需要下放标记!!!

代码

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define N 200003
#define inf 1000000000
using namespace std;
int n,m,root,val[N],ch[N][2],size[N],mn[N],delta[N],a[N],rev[N],cnt,fa[N];
void update(int now)
{
    int l=ch[now][0]; int r=ch[now][1]; size[now]=1;
    mn[now]=val[now];
    if (l)mn[now]=min(mn[now],mn[l]),size[now]+=size[l];
    if (r)mn[now]=min(mn[now],mn[r]),size[now]+=size[r];
}
void change(int now,int t)
{
    val[now]+=t; mn[now]+=t; delta[now]+=t;
}
void reverse(int now)
{
    swap(ch[now][0],ch[now][1]); 
    rev[now]^=1;
}
void pushdown(int now)
{
    int l=ch[now][0]; int r=ch[now][1];
    if (delta[now]) {
        change(l,delta[now]); change(r,delta[now]);
        delta[now]=0;
    }
    if (rev[now]) {
        reverse(l); reverse(r);
        rev[now]=0;
    }
}
int build(int l,int r)
{
    int now;
    if (l>r) return 0;
    if (l==r) {
        now=++cnt; val[now]=mn[now]=a[l]; size[now]=1;
        return now;
    }
    int mid=(l+r)/2; now=++cnt; val[now]=mn[now]=a[mid];
    ch[now][0]=build(l,mid-1);
    ch[now][1]=build(mid+1,r);
    fa[ch[now][0]]=now; fa[ch[now][1]]=now;
    update(now);
    return now;
}
int get(int x)
{
    return ch[fa[x]][1]==x;
}
void rotate(int x)
{
    int y=fa[x]; int z=fa[y];
    pushdown(y); pushdown(x); int which=get(x);
    ch[y][which]=ch[x][which^1]; fa[ch[x][which^1]]=y;
    ch[x][which^1]=y; fa[y]=x; fa[x]=z;
    if (z) ch[z][ch[z][1]==y]=x;
    update(y); update(x);
}
void splay(int x,int tar)
{
    for (int f;(f=fa[x])!=tar;rotate(x))
     if (fa[f]!=tar) rotate(get(f)==get(x)?f:x);
    if (!tar) root=x;
}
int find(int x)
{
    int now=root;
    while (true) {
        pushdown(now);
        if (x<=size[ch[now][0]]) now=ch[now][0];
        else {
            x-=size[ch[now][0]];
            if (x==1) return now;
            x--; now=ch[now][1];
        }
    }
}
int pre()
{
    pushdown(root);
    int now=ch[root][0]; pushdown(now);
    while (ch[now][1]){
      now=ch[now][1];
      pushdown(now);
    }
    return now;
}
void clear(int x)
{
    ch[x][1]=ch[x][0]=fa[x]=0;
}
void del(int x)
{
    x=find(x+1); splay(x,0);
    if (!ch[root][0]&&!ch[root][1]) {
       clear(root); root=0; return; 
    }
    if (!ch[root][0]) {
        int oldroot=root; root=ch[root][1]; fa[root]=0;
        clear(oldroot); return;
    }
    if (!ch[root][1]) {
        int oldroot=root; root=ch[root][0]; fa[root]=0;
        clear(oldroot); return;
    }
    int t=pre(); int oldroot=root;
    splay(t,0); ch[root][1]=ch[oldroot][1]; fa[ch[oldroot][1]]=root; 
    clear(oldroot); update(root);
}
int main()
{
    freopen("a.in","r",stdin);
    freopen("my.out","w",stdout);
    scanf("%d",&n);
    a[1]=a[n+2]=inf;
    for (int i=2;i<=n+1;i++) scanf("%d",&a[i]);
    root=build(1,n+2);
    scanf("%d",&m);
    for (int i=1;i<=m;i++) {
        //cout<<i<<endl;
        char s[10]; int x,y,k; scanf("%s",s+1);
        if (s[1]=='D') {
            scanf("%d",&x);
            del(x);
        }
        if (s[1]=='A') {
            scanf("%d%d%d",&x,&y,&k);
            int aa=find(x); int bb=find(y+2);
            splay(aa,0); splay(bb,aa);
            change(ch[ch[root][1]][0],k); update(ch[root][1]); update(root);
        }
        if (s[1]=='M') {
            scanf("%d%d",&x,&y);
            int aa=find(x); int bb=find(y+2);
            splay(aa,0); splay(bb,aa);
            printf("%d\n",mn[ch[ch[root][1]][0]]);
        }
        if (s[1]=='I') {
            scanf("%d%d",&x,&k);
            int aa=find(x+1); int bb=find(x+2);
            splay(aa,0); splay(bb,aa);
            ch[ch[root][1]][0]=++cnt;
            fa[cnt]=ch[root][1]; size[cnt]=1; mn[cnt]=val[cnt]=k;
            update(ch[root][1]); update(root); 
        }
        if (s[1]=='R'&&s[4]=='E') {
            scanf("%d%d",&x,&y);
            int aa=find(x); int bb=find(y+2);
            splay(aa,0); splay(bb,aa);
            reverse(ch[ch[root][1]][0]);
        }
        if (s[1]=='R'&&s[4]=='O') {
            scanf("%d%d%d",&x,&y,&k); int len=y-x+1;
            k=(k%len+len)%len; if (!k) continue;
            int aa=find(y+1-k); int bb=find(y+2);
            splay(aa,0); splay(bb,aa);
            int t=ch[ch[root][1]][0]; fa[t]=0; 
            ch[ch[root][1]][0]=0; update(ch[root][1]); update(root);
            aa=find(x); bb=find(x+1);
            splay(aa,0); splay(bb,aa);
            ch[ch[root][1]][0]=t; fa[t]=ch[root][1];
            update(ch[root][1]); update(root);
        }
    }
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值