Splay(区间修改)模板

#include<bits/stdc++.h>
using namespace std;

const int MAXN=1e5+2333;

int n,m;
int a[MAXN];

inline int read(){
    int num=0;char ch=getchar();
    while (!isdigit(ch)) ch=getchar();
    while (isdigit(ch)) num=num*10+ch-48,ch=getchar();
    return num;
}

namespace Splay {
    const int N=1e5+2333;
    int root,cnt;
    int siz[MAXN],ch[MAXN][2],fa[MAXN],tag[MAXN];
    void update(int x) {
        siz[x]=siz[ch[x][0]]+siz[ch[x][1]]+1;
    }
    void rotate(int x,int &pl) {
        int y=fa[x],z=fa[y],d=(ch[y][1]==x);
        ch[y][d]=ch[x][d^1],fa[ch[x][d^1]]=y;
        ch[x][d^1]=y,fa[y]=x;
        fa[x]=z;
        if (y!=pl) ch[z][ch[z][1]==y]=x;
        else pl=x;
        update(y),update(x);
    }
    void splay(int x,int &pl) {
        for (int y; (y=fa[x])&&x!=pl ; rotate(x,pl)) // ??????
            if (y!=pl)
                rotate((x==ch[y][0])^(y==ch[fa[y]][0]) ? x:y,pl);
    }
    void insert(int x,int vt) {
        if (!x) {
            root=x=++cnt,ch[x][0]=ch[x][1]=fa[x]=0,siz[x]=1;
            return;
        }
        int y;
        while (true) {
            y=ch[x][x<vt];
            if (!y) {
                y=++cnt,ch[y][0]=ch[y][1]=0,fa[y]=x,siz[y]=1,ch[x][x<vt]=y;
                break;
            }
            x=y;
        }
        splay(y,root);
    }
    void push_down(int x) {
        if (tag[x]) {
            tag[x]=0,tag[ch[x][0]]^=1,tag[ch[x][1]]^=1;
            swap(ch[x][0],ch[x][1]);
        }
    }
    int find_kth(int x,int k) {
        push_down(x);
        if (siz[ch[x][0]]+1==k)
            return x;
        else if (siz[ch[x][0]]>=k)
            return find_kth(ch[x][0],k);
        else return find_kth(ch[x][1],k-siz[ch[x][0]]-1);
    }
    void rever(int l,int r) {
        int x=find_kth(root,l-1),y=find_kth(root,r+1);
        splay(x,root),splay(y,ch[x][1]);
        tag[ch[y][0]]^=1;
    }
}
using namespace Splay;

int main() {
    n=read(),m=read();
    for (int i=1; i<=n+2; i++)
        insert(root,i);
    while (m--) {
        int l=read(),r=read();
        rever(l+1,r+1);
    }
    for (int i=2; i<=n+1; i++)
        printf("%d ",find_kth(root,i)-1);
    return 0;
}

 

posted @ 2018-08-24 12:25  QingCai_DCF  阅读(146)  评论(0编辑  收藏  举报