题意
此题为中文题面。
思路
辅助解释
Code
#include <bits/stdc++.h>
using namespace std;
#define _u_u_ ios::sync_with_stdio(false), cin.tie(nullptr)
#define cf int _o_o_;cin>>_o_o_;for (int Case = 1; Case <= _o_o_;Case++)
#define SZ(x) (int)(x.size())
inline void _A_A_();
signed main() {_A_A_();return 0;}
using ll = long long;
// #define int long long
int mod = 1e9 + 7;
const int maxn = 2e5 + 10;
const int N = 210, M = 5010;
const int inf = 0x3f3f3f3f;
int fa[maxn][20]; // 20个fa数组,20个并查集
int LOG2[maxn];
int find (int x,int k) { // 在第k层查找x的集合序号
if (x == fa[x][k]) return x;
return fa[x][k] = find(fa[x][k], k); // 路径压缩
}
inline void _A_A_() {
#ifdef LOCAL
freopen("in.in", "r", stdin);
#endif
_u_u_;
LOG2[0] = -1;
int n, m;
cin >> n >> m;
for (int i = 1;i <= n;i++) {
LOG2[i] = LOG2[i >> 1] + 1;
}
for (int i = 1;i <= n;i++) {
for (int k = 0;k <= LOG2[n];k++) {
fa[i][k] = i; // 对每一层的i初始化为i
}
}
for (int xxxx = 0; xxxx < m;xxxx++) {
int l1,r1,l2,r2;
cin >> l1 >> r1 >> l2 >> r2; // 一次大操作
int k = LOG2[r1 - l1 + 1];
int fa_l1 = find(l1, k);
int fa_l2 = find(l2, k);
fa[fa_l1][k] = fa_l2; // 在第k层的第一个小操作
int fa_r1 = find(r1 - (1 << k) + 1, k);
int fa_r2 = find(r2 - (1 << k) + 1, k);
fa[fa_r1][k] = fa_r2; // 在第k层的第二次小操作
}
for (int k = LOG2[n];k >= 1;k--) { // 将操作从高层向低层转移
for (int i = 1;i <= n;i++) {
int fa_i = find(i,k);
if (fa_i == i) continue;
int fa_d_l = find(i, k - 1);
int fa_d_r = find(fa_i, k - 1);
fa[fa_d_l][k - 1] = fa_d_r;
fa_d_l = find(i + (1 << (k - 1)), k - 1);
fa_d_r = find(fa_i + (1 << (k - 1)), k - 1);
fa[fa_d_l][k - 1] = fa_d_r;
}
}
ll ans = 1;
for (int i = 1;i <= n;i++) { // 在第0层得到答案
if(fa[i][0] == i) ans = (ans == 1 ? 9 : ans * 10 % mod);
}
cout << ans << "\n";
}
标签:洛谷,r1,int,fa,l2,l1,3295
From: https://www.cnblogs.com/FanWQ/p/16870774.html