$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 ;
}
随机推荐
- android客户端向服务器端验证登陆方法的实现1
遇到的问题:一个条件查询与多个条件查询,所用到的方式不一样 参考文档: http://www.oschina.net/question/1160609_133366 mybatis多条件查询的一 ...
- C#编译器选项(目标平台)
用vs编译C#项目的设置中,“属性-生成-目标平台”有anycpu,x86,x64等选项. anycpu(默认值)将编译程序集为使其在任意平台上都可以运行. 在任何可能的时候,应用程序作为 64 位进 ...
- Nginx简单了解
1.静态HTTP服务器 首先,Nginx是一个HTTP服务器,可以将服务器上的静态文件(如HTML.图片)通过HTTP协议展现给客户端. 配置: server { listen80; # 端口号 lo ...
- Process Autocad by python
一.处理AutoCad模块 -pyautocad 1.安装 pip install pyautocad 注:1.该操作会自动安装 comtypes模块,如果其他方式安装,请自行安装comtypes模块 ...
- uboot 命令
1.清除前一次的编译结果: make distclean 2.配置makefile:选择开发板 make smdk6410_config 3.编译 make 注意::编译时,打开的文档文件,目录都要 ...
- 在fedora25x86下编译opencv的Android版本的过程记录
准备材料: 1. 32位的Fedora25(不建议使用64位系统----64位系统下也是可以编译的,这里为了简单起见,考虑使用32位操作系统.事实上,本人在64位操作系统下也做了尝试,也完成了编译.) ...
- 为什么在 Java 中用 (low+high)>>>1 代替 (low+high)/2 或 (low+high)>>1 来计算平均值呢?好在哪里?
>>>与>>是位运算符,只对整型有效(不能用于浮点型).当是整型的时候(low+high)>>1可以代替(low+high)/2.>>>是无 ...
- Mysql 存储过程使用游标
-- 完整例子 CREATE PROCEDURE test BEGIN -- 定义参数 DECLARE _id INT; -- 定义游标 DECLARE no_more_products ...
- multiTarget within one project pods manage
step1:file->new->target create 1 targetstep2:change Podfile and update podstep3:check new targ ...
- 01-bilibilidemo配置
github-ijkplayer(bilibili)->cd 桌面位置 git clone https://github.com/Bilibili/ijkplayer.git ijkplayer ...