题目:CF566D Restructuring Company
区间并查集合并,技巧就是记录不在当前区间的下一个点nxt是哪一个。
1 #include<stdio.h>
2 #define it register int
3 #define il inline
4 const int N=1000005;
5 int n,m,fa[N],nxt[N],opt;
6 il int fd(it x){
7 return fa[x]==x?x:fa[x]=fd(fa[x]);
8 }
9 il void mer(it u,it v){
10 u=fd(u),v=fd(v);
11 if(u!=v) fa[v]=fa[u];
12 }
13 namespace io {
14 const int SIZE = (1 << 21) + 1;
15 char ibuf[SIZE], *iS, *iT, obuf[SIZE], *oS = obuf, *oT = oS + SIZE - 1, c, qu[55];
16 int f, qr;
17 #define gc() (iS == iT ? (iT = (iS = ibuf) + fread (ibuf, 1, SIZE, stdin), (iS == iT ? EOF : *iS ++)) : *iS ++)
18 inline void flush () {fwrite (obuf, 1, oS - obuf, stdout);oS = obuf;}
19 template <class I>
20 inline void fr (I &x) {
21 for (f = 1, c = gc(); c < '0' || c > '9'; c = gc()) if (c == '-') f = -1;
22 for (x = 0; c <= '9' && c >= '0'; c = gc()) x = x * 10 + (c & 15);
23 x *= f;
24 }
25 struct Flusher_ {~Flusher_() {flush();}} io_flusher_;
26 }
27 using io :: fr;
28 int main(){
29 freopen("a.in","r",stdin);
30 freopen("a.out","w",stdout);
31 fr(n),fr(m);it u,v;
32 for(it i=1;i<=n;++i) fa[i]=i,nxt[i]=i+1;
33 while(m--){
34 fr(opt),fr(u),fr(v);
35 if(opt==1) mer(u,v);
36 if(opt==2) for(it i=u+1,j;i<=v;i=j) j=nxt[i],mer(i-1,i),nxt[i]=nxt[v];
37 if(opt==3) fd(u)==fd(v)?puts("YES"):puts("NO");
38 }
39 return 0;
40 }