1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136
| #include<bits/stdc++.h>
using namespace std;
const int N = 100005;
int n, q, ans;
int fa[N]; int findfa(int x){ return x == fa[x] ? x : fa[x] = findfa(fa[x]); }
struct LinkCutTree{ int sta[N], staTop; struct Splay{ int son[2], fa; int sz, si; bool rev; }tr[N]; #define which(x, y) (tr[y].son[1] == x) inline void pushup(int x){ if(x){ tr[x].sz = tr[x].si + 1; if(tr[x].son[0]) tr[x].sz += tr[tr[x].son[0]].sz; if(tr[x].son[1]) tr[x].sz += tr[tr[x].son[1]].sz; } } inline void pushdown(int x){ if(tr[x].rev){ if(tr[x].son[0]){ tr[tr[x].son[0]].rev ^= 1; swap(tr[tr[x].son[0]].son[0], tr[tr[x].son[0]].son[1]); } if(tr[x].son[1]){ tr[tr[x].son[1]].rev ^= 1; swap(tr[tr[x].son[1]].son[0], tr[tr[x].son[1]].son[1]); } tr[x].rev ^= 1; } } inline bool isRoot(int x){ return tr[tr[x].fa].son[0] != x && tr[tr[x].fa].son[1] != x; } inline void rotate(int x, int dir){ int y = tr[x].fa, z = tr[y].fa, B = tr[x].son[dir]; if(!isRoot(y)) tr[z].son[which(y,z)] = x; tr[x].son[dir] = y; tr[y].son[dir^1] = B; tr[x].fa = z; tr[y].fa = x; tr[B].fa = y; pushup(y); pushup(x); } inline void splay(int x){ sta[staTop = 1] = x; for(int i = x; !isRoot(i); i = tr[i].fa) sta[++staTop] = tr[i].fa; while(staTop) pushdown(sta[staTop--]); while(!isRoot(x)){ int y = tr[x].fa, z = tr[y].fa, dir1 = which(x,y)^1, dir2 = which(y,z)^1; if(isRoot(y)) rotate(x, dir1); else{ if(dir1 == dir2) rotate(y, dir2); else rotate(x, dir1); rotate(x, dir2); } } } inline void access(int x){ for(int y = 0; x; y = x, x = tr[x].fa){ splay(x); tr[x].si += tr[tr[x].son[1]].sz - tr[y].sz; tr[x].son[1] = y; pushup(x); } } inline void makeRoot(int x){ access(x); splay(x); tr[x].rev ^= 1; swap(tr[x].son[0], tr[x].son[1]); pushup(x); } inline int findRoot(int x){ access(x); splay(x); while(tr[x].son[0]) x = tr[x].son[0]; return x; } inline void link(int x, int y){ makeRoot(x); access(y); splay(y); if(findRoot(y) != x){ tr[x].fa = y; tr[y].si += tr[x].sz; pushup(y); } } inline void cut(int x, int y){ makeRoot(x); access(y); splay(y); if(tr[y].son[0] != x) return; tr[y].son[0] = tr[x].fa = 0; pushup(y); }
inline int maintain(int x, int y){ int g = 1e9, ls = 0, rs = 0; int _x = x, _y = y; makeRoot(x); int tot = tr[x].sz; access(y); splay(y); while(y){ pushdown(y); int lt = ls + tr[tr[y].son[0]].sz; int rt = rs + tr[tr[y].son[1]].sz; if(max(lt, rt) <= tot / 2) g = min(g, y); if(lt < rt) ls += tr[tr[y].son[0]].sz + tr[y].si + 1, y = tr[y].son[1]; else rs += tr[tr[y].son[1]].sz + tr[y].si + 1, y = tr[y].son[0]; } makeRoot(g); fa[g] = fa[_x] = fa[_y] = g; return g; } }LCT;
int main(){ scanf("%d%d", &n, &q); for(int i = 1; i <= n; i++) ans ^= i, fa[i] = i; while(q--){ char opt[2]; int x, y; scanf("%s", opt); if(opt[0] == 'A'){ scanf("%d%d", &x, &y); int gx = findfa(x), gy = findfa(y); LCT.link(x, y); ans ^= gx ^ gy; ans ^= LCT.maintain(gx, gy); } else if(opt[0] == 'Q'){ scanf("%d", &x); printf("%d\n", LCT.findRoot(x)); } else printf("%d\n", ans); } return 0; }
|