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 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162
| #include <bits/stdc++.h>
#define x first #define y second #define y1 Y1 #define y2 Y2 #define mp make_pair #define pb push_back
using namespace std;
typedef long long LL; typedef pair<int, int> pii;
template <typename T> inline int Chkmax (T &a, T b) { return a < b ? a = b, 1 : 0; } template <typename T> inline int Chkmin (T &a, T b) { return a > b ? a = b, 1 : 0; }
inline void proc_status() { ifstream t ("/proc/self/status"); cerr << string (istreambuf_iterator <char> (t), istreambuf_iterator <char> ()) <<endl; }
template <typename T> T read () { T sum = 0, fl = 1; char ch = getchar(); for (; !isdigit(ch); ch = getchar()) if (ch == '-') fl = -1; for (; isdigit(ch); ch = getchar()) sum = (sum << 3) + (sum << 1) + ch - '0'; return sum * fl; }
const int Maxn = 5e5 + 100;
int N, M, Q;
int node_cnt, id[Maxn], real_num; struct tree { int fa, ch[2], rev, sum, val; } Tree[Maxn];
namespace LCT { #define ls (Tree[x].ch[0]) #define rs (Tree[x].ch[1]) int Stack[Maxn], top;
inline int is_root (int x) { return Tree[Tree[x].fa].ch[0] != x && Tree[Tree[x].fa].ch[1] != x; } inline int judge (int x) { return Tree[Tree[x].fa].ch[1] == x; } inline void connect (int x, int f, int dir) { Tree[x].fa = f; Tree[f].ch[dir] = x; } inline void push_up (int x) { Tree[x].sum = Tree[ls].sum + Tree[rs].sum + Tree[x].val; } inline void push_down (int x) { if (Tree[x].rev) swap(ls, rs), Tree[ls].rev ^= 1, Tree[rs].rev ^= 1, Tree[x].rev = 0; } inline void rotate (int x) { int f = Tree[x].fa, anc = Tree[f].fa, dirx = judge (x), dirf = judge (f); if (!is_root(f)) Tree[anc].ch[dirf] = x; Tree[x].fa = anc; connect (Tree[x].ch[dirx ^ 1], f, dirx); connect (f, x, dirx ^ 1); push_up(f), push_up(x); } inline void splay (int x) { Stack[++top] = x; for (int y = x; !is_root(y); y = Tree[y].fa) Stack[++top] = Tree[y].fa; while (top) push_down(Stack[top--]); for (; !is_root(x); rotate (x)) if (!is_root(Tree[x].fa)) rotate (judge(x) == judge(Tree[x].fa) ? Tree[x].fa : x); }
inline int access (int x) { int y = 0; for (; x; x = Tree[y = x].fa) splay(x), rs = y, push_up(x); return y; } inline void link (int x, int f) { splay(x); Tree[x].fa = f; } inline void cut (int x) { access(x), splay (x); Tree[ls].fa = 0, ls = 0; push_up(x); } inline void newnode (int f, int val) { ++node_cnt; Tree[node_cnt].val = Tree[node_cnt].sum = val; link (node_cnt, f); if (val) id[++real_num] = node_cnt; } inline int query (int x, int y) { int ans = 0; access (x), splay(x), ans += Tree[x].sum; int lca = access(y); splay(y), ans += Tree[y].sum; access(lca), splay(lca), ans -= Tree[lca].sum * 2; return ans; }
#undef ls #undef rs }
struct query { int pos, time, x, y, op; } Query[Maxn];
inline int cmp (query a, query b) { if (a.pos == b.pos) { if (a.op == b.op) return a.time < b.time; return a.op < b.op; } return a.pos < b.pos; }
int L[Maxn], R[Maxn], Ans[Maxn];
inline void Solve () { LCT :: newnode(0, 1), LCT :: newnode(1, 0); L[real_num] = 1, R[real_num] = N; int last = 2, op_cnt = 0;
for (int i = 1; i <= M; ++i) { int op = read<int>(); if (!op) { LCT :: newnode (last, 1); L[real_num] = read<int>(), R[real_num] = read<int>(); } else if (op == 1) { int l = read<int>(), r = read<int>(), x = read<int>(); Chkmax(l, L[x]), Chkmin(r, R[x]); if (l > r) continue; LCT :: newnode (last, 0); Query[++op_cnt] = (query){l, i, node_cnt, id[x], op}; Query[++op_cnt] = (query){r + 1, i, node_cnt, last, op}; last = node_cnt; } else { int x = read<int>(), u = read<int>(), v = read<int>(); Query[++op_cnt] = (query){x, ++Q, id[u], id[v], op}; } } sort (Query + 1, Query + op_cnt + 1, cmp);
for (int i = 1; i <= op_cnt; ++i) { if (Query[i].op == 1) LCT :: cut(Query[i].x), LCT :: link (Query[i].x, Query[i].y); else Ans[Query[i].time] = LCT :: query (Query[i].x, Query[i].y); }
for (int i = 1; i <= Q; ++i) printf("%d\n", Ans[i]); }
inline void Input () { N = read<int>(), M = read<int>(); }
int main() { #ifndef ONLINE_JUDGE freopen("A.in", "r", stdin); freopen("A.out", "w", stdout); #endif Input(); Solve(); return 0; }
|