我的代码在不该输出 NO,的时候输出 NO。
#pragma GCC optimize(2,3,"Ofast","inline")
#include <bits/stdc++.h>
using namespace std;
//#define int long long
#define N 400010
int sq, n, co[N], l[N], r[N], tot;
struct note {
int l, r, id;
} s[N];
vector<note> q[1005], A, emp;
int f[N];
int get(int x) {
return x == f[x] ? x : f[x] = get(f[x]);
}
void merge(int x, int y) {
return f[get(x)] = get(y), void();
}
int qop[N], ql[N], qr[N], val[N * 2], cnt;
int find(int x) {
return lower_bound(val + 1, val + cnt + 1, x) - val;
}
signed main() {
ios::sync_with_stdio(0), cin.tie(0), cout.tie(0);
//freopen("1.in","r",stdin);
//freopen("1.out","w",stdout);
cin >> n, sq = sqrt(N - 10);
for (int i = 1; i <= N - 10; i++)
co[i] = (i - 1) / sq + 1, f[i] = i;
for (int i = 1; i <= N - 10; i++)
l[i] = co[i] == co[i - 1] ? l[i - 1] : i;
for (int i = N - 10; i >= 1; i--)
r[i] = co[i] == co[i + 1] ? r[i + 1] : i;
for (int i = 1; i <= n; i++) {
cin >> qop[i] >> ql[i] >> qr[i];
if (qop[i] == 1)
val[++cnt] = ql[i], val[++cnt] = qr[i];
}
sort(val + 1, val + cnt + 1), cnt = unique(val + 1, val + cnt + 1) - val - 1;
for (int i = 1; i <= n; i++)
if (qop[i] == 1)
ql[i] = find(ql[i]), qr[i] = find(qr[i]);
for (int T = 1; T <= n; T++) {
int op = qop[T], x = ql[T], y = qr[T], w;
if (op == 2) {
if (get(x) == get(y)) {
cout << "YES\n";
continue;
}
int w = co[s[x].l], F = get(y), L, R;
string ans = "NO\n";
for (int i = 0; i < q[w].size(); i++)
if (q[w][i].l < s[x].l && s[x].l < q[w][i].r) {
if (get(q[w][i].id) == F) {
ans = "YES\n";
break;
}
}
w = co[s[x].r];
for (int i = 0; i < q[w].size(); i++)
if (q[w][i].l < s[x].r && s[x].r < q[w][i].r) {
if (get(q[w][i].id) == F) {
ans = "YES\n";
break;
}
}
cout << ans;
continue;
}
s[++tot].l = x, s[tot].r = y, s[tot].id = tot;
w = co[x], A = emp;
for (vector<note>::iterator it = q[w].begin(); it != q[w].end(); it++) {
note i = *it;
if (i.l < x && x < i.r) {
merge(i.id, tot);
x = min(x, i.l), y = max(y, i.r);
} else
A.push_back(i);
}
q[w] = A, A = emp;
w = co[y];
for (vector<note>::iterator it = q[w].begin(); it != q[w].end(); it++) {
note i = *it;
if (i.l < y && y < i.r) {
merge(i.id, tot);
x = min(x, i.l), y = max(y, i.r);
} else
A.push_back(i);
}
q[w] = A, A = emp;
note nw;
nw.l = x, nw.r = y, nw.id = tot;
for (int i = co[x]; i <= co[y]; i++)
q[i].push_back(nw);
}
return 0;
}