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
| #include <cstdio> #include <algorithm> using namespace std; template <class Type> void read (Type &x) { char c; bool flag = false; while ((c = getchar()) < '0' || c > '9') c == '-' && (flag = true); x = c - '0'; while ((c = getchar()) >= '0' && c <= '9') x = (x << 1) + (x << 3) + c - '0'; flag && (x = ~x + 1); } template <class Type, class ...rest> void read (Type &x, rest &...y) { read(x), read(y...); } template <class Type> void write (Type x) { x < 0 && (putchar('-'), x = ~x + 1); x > 9 && (write(x / 10), 0); putchar('0' + x % 10); } const int N = 2e5 + 10, M = 1e7 + 10; int n, m, rt[N], idx; struct Node { int l, r, d, p; } tr[M]; void build (int &x, int l = 1, int r = n) { x = ++idx; if (l == r) return tr[x].p = l, tr[x].d = 1, void(); int mid = l + r >> 1; build(tr[x].l, l, mid), build(tr[x].r, mid + 1, r); } void insert (int &x, int t, int p, int d, int l = 1, int r = n) { tr[++idx] = tr[x]; x = idx; if (l == r) return tr[x].p = p, tr[x].d = d, void(); int mid = l + r >> 1; t <= mid ? insert(tr[x].l, t, p, d, l, mid) : insert(tr[x].r, t, p, d, mid + 1, r); } int QueryP (int x, int t, int l = 1, int r = n) { if (l == r) return tr[x].p; int mid = l + r >> 1; return t <= mid ? QueryP(tr[x].l, t, l, mid) : QueryP(tr[x].r, t, mid + 1, r); } int QueryD (int x, int t, int l = 1, int r = n) { if (l == r) return tr[x].d; int mid = l + r >> 1; return t <= mid ? QueryD(tr[x].l, t, l, mid) : QueryD(tr[x].r, t, mid + 1, r); } int fa (int t, int x) { int k = QueryP(t, x); return k == x ? x : fa(t, k); } int main () { read(n, m); build(rt[0]); for (int i = 1, op; i <= m; ++i) { read(op); if (op == 1) { int a, b; read(a, b); rt[i] = rt[i - 1]; a = fa(rt[i], a), b = fa(rt[i], b); if (a == b) continue; int da = QueryD(rt[i], a), db = QueryD(rt[i], b); if (da == db) { insert(rt[i], a, b, da); insert(rt[i], b, b, db + 1); } else { if (da > db) swap(a, b), swap(da, db); insert(rt[i], a, b, da); } } else if (op == 2) { int k; read(k); rt[i] = rt[k]; } else if (op == 3) { int a, b; read(a, b); rt[i] = rt[i - 1]; write(fa(rt[i], a) == fa(rt[i], b)), puts(""); } } return 0; }
|