可持久化数组用rope搞就MLE了有办法吗这个)
#include <bits/stdc++.h>
typedef std::pair <int, int> pii;
#define rep(i, x, y) for (int i = (x); i <= (y); ++i)
#define per(i, x, y) for (int i = (x); i >= (y); --i)
int read () {
int res = 0;
bool f = false;
char temp = getchar();
for (; !isdigit(temp); temp = getchar()) f = temp == '-';
for (; isdigit(temp); temp = getchar()) res = res * 10 + temp - '0';
if (f) return -res;
return res;
}
#include <ext/rope>
constexpr int maxn = 2e5 + 5;
using namespace __gnu_cxx;
typedef rope <int> rpi;
int ts[maxn], n, m;
rpi *s[maxn], *sz[maxn];
int fd (rpi *r, int x) {
int t = r->at(x);
if (t != x) return fd(r, t);
return x;
}
void uni (int u, int x, int y) {
x = fd(s[u], x), y = fd(s[u], y);
if (x == y) return;
int sx = sz[u]->at(x), sy = sz[u]->at(y);
if (sx > sy) s[u]->replace(y, x), sz[u]->replace(x, sx + sy);
else s[u]->replace(x, y), sz[u]->replace(y, sx + sy);
}
signed main () {
n = read(), m = read();
rep (i, 0, n) ts[i] = i;
s[0] = new rpi;
s[0]->append(ts, ts + 1 + n);
sz[0] = new rpi;
sz[0]->append(n + 1, 1);
rep (i, 1, m) {
int op = read();
if (op == 1) {
int a = read(), b = read();
s[i] = new rpi(*s[i - 1]);
sz[i] = new rpi(*sz[i - 1]);
uni(i, a, b);
} else if (op == 2) {
int a = read();
s[i] = new rpi(*s[a]);
sz[i] = new rpi(*sz[a]);
} else {
int a = read(), b = read();
s[i] = new rpi(*s[i - 1]);
sz[i] = new rpi(*sz[i - 1]);
printf("%d\n", fd(s[i], a) == fd(s[i], b));
}
}
return 0;
}