4569: [Scoi2016]萌萌哒
分析:
每次给出的两个区间长度是一样的,对应位置的数字也是一样的,那么可以将两两对应的数字用并查集合并,设最后有$cnt$个不同的集合,答案就是$9\times 10 ^{cnt-1}$,第一个数不能是0。
暴力合并太慢了,考虑优化。对于一段区间,用倍增的思想分成log段,分别合并log段,最后的下放一下标记即可。类似线段树的懒标记。
代码:
#include<cstdio> #include<algorithm> #include<cstring> #include<iostream> #include<cmath> #include<cctype> #include<set> #include<queue> #include<vector> #include<map> using namespace std; typedef long long LL; inline int read() { int x=0,f=1;char ch=getchar();for(;!isdigit(ch);ch=getchar())if(ch==‘-‘)f=-1; for(;isdigit(ch);ch=getchar())x=x*10+ch-‘0‘;return x*f; } const int mod = 1e9 + 7, N = 200005; int f[20][N], Log[N]; int find(int x,int y) { return f[y][x] == x ? x : f[y][x] = find(f[y][x], y); } void Union(int a,int b,int c) { // 合并两个段 [a,a+(1<<c)-1] 与 [b,b+(1<<c)-1] if (find(a, c) != find(b, c)) f[c][f[c][a]] = f[c][b]; } int main() { int n = read(), m = read(), cnt = 0; for (int i = 2; i <= n; ++i) Log[i] = Log[i >> 1] + 1; for (int j = 0; j <= Log[n]; ++j) for (int i = 1; i + (1 << j) - 1 <= n; ++i) f[j][i] = i; for (int i = 1; i <= m; ++i) { int a = read(), b = read(), c = read(), d = read(); for (int j = Log[b - a + 1]; ~j; --j) if (a + (1 << j) - 1 <= b) Union(a, c, j), a += (1 << j), c += (1 << j); } for (int j = Log[n]; j; --j) { // 下放标记 for (int i = 1; i + (1 << j) - 1 <= n; ++i) { Union(i, find(i, j), j - 1); Union(i + (1 << (j - 1)), find(i, j) + (1 << (j - 1)), j - 1); } } for (int i = 1; i <= n; ++i) if (find(i, 0) == i) cnt ++; LL ans = 9; for (int i = 1; i < cnt; ++i) ans = ans * 10 % mod; cout << ans; return 0; }
原文地址:https://www.cnblogs.com/mjtcn/p/10493838.html
时间: 2024-11-09 01:10:58