POJ 2104 . K-th Number
来源:互联网 发布:健身 年轻 知乎 编辑:程序博客网 时间:2024/06/06 03:34
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.
Solution
这是一道 静态区间中求第K大值 的经典问题。
直接上 主席树 即可!!!
Code
#include<cstdio>#include<algorithm>using namespace std;const int N=100001;struct data{ int l,r,ls,rs,v;}f[N*20];struct value{ int v,id;}a[N];int tot,pos;int rank[N],rt[N];inline int read(){ int X=0,w=1; char ch=0; while(ch<'0' || ch>'9') {if(ch=='-') w=-1;ch=getchar();} while(ch>='0' && ch<='9') X=(X<<3)+(X<<1)+ch-'0',ch=getchar(); return X*w;}inline bool cmp(value x,value y){ return x.v<y.v;}inline void build(int &x,int l,int r){ f[x=++tot].l=l,f[x].r=r; if(l==r) return; int mid=(l+r)>>1; build(f[x].ls,l,mid); build(f[x].rs,mid+1,r);}inline void insert(int pre,int &x){ f[x=++tot]=f[pre],f[x].v++; if(f[x].l==f[x].r) return; int mid=(f[x].l+f[x].r)>>1; if(rank[pos]<=mid) insert(f[pre].ls,f[x].ls); else insert(f[pre].rs,f[x].rs);}inline int query(int pre,int x,int k){ if(f[x].l==f[x].r) return a[f[x].l].v; int t=f[f[x].ls].v-f[f[pre].ls].v; if(k<=t) return query(f[pre].ls,f[x].ls,k); return query(f[pre].rs,f[x].rs,k-t);}int main(){ int n=read(),m=read(); for(int i=1;i<=n;i++) a[a[i].id=i].v=read(); sort(a+1,a+1+n,cmp); build(rt[0],1,n); for(int i=1;i<=n;i++) rank[a[i].id]=i; for(int i=1;i<=n;i++) insert(rt[i-1],rt[pos=i]); while(m--) { int l=read(),r=read(),k=read(); printf("%d\n",query(rt[l-1],rt[r],k)); } 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-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
- 【转】JVM调优总结(一)-一些概念
- Linux中的进程间通信
- 解决R语言xlsx安装遇到问题
- 我大声地告诉您,一卡通的终极目标就是无卡通
- 《权限系列shiro+cas》----配置各个应用程序
- POJ 2104 . K-th Number
- 使用导航控件
- hadoop 2.2.X 配置参数说明:mapred-site.xml
- POJ1155-树形DP&&背包
- 【转】JVM调优总结(二)-一些概念
- Apache Kylin的入门安装
- 初学Python之Flask开发API接口
- 用户注册和登录实例
- 装饰者模式