BZOJ 5005 & JZOI 3959 鸡腿の乒乓
SOL: 我们发现两个有交且不相互覆盖的区间可以互相到达,我们用并查集把这样的区间缩成一个大区间,用线段树维护即可。
#pragma GCC optimize("-O2") #include<bits/stdc++.h> #define pb push_back #define Mid (l+r>>1) #define ls now<<1,l,Mid #define rs now<<1|1,Mid+1,r #define N 300015 using namespace std; int A[N],l[N],r[N],L[N],R[N],f[N],oz,n,op[N]; struct qr{vector<int> c;}tr[N*2]; int tot=0; #define sight(x) ('0'<=x&&x<='9') inline void read(int &x){ static char c; static int b; for (b=1,c=getchar();!sight(c);c=getchar())if (c=='-') b=-1; for (x=0;sight(c);c=getchar())x=x*10+c-48; x*=b; } void write(int x){if (x<10) {putchar('0'+x); return;} write(x/10); putchar('0'+x%10);} inline void writeln(int x){ if (x<0) putchar('-'),x*=-1; write(x); putchar('\n'); } inline void writel(int x){ if (x<0) putchar('-'),x*=-1; write(x); putchar(' '); } void Li() { sort(A+1,A+A[0]+1); for (int i=1;i<=n;i++){ if (op[i]==2) continue; l[i]=upper_bound(A+1,A+A[0]+1,l[i])-A, r[i]=upper_bound(A+1,A+A[0]+1,r[i])-A; } } int gf(int x){ return x^f[x]?f[x]=gf(f[x]):x; } void Del(int now,int l,int r,int x){ if (tr[now].c.size()) { int id; for (int i=0;i<tr[now].c.size();i++) { id=tr[now].c[i]; f[gf(id)]=oz; L[oz]=min(L[oz],L[id]); R[oz]=max(R[oz],R[id]); } tr[now].c.clear(); } if (l==r) return; if (x<=Mid) Del(ls,x); else Del(rs,x); } void up(int now,int l,int r,int L,int R){ if (L<=l&&r<=R) { tr[now].c.pb(oz); return; } if (L<=Mid) up(ls,L,R); if (R> Mid) up(rs,L,R); } signed main () { read(n); for (int i=1;i<=n;i++) { read(op[i]),read(l[i]); read(r[i]); if (op[i]==1) { A[++A[0]]=l[i],A[++A[0]]=r[i]; } } Li(); for (int t=1;t<=n;t++) { if (op[t]==1) { L[++oz]=l[t],R[oz]=r[t]; f[oz]=oz; Del(1,1,A[0],l[t]); Del(1,1,A[0],r[t]); up(1,1,A[0],L[oz]+1,R[oz]-1); } else { int y=gf(l[t]),x=gf(r[t]); if (x==y||(L[x]<L[y]&&L[y]<R[x])||(L[x]<R[y]&&R[y]<R[x])) puts("YES"); else puts("NO"); } } return 0; }