poj 2104 K-th Number(主席树)
2016-03-04 16:45
399 查看
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
Sample Output
Hint
This problem has huge input,so please use c-style input(scanf,printf),or you may got time limit exceed.
【思路】
主席树
戳这 click here
【代码】
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.
【思路】
主席树
戳这 click here
【代码】
#include<cstdio> #include<cstring> #include<iostream> #include<algorithm> #define FOR(a,b,c) for(int a=(b);a<=(c);a++) using namespace std; typedef long long ll; const int N = 1e5+10; const int M = 2*1e6+10; int n,m,tot,sz; int v ,hash ,root ; int sum[M],ls[M],rs[M]; ll read() { char c=getchar(); ll x=0,f=1; while(!isdigit(c)) { if(c=='-') f=-1; c=getchar(); } while(isdigit(c)) x=x*10+c-'0',c=getchar(); return x*f; } void update(int l,int r,int x,int& y,int num) { y=++sz; sum[y]=sum[x]+1; if(l==r) return ; ls[y]=ls[x],rs[y]=rs[x]; int mid=(l+r)>>1; if(num<=mid) update(l,mid,ls[x],ls[y],num); else update(mid+1,r,rs[x],rs[y],num); } int query(int x,int y,int rk) { int a=root[x-1],b=root[y]; int l=1,r=tot; while(l<r) { int mid=(l+r)>>1; int now=sum[ls[b]]-sum[ls[a]]; if(rk<=now) r=mid,a=ls[a],b=ls[b]; else l=mid+1,rk-=now,a=rs[a],b=rs[b]; } return hash[l]; } int main() { n=read(),m=read(); FOR(i,1,n) v[i]=read(),hash[i]=v[i]; sort(hash+1,hash+n+1); tot=unique(hash+1,hash+n+1)-hash-1; FOR(i,1,n) v[i]=lower_bound(hash+1,hash+tot+1,v[i])-hash; FOR(i,1,n) { update(1,tot,root[i-1],root[i],v[i]); } int x,y,z; FOR(i,1,m) { x=read(),y=read(),z=read(); printf("%d",query(x,y,z)); if(i!=m) puts(""); } return 0; }
相关文章推荐
- HDOJ-1789 Doing Homework again
- 大话设计模式-观察者模式
- 【Android学习】四种布局方式和属性
- MvvmLight框架使用入门(四)
- JQuery中window.onload的实现
- awk处理多个文件
- 应用内切换主题有哪些方案可以实现
- 基于Maven构建开发第一个Storm项目
- Android Context上下文解析
- js刷新页面
- iPhone中使用popover功能
- 基于Android系统的socket UDP协议通信
- 大话设计模式-建造者模式
- 【python】编程语言入门经典100例--20
- 安装npm
- 构建高并发高可用的电商平台架构实践
- nRF iOS项目(github开源项目)编译运行相关问题解决方案
- c# 照片轮播控件
- 自定义复杂bean注册到spring
- RecyclerView 控制分割线样式