$bzoj4569$
$st表+并查集$
$考虑暴力方法:我们每次将对应相等的位置用并查集连起来,那么最终答案就是9*10^{连通块个数-1}$
$很明显上面这个办法过不去,问题在于重复次数太多了,如果一个区间已经对应相等了就不用再次连,用st表优化这个过程$
$每次向st表一样递归连接,分成log层,每层维护\frac{n}{logn}个并查集,代表区间,那么我们加入记忆化的思想,如果对应区间已经联通就返回,这个就是用并查集完成,否则继续递归进行这个过程。其实这里就是运用了记忆化的思想$
$那么很明显每层穿起来所有区间需要区间个数-1次,那么一共有nlogn个区间,复杂度也就是nlogn了$
$所以看见这种题就要考虑用一些方式记忆化从而降低复杂度$
#include<bits/stdc++.h>
using namespace std;
const int N = 1e5 + , P = 1e9 + ;
int n, m;
int fa[][N], Log[N];
int find(int p, int x) {
return x == fa[p][x] ? x : fa[p][x] = find(p, fa[p][x]);
}
void merge(int k, int a, int b) {
int x = find(k, a), y = find(k, b);
if(x == y) {
return;
}
fa[k][x] = y;
if(!k) {
return;
}
merge(k - , a, b);
merge(k - , a + ( << k - ), b + ( << k - ));
}
int main() {
scanf("%d%d", &n, &m);
if(n == ) {
puts("");
return ;
}
for(int j = ; j <= ; ++j) {
for(int i = ; i + ( << j) - <= n; ++i) {
fa[j][i] = i;
}
}
for(int i = ; i <= n; ++i) {
Log[i] = Log[i >> ] + ;
}
while(m--) {
int a, b, c, d;
scanf("%d%d%d%d", &a, &b, &c, &d);
int t = Log[b - a + ];
merge(t, a, c);
merge(t, b - ( << t) + , d - ( << t) + );
}
long long ans = , f = ;
for(int i = ; i <= n; ++i) {
if(find(, i) == i) {
if(f) {
ans = ans * % P;
} else {
f = ;
}
}
}
printf("%lld\n", ans);
return ;
}
随机推荐
- leetcode 题解 || Remove Nth Node From End of List 问题
problem: Given a linked list, remove the nth node from the end of list and return its head. For exam ...
- Quality control
定义测试 为测试添加测试项 测试项目按类型分2种 Qualitative 定性,描述类的,比如颜色,是,否 Quantitative 定量,有明确的衡量 定性 ...
- 2013-06-09 12:03 如何在SQLServer中锁定某行记录
锁的概述 一. 为什么要引入锁 多个用户同时对数据库的并发操作时会带来以下数据不一致的问题: 丢失更新 A,B两个用户读同一数据并进行修改,其中一个用户的修改结果破坏了另一个修改的结果,比如订 ...
- nodejs while-loop
node-while-loop A while loop alternative for Nodejs based on promises. Install $ npm install --save ...
- mycat 连续分片 -> 按日期(天)分片
1,按日期(天)分片 按日期(天)分片:从開始日期算起,依照天数来分片 比如,从2016-01-01.每10天一个分片 注意事项:须要提前将分片规划好,建好.否则有可能日期超出实际配置分片数 2,加入 ...
- Android SQLite性能分析
作为Android预置的数据库模块,对SQLite的深入理解是很有必要的,能够从中找到一些优化的方向. 这里对SQLite的性能和内存进行了一些測试分析.对照了不同操作的运行性能和内存占用的情况,粗略 ...
- Leetcode_num2_Maximum Depth of Binary Tree
题目: Given a binary tree, find its maximum depth. The maximum depth is the number of nodes along the ...
- AndroidManifest具体解释之Application(有图更好懂)
可以包括的标签: <activity> <activity-alias> <service> <receiver> <provider> & ...
- 官方Caffe-windows 配置与示例运行
http://blog.csdn.net/guoyk1990/article/details/52909864 标签: caffewindows配置训练自己的数据 2016-10-24 13:34 1 ...
- 《Android Studio有用指南》7.1 AndroidStudio代码检查工具概述
本文节选自<Android Studio有用指南> 作者: 毕小朋 博客: http://blog.csdn.net/wirelessqa 眼下本书已上传到百度阅读, 在百度中搜索[Anr ...