Establish ST table, maintain one for each layer and check the set.
Each information can be divided into two lengths of $2$ of the power of the interval equal information, equivalent to the St table two pairs of points of the merger.
Then recursively merge and exit once the discovery has been merged.
Because there will only be $o (n\log N) $ merges, the time complexity is $o (N\log n\alpha (n)) $.
#include <cstdio>int n,m,i,j,a,b,c,d,f[17][100010],v[100010],ans=9;int F (int i,int j) {return f[i][j]==j?j:f[i] [J]=f (I,f[i][j]);} void merge (int p,int x,int y) { if (F (p,x) ==f (p,y)) return; F[p][f[p][x]]=f[p][y]; if (!p) return; p--; Merge (P,x,y), merge (p,x+ (1<<p), y+ (1<<p));} inline void Read (int&a) {char c;while (!) ( ((C=getchar ()) >= ' 0 ') && (c<= ' 9 ')); a=c-' 0 '; while (((C=getchar ()) >= ' 0 ') && (c<= ' 9 ')) (a*= Ten) +=c-' 0 ';} int main () { read (n), read (m); if (n==1) return puts ("ten"), 0; for (i=0; (1<<i) <=n;i++) for (j=1;j+ (1<<i) -1<=n;j++) f[i][j]=j; while (m--) { read (a), read (b), read (c), read (d); I=31-__builtin_clz (b-a+1); Merge (I,a,c), merge (i,b-(1<<i) +1,d-(1<<i) +1); } For (V[f (0,1)]=1,i=2;i<=n;i++) if (!v[f (0,i)]) v[f[0][i]]=1,ans=10ll*ans%1000000007; Return printf ("%d", ans), 0;}
BZOJ4569: [Scoi2016] Meng Meng da