文藝平衡樹
阿新 • • 發佈:2020-06-30
#include<iostream> #include<cstring> #include<algorithm> #include<cmath> #include<cstdlib> #include<climits> #include<stack> #include<vector> #include<queue> #include<set> #include<bitset> #include<map> //#include<regex> #include<cstdio> #include <iomanip> #pragma GCC optimize(2) #define up(i,a,b) for(int i=a;i<b;i++) #define dw(i,a,b) for(int i=a;i>b;i--) #define upd(i,a,b) for(int i=a;i<=b;i++) #define dwd(i,a,b) for(int i=a;i>=b;i--) //#define local typedef long long ll; typedef unsigned long long ull; const double esp = 1e-6; const double pi = acos(-1.0); const int INF = 0x3f3f3f3f; const int inf = 1e9; using namespace std; ll read() { char ch = getchar(); ll x = 0, f = 1; while (ch<'0' || ch>'9') { if (ch == '-')f = -1; ch = getchar(); } while (ch >= '0' && ch <= '9') { x = x * 10 + ch - '0'; ch = getchar(); } return x * f; } typedef pair<int, int> pir; #define lson l,mid,root<<1 #define rson mid+1,r,root<<1|1 #define lrt root<<1 #define rrt root<<1|1 const int N = 1e5 + 10; int root, tot; struct node { int ch[2], fa, sz, cnt, val, lazy; }tree[N]; int a[N]; void pushup(int rt) { tree[rt].sz = tree[tree[rt].ch[0]].sz + tree[tree[rt].ch[1]].sz + tree[rt].cnt; } void pushdown(int rt) { if (rt&&tree[rt].lazy) { int ls = tree[rt].ch[0]; int rs = tree[rt].ch[1]; swap(tree[rt].ch[0], tree[rt].ch[1]); tree[ls].lazy ^= 1; tree[rs].lazy ^= 1; tree[rt].lazy = 0; } } bool ident(int u, int fa) { return tree[fa].ch[1] == u; } void connect(int u,int fa,int son) { tree[u].fa = fa; tree[fa].ch[son] = u; } void rotate(int u) { int f = tree[u].fa; int ff = tree[f].fa; int isson = ident(u, f); connect(u, ff, ident(f,ff)); connect(tree[u].ch[isson ^ 1], f, isson); connect(f, u, isson ^ 1); pushup(f); pushup(u); } void splay(int u, int goal) { while (tree[u].fa != goal) { int f = tree[u].fa; int ff = tree[f].fa; if (ff != goal) (ident(f, ff) ^ (ident(u, f))) ? rotate(u) : rotate(f); rotate(u); } if (goal == 0)root = u; } int K_th(int k) { int u = root; if (tree[u].sz < k)return -1; while (23) { pushdown(u); int v = tree[u].ch[0]; if (k > tree[v].sz + tree[u].cnt) { k -= tree[v].sz + tree[u].cnt; u = tree[u].ch[1]; } else { if (tree[v].sz >= k) u = v; else return u; } } } void build(int l, int r,int fa,int &o) { if (l > r)return; o = ++tot; int now = o; int mid = (l + r) >> 1; tree[now].val = a[mid]; tree[now].cnt = 1; tree[now].fa = fa; tree[now].cnt = 1; build(l, mid - 1, now, tree[now].ch[0]); build(mid + 1, r, now, tree[now].ch[1]); pushup(now); } void rever(int l, int r) { int L = l; int R = r + 2; int templ = K_th(L); int tempr = K_th(R); splay(templ, 0); splay(tempr, templ); int temprson = tree[tempr].ch[0]; tree[temprson].lazy ^= 1; } void print(int o) { if (!o)return; pushdown(o); print(tree[o].ch[0]); if (tree[o].val != INF && tree[o].val != -INF) printf("%d ", tree[o].val); print(tree[o].ch[1]); } int main() { int n, m; n = read(), m = read(); upd(i, 1, n) a[i + 1] = i; a[1] = -INF; a[2 + n] = INF; build(1, n + 2, 0, root); root = 1; int l, r; while (m--) { l = read(), r = read(); rever(l, r); } print(root); }