主席树模版
来源:互联网 发布:淘宝号改后台有危害吗 编辑:程序博客网 时间:2024/06/09 20:15
参考wuyiqi
#include <cstdio>#include <cstring>#include <algorithm>using namespace std;const int maxn = 100010;int ls[maxn*20], rs[maxn*20], sum[maxn*20];int T[maxn], tot;void build(int l, int r, int& rt){rt = ++tot;sum[rt] = 0;if(l == r)return;int m = l+r>>1;build(l, m, ls[rt]);build(m+1, r, rs[rt]);}void update(int last, int p, int l, int r, int& rt){rt = ++tot;ls[rt] = ls[last];rs[rt] = rs[last];sum[rt] = sum[last]+1;if(l == r)return;int m = l+r>>1;if(p <= m)update(ls[last], p, l, m, ls[rt]);elseupdate(rs[last], p, m+1, r, rs[rt]);}int query(int x, int y, int l, int r, int k){if(l == r)return l;int m = l+r>>1;int cnt = sum[ls[y]]-sum[ls[x]];if(k <= cnt)return query(ls[x], ls[y], l, m, k);elsereturn query(rs[x], rs[y], m+1, r, k-cnt);}int num[maxn], a[maxn], n, q;void cal(){for(int i = 1; i <= n; i++){scanf("%d", &a[i]);num[i] = a[i];}tot = 0;sort(num+1, num+n+1);int cnt = unique(num+1, num+n+1)-(num+1);build(1, cnt, T[0]);for(int i = 1; i <= n; i++){a[i] = lower_bound(num+1, num+cnt+1, a[i])-num;}for(int i = 1; i <= n; i++)update(T[i-1], a[i], 1, cnt, T[i]);while(q--){int x, y, k;scanf("%d %d %d", &x, &y, &k);printf("%d\n", num[query(T[x-1], T[y], 1, cnt, k)]);}}int main(){int T;scanf("%d", &T);while(T--){scanf("%d %d", &n, &q);cal();}return 0;}
0 0
- 主席树模版
- 主席树模版
- hdoj 2665 主席树模版
- poj2104 K-th number 主席树模版
- POJ 2104&2761 主席树模版
- 主席树模版 记录历史操作
- 省选模版复习——主席树
- poj 2104 -- K-th Number (主席树模版题)
- 主席、树、主席树!
- 主席树
- 主席树
- 主席树
- 主席树
- 主席树
- 主席树
- 主席树
- 主席树
- 主席树
- java线程基础
- Mockplus 的主要应用人群
- ubuntu安装之后的最初几天一路杂记
- 浙大PAT水题简单分析 转
- 《史蒂夫·乔布斯传》读后感
- 主席树模版
- Android手机目前常见的分辨率
- Java自学总结之八序列化
- 多态 继承 复合
- Java异常处理
- PHP面向对象基础笔记一
- C语言学习笔记 1 C语言介绍
- android 实现listview高固定,且不滑动
- os x下的aptitude