treap求调

P3391 【模板】文艺平衡树

Land_ER @ 2022-05-15 13:38:03

rt,样例是对的,交上去全WA

#include <bits/stdc++.h>
typedef int ptr;
const int N = 100005;
using std::pair;
using std::make_pair;
class tree {
    private:
        int num[N], priority[N], size[N];
        ptr lc[N], rc[N], root, tot;
        std::bitset<N> tag;
        void pushdown(ptr id);
        pair<ptr, ptr> split(ptr id, int n);
        ptr merge(ptr a, ptr b);
        void printans(ptr id);
    public:
        void insert(int n);
        void filp(int l, int r);
        void print(void);
};
void tree::pushdown(ptr id) {
    if(tag[id]) {
        std::swap(lc[id], rc[id]);
        tag[id] = false;
        if(lc[id])
            tag[lc[id]] = !tag[lc[id]];
        if(rc[id])
            tag[rc[id]] = !tag[rc[id]];
    }
    return;
}
pair<ptr, ptr> tree::split(ptr id, int n) {
    if(!id)
        return make_pair(0, 0);
    pushdown(id);
    pair<ptr, ptr> p;
    if(n <= size[lc[id]]) {
        p = split(lc[id], n);
        lc[id] = p.second;
        p.second = id;
        return p;
    } else {
        p = split(rc[id], n-size[lc[id]]-1);
        rc[id] = p.first;
        p.first = id;
        return p;
    }
}
ptr tree::merge(ptr a, ptr b) {
    pushdown(a);
    pushdown(b);
    if(!a)
        return b;
    if(!b)
        return a;
    if(priority[a] > priority[b]) {
        rc[a] = merge(rc[a], b);
        size[a] = size[lc[a]] + size[rc[a]] + 1;
        return a;
    } else {
        lc[b] = merge(a, lc[b]);
        size[b] = size[lc[b]] + size[rc[b]] + 1;
        return b;
    }
}
void tree::printans(ptr id) {
    if(!id)
        return;
    pushdown(id);
    printans(lc[id]);
    printf("%d ", num[id]);
    printans(rc[id]);
    return;
}
void tree::insert(int n) {
    static std::mt19937 rnd(0);
    ++ tot;
    num[tot] = n;
    priority[tot] = rnd();
    size[tot] = 1;
    root = merge(root, tot);
    return;
}
void tree::filp(int l, int r) {
    pair<ptr, ptr> p, q;
    p = split(root, l-1);
    q = split(p.second, r-l+1);
    tag[q.first] = !tag[q.first];
    p.second = merge(q.first, q.second);
    root = merge(p.first, p.second);
    return;
}
void tree::print(void) {
    printans(root);
    return;
}
tree t;
int n, m, l, r, a;
int main(void) {
    scanf("%d %d", &n, &m);
    for(int i = 1; i <= n; ++ i)
        t.insert(i);
    while(m --) {
        scanf("%d %d", &l, &r);
        t.filp(l, r);
    }
    t.print();
    return 0;
}

|