【并查集分块】Codeforces 475D CGCDSSQ
来源:互联网 发布:2017年网络新项目 编辑:程序博客网 时间:2024/05/01 08:26
题目点这里
bin神说这题和他当年出的一道题差不多TAT
听了半天终于听懂了TAT。。。
利用gcd的递减性 从左往右把一段的gcd和最新的数相同的的放到一个并查集里去。。
可以证明段数是不会增加的 = =
#include <cstdio>#include <iostream>#include <map>using namespace std;const int Nmax = 1e5 + 5;int N, Q;int a[Nmax], f[Nmax], g[Nmax];map <int, long long> m;int find(int x) { return (x == f[x]) ? x : f[x] = find(f[x]); }inline void Union(int u, int v) { f[find(u)] = find(v); }inline int gcd(int a, int b){ if (!b) return a; for (int t = a % b; t; a = b, b = t, t = a % b); return b;}int main(){ ios :: sync_with_stdio(false); cin >> N; for (int i = 1; i <= N; ++ i) cin >> a[i]; for (int i = 1; i <= N + 1; ++ i) f[i] = i; for (int i = N; i; -- i) { int s = i, last = i; g[i] = a[i]; for (int j = find(i + 1); j <= N; j = find(j + 1)) { g[j] = gcd(a[i], g[j]); if (g[j] ^ g[last]) { m[g[last]] += last - s + 1; s = last + 1; } else Union(last, j); last = j; } m[g[N]] += N - s + 1; } for (cin >> Q; Q --; ) { int x; cin >> x; cout << m[x] << endl; } return 0;}
0 0
- 【并查集分块】Codeforces 475D CGCDSSQ
- 【codeforces】 475D CGCDSSQ
- Codeforces 475 D. CGCDSSQ
- CodeForces 475D CGCDSSQ
- CodeForces 475D CGCDSSQ
- CodeForces 475D CGCDSSQ RMQ
- Codeforces 475D CGCDSSQ 题解
- codeforces 475D CGCDSSQ ST表+二分
- 【CODEFORCES】 D. CGCDSSQ
- codeforces 475D CGCDSSQ (st表+数论)
- [Codeforces#475D]CGCDSSQ(st表+二分+数学相关)
- Codeforces 366D 贪心+并查集
- Codeforces 371D. Vessels【并查集】
- codeforces 366D 并查集
- codeforces 593D(并查集 + LCA)
- codeforces 691D 并查集
- [Codeforces 325 D. Reclamation]并查集
- Codeforces 766D 并查集
- Java之多线程
- ubuntu使用bitbucket(1)
- Android应用如何支持屏幕多尺寸多分辨率问题
- 从零开始学习Python(一) 之初识python
- 欢迎使用CSDN-markdown编辑器
- 【并查集分块】Codeforces 475D CGCDSSQ
- ORACLE9i_性能调优基础一(Diagnostic and Tuning Tools)
- 用无向带权图实现校园导航系统
- 从Apriori到MS-Apriori算法
- 计算机视觉领域的一些牛人博客,研究机构等的网站链接
- 记录apache-cxf-3.0.4编写web服务的过程(2)——集合
- 【LeetCode刷题记录】Single Number
- find命令详解
- 《STL源码剖析学习笔记》