【POJ 2104 K-th Number】+ 主席树
来源:互联网 发布:php支付宝接口开发文档 编辑:程序博客网 时间:2024/05/15 13:29
K-th Number
Time Limit: 20000MS Memory Limit: 65536K
Total Submissions: 51458 Accepted: 17601
Case Time Limit: 2000MS
Description
You are working for Macrohard company in data structures department. After failing your previous task about key insertion you were asked to write a new data structure that would be able to return quickly k-th order statistics in the array segment.
That is, given an array a[1…n] of different integer numbers, your program must answer a series of questions Q(i, j, k) in the form: “What would be the k-th number in a[i…j] segment, if this segment was sorted?”
For example, consider the array a = (1, 5, 2, 6, 3, 7, 4). Let the question be Q(2, 5, 3). The segment a[2…5] is (5, 2, 6, 3). If we sort this segment, we get (2, 3, 5, 6), the third number is 5, and therefore the answer to the question is 5.
Input
The first line of the input file contains n — the size of the array, and m — the number of questions to answer (1 <= n <= 100 000, 1 <= m <= 5 000).
The second line contains n different integer numbers not exceeding 109 by their absolute values — the array for which the answers should be given.
The following m lines contain question descriptions, each description consists of three numbers: i, j, and k (1 <= i <= j <= n, 1 <= k <= j - i + 1) and represents the question Q(i, j, k).
Output
For each question output the answer to it — the k-th number in sorted a[i…j] segment.
Sample Input
7 3
1 5 2 6 3 7 4
2 5 3
4 4 1
1 7 3
Sample Output
5
6
3
Hint
This problem has huge input,so please use c-style input(scanf,printf),or you may got time limit exceed.
本来在学平面桶分割法~却各种T~最后发现需要用主席树~=.=~几乎裸主席树~一次建树一次查询~(划分树和函数式线段树也是能写的~)
AC代码:
#include<cstdio>#include<algorithm>using namespace std;const int MAXN = 1e5 + 10;struct node1{ int L,R,sum;}st[MAXN * 20];struct node2{ int x,idx; bool operator < (const node2 &px) const{ return x < px.x; }}a[MAXN];int root[MAXN],vis[MAXN],N,M,nl,fl,fr,fk;void ins(int &num,int &x,int L,int R){ st[++nl] = st[x]; x = nl; ++st[x].sum; if(L == R) return ; int mid = (L + R) >> 1; if(num <= mid) ins(num,st[x].L,L,mid); else ins(num,st[x].R,mid + 1,R);}int que(int i,int j,int k,int L,int R){ if(L == R) return L; int t = st[st[j].L].sum - st[st[i].L].sum; int mid = (L + R) >> 1; if(k <= t) return que(st[i].L,st[j].L,k,L,mid); else return que(st[i].R,st[j].R,k - t,mid + 1 , R);}int main(){ st[0].L = st[0].R = st[0].sum = root[0] = 0; scanf("%d %d",&N,&M); for(int i = 1 ; i <= N; i++){ scanf("%d",&a[i].x); a[i].idx = i; } sort(a + 1 , a + 1 + N); for(int i = 1 ; i <= N; i++) vis[a[i].idx] = i; nl = 0; for(int i = 1 ; i <= N; i++){ root[i] = root[i - 1]; ins(vis[i],root[i],1,N); } while(M--){ scanf("%d %d %d",&fl,&fr,&fk); printf("%d\n",a[que(root[fl - 1],root[fr],fk,1,N)].x); } return 0;}
- [POJ 2104]K-th Number 主席树
- poj 2104 K-th Number (主席树)
- poj 2104 K-th Number【主席树】
- POJ 2104 K-th Number [主席树]
- POJ 2104 K-th Number 主席树
- 主席树 poj 2104 K-th Number
- POJ 2104 K-th Number 主席树
- POJ 2104 K-th Number(主席树)
- 【POJ 2104 K-th Number】+ 主席树
- 【POJ】2104 K-th Number 主席树
- POJ-2104:K-th Number(主席树)
- 【主席树】POJ 2014 K-th Number
- poj 2104 K-th Number(划分树 or 主席树)
- POJ 2104 K-th Number (主席树 || 划分树)
- POJ 2104 K-th Number (划分树 / 主席树)
- poj 2104 K-th Number(划分树裸题&主席树)
- POJ 2104 K-th Number 静态主席树(裸
- POJ 2104 K-th Number 经典主席树
- 在Fedora24/25中轻松安装gcc 4.9
- Darwin中RTSP协议的实现
- iOS_为数字键盘增加完成按钮即ToolBar
- 枚举法、穷举法
- ERROR 1044 (42000): Access denied for user ''@'localhost' to database 'mysql'
- 【POJ 2104 K-th Number】+ 主席树
- sublime 汉化插件
- hadoop streaming参数配置
- TS文件用vlc播放,seek灰屏问题研究
- 后缀表达式转中缀表达式
- Java的文件操作
- 斗地主AI设计
- 年末BAT面试大全
- SpringMVC与Struts2区别与比较总结