$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 ;
}

随机推荐

  1. c#列表操作

    Enumerable[从元数据]   //        // 摘要:         //     从序列的开头返回指定数量的连续元素.        //        // 参数:        ...

  2. ftp的实现

    ftp.h #define BUFSIZE 512#define CMDSIZE 64#define ARGSIZE 64#define PASSIVE_ON 0x1 struct ftpcmd{ c ...

  3. 修改mysql数据库 密码

    将密码改成123456 update mysql.user set authentication_string=password('123456') where user='root' and Hos ...

  4. H5 手机横竖屏判读

    $.fn.screenCheck = function() { var pDiv = $('<div></div>'); pDiv.addClass("screenC ...

  5. ZOJ 1516 Uncle Tom&#39;s Inherited Land(二分匹配 最大匹配 匈牙利啊)

    题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemId=516 Your old uncle Tom inherited a p ...

  6. 800元组装一台3D打印机全教程流程-零件清单

    继前面的教程800元组装一台3D打印机全教程流程 k800是一台根据kosselmini改进的低成本3d打印机,通过改变设计,降低了成本,但损失较少性能,取得性价比. 主要改动是:底部支架改为-> ...

  7. Android中通过GPS或NetWork获取当前位置的经纬度

    今天在Android项目中要实现一个通过GPS或NetWork来获取当前移动终端设备的经纬度功能.要实现该功能要用到Android Framework 中的 LocationManager 类.下面我 ...

  8. Presenting view controllers on detached view controllers is discouraged

    本文转载至 http://www.xuebuyuan.com/2117943.html Presenting view controllers on detached view controllers ...

  9. httpclient4 模拟访问网页 模拟登录 简单例子

    JAVA后台模拟登录一个网站,获得一定权限后进一步操作. 所用的工具: Apache HttpComponents client 4.3版本 以下为代码: import org.apache.http ...

  10. Please read "Security" section of the manual to find out how to run mysqld as root!

    [root@test ~]# /usr/local/mysql/bin/mysqld2018-08-05T08:29:05.143142Z 0 [Warning] [MY-011070] [Serve ...