Codeforces 840D [主席树]
来源:互联网 发布:淘宝代理货源平台 编辑:程序博客网 时间:2024/05/22 20:22
Description
Solution
跟PBS和gjghfd大神打cf的vp。
这个题跟以前一道区间众数很像,发现
用主席数维护。。
脑抽多打了一个东西。。
#include <cstdio>#include <cstring>#include <iostream>#include <algorithm>using namespace std;inline char get(void) { static char buf[100000], *S = buf, *T = buf; if (S == T) { T = (S = buf) + fread(buf, 1, 100000, stdin); if (S == T) return EOF; } return *S++;}inline void read(int &x) { static char c; x = 0; int sgn = 0; for (c = get(); c < '0' || c > '9'; c = get()) if (c == '-') sgn = 1; for (; c >= '0' && c <= '9'; c = get()) x = x * 10 + c - '0'; if (sgn) x = -x;}const int N = 303030;const int INF = 1 << 30;int rt[N], ls[N * 20], rs[N * 20], size[N * 20], a[N];int n, q, l, r, k, Tcnt, ans, p, num;inline int Min(int a, int b) { return a < b ? a : b;}inline void Insert(int &o, int l, int r, int pos) { ls[++Tcnt] = ls[o]; rs[Tcnt] = rs[o]; size[Tcnt] = size[o] + 1; o = Tcnt; if (l == r) return; int mid = (l + r) >> 1; if (pos <= mid) Insert(ls[o], l, mid, pos); else Insert(rs[o], mid + 1, r, pos);}inline int Kth(int x, int y, int k) { x = rt[x]; y = rt[y]; int l = 1, r = n, mid; while (l != r) { mid = (l + r) >> 1; if (size[ls[y]] - size[ls[x]] >= k) { x = ls[x]; y = ls[y]; r = mid; } else { k -= size[ls[y]] - size[ls[x]]; x = rs[x]; y = rs[y]; l = mid + 1; } } return l;}inline int Query(int x, int y, int p) { x = rt[x]; y = rt[y]; int l = 1, r = n, mid; while (l != r) { mid = (l + r) >> 1; if (p <= mid) { x = ls[x]; y = ls[y]; r = mid; } else { x = rs[x]; y = rs[y]; l = mid + 1; } } return size[y] - size[x];} // 多打的东西。。int main(void) { freopen("1.in", "r", stdin); read(n); read(q); for (int i = 1; i <= n; i++) { read(a[i]); rt[i] = rt[i - 1]; Insert(rt[i], 1, n, a[i]); } while (q--) { read(l); read(r); read(k); ans = INF; for (int i = 1; i <= k; i++) { p = (r - l + 1) * i / k; if (Query(l - 1, r, num = Kth(l - 1, r, p)) > (r - l + 1) / k) ans = Min(ans, num); } if (ans == INF) ans = -1; printf("%d\n", ans); } return 0;}
阅读全文
3 0
- Codeforces 840D [主席树]
- codeforces 840D 主席树
- [主席树] Codeforces 840D .Destiny
- Codeforces 840D Destiny 【主席树】
- codeforces 840D Destiny 主席树
- Codeforces Round #429 (Div. 1) D. Destiny(主席树)
- CodeForces 707D Persistent Bookcase (操作建树DFS|主席树+主席树)
- Codeforces Round #368-D. Persistent Bookcase-(离线建树+暴力xjb搞)/(主席树在线乱搞)
- Codeforces 707D Persistent Bookcase(离线dfs或在线主席树)
- spoj-D-query(主席树)
- SPOJ 3267. D-query 主席树
- SPOJ DQUERY D-query 主席树
- SPOJ D-query(主席树应用)
- 【SPOJ3267】D-query-主席树应用
- spoj DQUERY D-query SPOJ -(主席树)
- spoj D-Query(主席树)
- 【Codeforces 597C】 Subsequences - DP 主席树
- Codeforces Gym-101161E【LCA+主席树】
- 【常见面试题】网易游戏前端面试题集锦1
- 【每周论文】Time, Clocks, and Ordering of Events in a Distributed System
- [主席树] Codeforces 840D .Destiny
- 前来学习中
- hibernate查询语句总结
- Codeforces 840D [主席树]
- Linux Socket编程(不限Linux)
- hpu暑假训练 A 过山车【二分图】
- Unity3D 生成ipa测试版本(Xcode 8.3 微信登录分享)
- 正则表达式必知必会
- HDOJ 1162 Eddy's picture 最小生成树
- Python装饰器
- xampp 系统不支持:mysql 错误 以及Mysql密码重置
- 生信脚本练习(12)求fasta文件各序列长度并统计作图