hdu 5726 GCD (线段树 + 区间预处理)
来源:互联网 发布:arraymap遍历数据 编辑:程序博客网 时间:2024/06/05 16:59
分析:
-用线段树来维护区间gcd。
-枚举右端点,从上一个右端点转移到以该点为右端点的所有区间的gcd值。
- gcd值是在
- 以该点为区间右端点的话,所有区间的gcd数不会超过
心得:
求满足条件的区间的个数,通常就是枚举端点,然后区间之间相互转移。
#include <iostream>#include <map>#include <algorithm>#include <cstdio>using namespace std;#define sa(x) scanf("%d", &(x))#define root 1, n, 1#define lson l, m, rt << 1#define rson m + 1, r, rt << 1 | 1struct jibancanyang{ int n, gcds[112345 << 2], A[112345], cnt; map<int, long long> ans, pre, aft; void push_up(int rt) { gcds[rt] = __gcd(gcds[rt << 1], gcds[rt << 1 | 1]); } void build(int l, int r, int rt) { if (l == r) { gcds[rt] = A[l - 1]; return; } int m = (l + r) / 2; build(lson), build(rson); push_up(rt); } int query(int L, int R, int l, int r, int rt) { if (L <= l && r <= R) return gcds[rt]; int m = (l + r) >> 1; int a = -1, b = -1; if (L <= m) a = query(L, R, lson); if (R > m) b = query(L, R, rson); if (a == -1) return b; if (b == -1) return a; return __gcd(a, b); } void space() { pre.clear(); for (int i = 0; i < n; i++) { aft.clear(); aft[A[i]]++; for (map<int, long long>::iterator it = pre.begin(); it != pre.end(); ++it) { aft[__gcd(it -> first, A[i])] += it -> second; } pre = aft; for (map<int, long long>::iterator it = pre.begin(); it != pre.end(); ++it) { ans[it -> first] += it -> second; } } } void run() { cnt = 0; build(1, n, 1); space(); int m; sa(m); while (m--) { int l, r; sa(l), sa(r); int g = query(l, r, 1, n, 1); printf("%d %lld\n", g, ans[g]); } }}ac;int main(){ // freopen("in.txt", "r", stdin); int T; sa(T); for (int cas = 1; cas <= T; cas++){ printf("Case #%d:\n", cas); //cout << "Case #" << cas << ":" << endl; sa(ac.n); ac.ans.clear(); for (int i = 0; i < ac.n; i++) { sa(ac.A[i]); } ac.run(); } return 0;}
0 0
- hdu 5726 GCD (线段树 + 区间预处理)
- HDU 5726 GCD(线段树+预处理)
- luogu1890 gcd区间(线段树/预处理)
- HDU 5726 GCD (线段树维护区间gcd)
- hdu 5726 GCD(线段树+预处理)
- HDU 5726 GCD (rmq+二分 or 线段树 维护区间gcd)
- hdu5726 GCD (线段树+区间gcd)
- hdu 5726(区间gcd)
- hdu 5381 The sum of gcd(线段树等差数列区间修改+单点查询)
- hdu 4630 No Pain No Game (区间gcd相关x线段树or树状数组)
- 等差区间 线段树+GCD
- hdu 5726 GCD (区间RMQ )
- HDU 5726 GCD(DP+线段树)
- hdu 5726 GCD (区间gcd-RMQ)
- 【二维线段树(二维区间GCD)】[NOI2012]魔幻棋盘
- 维护区间gcd(线段树 || ST表+二分)
- hdu 5381 The sum of gcd(线段树+gcd)
- hdu_5726_GCD(线段树维护区间+预处理)
- 使用Lock来实现生产者和消费者问题
- Win7 64位系统上配置使用32位的Eclipse
- 监控云—反盗版软件之利器!
- 访问webview屏蔽https证书
- UESTC 94 - Bracket Sequence(线段树+区间合并)
- hdu 5726 GCD (线段树 + 区间预处理)
- C++识别中文标点
- webdriver 实例 IE8报错:Unexpected error launching Internet Explorer
- MySQL5.6之Index Condition Pushdown(ICP,索引条件下推)
- 面试题28:字符串的排列
- iBatis与MyBatis区别
- Android应用内存泄露分析、改善经验总结
- Python quant
- JAVAFX开发出现的小bug