[CodeVS1343]蚱蜢 非旋转式Treap

非旋转式Treap代码请看周魏大爷blog
http://blog.csdn.net/zawedx/article/details/54706561
一个写程序不需要if语句,外加变态压行的大爷

听q神说用非旋转Treap写LCT多一个log?

#include 
#include 
#include 
#include 
#define N 200050

#define mp make_pair
#define L tr[t].l
#define R tr[t].r 

using namespace std;
typedef pair<int,int> par;
int n,m,a[N];
int r,r1,r2,tmp;
struct Node{ int l,r,v,mx,key,siz; }tr[N];

inline void gp(int &x,int &y,par p) { x = p.first , y = p.second; }
inline void maintain(int t) {
    tr[t].siz = tr[L].siz + tr[R].siz + 1;
    tr[t].mx = max(tr[t].v , max(tr[L].mx , tr[R].mx));   
}

int mer(int x,int y) {
    if (!x || !y) return x + y;
    if (tr[x].key > tr[y].key) {
        tr[x].r = mer(tr[x].r,y); maintain(x); return x;
    } else {
        tr[y].l = mer(x,tr[y].l); maintain(y); return y;
    }
}

#define cur (tr[L].siz + 1)
par spl(int t,int k) {
    if (!k) return mp(0,t);
    if (k == tr[t].siz) return mp(t,0);
    int tmp = 0;
    if (k < cur){
        gp(tmp,L,spl(L,k)) , maintain(t); return mp(tmp,t);
    } else {
        gp(R,tmp,spl(R,k-cur)) , maintain(t); return mp(t,tmp);
    }
}

int main() {
    srand(4396);
    scanf("%d%d",&n,&m);
    for (int i=1;i<=n;i++) scanf("%d",&a[i]);
    for (int i=1;i<=n;i++) {
        tr[i].mx = tr[i].v = a[i]; tr[i].siz = 1;
        tr[i].key = rand();
        r = i==1 ? 1 : mer(r,i);
    }
    while (m--) {
        int x,y; char cmd[10];
        scanf("%d%s%d",&x,cmd+1,&y);
        if (cmd[1] == 'D') {
            gp(r,r1,spl(r,x));
            gp(r1,r2,spl(r1,y));
            printf("%d\n",tr[r1].mx);
            gp(r,tmp,spl(r,x-1));
            r1 = mer(r1,tmp);
            r = mer(r,r1);
            r = mer(r,r2);
        } else {
            gp(r,r1,spl(r,x-1));
            gp(r2,r,spl(r,x-y-1));
            printf("%d\n",tr[r].mx);
            gp(tmp,r1,spl(r1,1));
            r2 = mer(r2,tmp);
            r = mer(r2,r);
            r = mer(r,r1);
        }
    } 
    return 0;
} 

你可能感兴趣的:([CodeVS1343]蚱蜢 非旋转式Treap)