POJ 2761 Feed the dogs 主席树(区间第k大)
来源:互联网 发布:sql语句采用的编写语言 编辑:程序博客网 时间:2024/06/01 22:12
题目:
http://poj.org/problem?id=2761
题意:
求区间第k大数,很裸
思路:
很裸的主席树
#include <iostream>#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int N = 100100;int root[N], lson[N*20], rson[N*20], sum[N*20];int a[N], b[N];int n, m, tot;void build(int l, int r, int &rt){ rt = ++tot; sum[rt] = 0; if(l == r) return; int mid = (l + r) >> 1; build(l, mid, lson[rt]); build(mid + 1, r, rson[rt]);}void update(int pre, int &rt, int l, int r, int val){ rt = ++tot; lson[rt] = lson[pre], rson[rt] = rson[pre], sum[rt] = sum[pre] + 1; if(l == r) return; int mid = (l + r) >> 1; if(val <= mid) update(lson[pre], lson[rt], l, mid, val); else update(rson[pre], rson[rt], mid + 1, r, val);}int query(int ss, int tt, int l, int r, int k){ if(l == r) return l; int mid = (l + r) >> 1; int tmp = sum[lson[tt]] - sum[lson[ss]]; if(k <= tmp) return query(lson[ss], lson[tt], l, mid, k); else return query(rson[ss], rson[tt], mid + 1, r, k - tmp);}int main(){ while(~ scanf("%d%d", &n, &m)) { for(int i = 1; i <= n; i++) scanf("%d", &a[i]), b[i] = a[i]; sort(b+1, b+1+n); int len = unique(b+1, b+1+n) - b - 1; tot = 0; build(1, len, root[0]); for(int i = 1; i <= n; i++) a[i] = lower_bound(b+1, b+1+len, a[i]) - b; for(int i = 1; i <= n; i++) update(root[i-1], root[i], 1, len, a[i]); int v, u, k; while(m--) { scanf("%d%d%d", &v, &u, &k); printf("%d\n", b[query(root[v-1], root[u], 1, len, k)]); } } return 0;}
0 0
- POJ 2761 Feed the dogs 主席树(区间第k大)
- POJ 2761 Feed the dogs 求区间第k大 划分树
- Splay树(区间第k小)——POJ 2761 Feed the dogs
- POJ 2761-Feed the dogs(划分树)求区间内第k小的数
- poj 2761 Feed the dogs 求区间第k大的数
- POJ 2761 Feed the dogs(主席树)
- POJ 2761 Feed the dogs(主席树)
- POJ 2761 Feed the dogs(Treap求第K小数)
- POJ 2761 Feed the dogs 树状数组求第k大的数
- POJ 题目2761 Feed the dogs(主席树||划分树)
- 【主席树】poj2104 K-th Number && poj2761 Feed the dogs
- poj 2104 主席树(区间第k大)
- 划分树 POJ 2761 feed the dogs
- poj 2761 Feed the dogs 划分树
- POJ 2761Feed the dogs 划分树
- POJ 2761 Feed the dogs(划分树)
- POJ-2761-Feed the dogs
- POJ 2761 Feed the dogs
- 调节一个多行Label之间的间隙
- Tomcat
- WAMPServer集成环境的安装和配置
- 我的liunx学习笔记之ldap网络帐号
- Unity3D 接Android SDK 遇到的坑
- POJ 2761 Feed the dogs 主席树(区间第k大)
- XML-DOM解析1
- LeetCode: Divide Two Integers
- iOS系统网络抓包方法
- Android中图片占用内存计算
- 我的liunx学习笔记之selinux的初级管理
- 用python配置houdini工具架和资产环境
- OC与Swift中的字符串
- How to be an expert