非旋treap模板

模板题:洛谷3391

#include<cstdio>
#include<iostream>
#include<algorithm>
using namespace std;
const int maxn = 10010;
int v[maxn], R[maxn], l[maxn], r[maxn], vn[maxn], sz[maxn];
int rt, n, cnt, m;
int st[maxn], sttop;
int data[maxn];
int cov[maxn];
inline int qread(){
    register int ch = getchar(), x = 0;
    while(ch < '0' || ch > '9')    ch = getchar();
    while(ch >= '0'    && ch <= '9')    x = (x << 3) + (x << 1) + ch - 48, ch = getchar();
    return x;
}
inline int zrandom() {
    static int seed = 5411;
    return seed = int(seed * 20180801LL % ((1LL << 31) - 1));
}
inline void update(int x) {
    sz[x] = sz[l[x]] + sz[r[x]] + 1;
}
void pushdown(int x){
    swap(l[x], r[x]);
    if(l[x])    cov[l[x]] ^= 1;
    if(r[x])    cov[r[x]] ^= 1;
    cov[x] = 0;
}
int newnode(int w) {
    int x = ++cnt;
    v[x] = w;
    R[x] = zrandom();
    sz[x] = 1;
    return x;
}
int merge(int a, int b) {
    if(!a || !b)    return a | b;
    if(R[a] < R[b])    {
        if(cov[a])    pushdown(a);    
        r[a] = merge(r[a], b);
        update(a);
        return a;
    } else            {
        if(cov[b])    pushdown(b);
        l[b] = merge(a, l[b]);
        update(b);
        return b;
    }
}
pair<int, int> spilt(int x, int k) {
    if(!x)    return make_pair(0, 0);
    pair<int, int> t;
    if(cov[x])    pushdown(x);
    if(k <= sz[l[x]]) {
        t = spilt(l[x], k);
        l[x] = t.second;
        update(x);
        t = make_pair(t.first, x);
    } else {
        t = spilt(r[x], k - sz[l[x]] - 1);
        r[x] = t.first;
        update(x);
        t = make_pair(x, t.second);
    }
    return t;
}
void reverse(int l, int r){
    pair<int, int> p1, p2;
    p1 = spilt(rt, l - 1);
    p2 = spilt(p1.second, r - l + 1);
    cov[p2.first] ^= 1;
    rt = merge(p1.first, merge(p2.first, p2.second));
}
void show(int x) {
    if(!x)    return ;
    if(cov[x])    pushdown(x);
    show(l[x]);
    printf("%d ", v[x]);
    show(r[x]);
}
int main(void) {
    n = qread();
    m = qread();
    for(int i = 1; i <= n; ++i)
        rt = merge(rt, newnode(i));    
    while(m--){
        int x = qread(), y = qread();
        reverse(x, y);
    }
    show(rt);
}

 

posted @ 2018-08-23 16:14  junk_yao  阅读(118)  评论(0)    收藏  举报