您的位置:首页 > 其它

HDU 2665 Kth number

2018-02-11 16:30 155 查看

Kth number

Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 13726    Accepted Submission(s): 4160


Problem Description

Give you a sequence and ask you the kth big number of a inteval.  

 

Input

The first line is the number of the test cases. 
For each test case, the first line contain two integer n and m (n, m <= 100000), indicates the number of integers in the sequence and the number of the quaere. 
The second line contains n integers, describe the sequence. 
Each of following m lines contains three integers s, t, k. 
[s, t] indicates the interval and k indicates the kth big number in interval [s, t]  

 

Output

For each test case, output m lines. Each line contains the kth big number.  

 

Sample Input

1 10 1 1 4 2 3 5 6 7 8 9 0 1 3 2  

 

Sample Output

2  

分析

求一段区间内的第k大值。

主席树~

code

 

#include<cstdio>
#include<algorithm>

using namespace std;

const int N = 100010;
int sum[N*20],ls[N*20],rs[N*20];
int num
,t
,Root
,tot;

void build(int l,int r,int &rt) {
rt = ++tot;
sum[rt] = 0;
if (l==r) return ;
int m = (l + r) / 2;
build(l,m,ls[rt]);
build(m+1,r,rs[rt]);
}
void update(int l,int r,int &rt,int last,int p) {
rt = ++tot;
ls[rt] = ls[last];rs[rt] = rs[last];
sum[rt] = sum[last] + 1;
if (l==r) return ;
int m = (l + r) / 2;
if (p<=m) update(l,m,ls[rt],ls[last],p);
else update(m+1,r,rs[rt],rs[last],p);
}
int query(int l,int r,int L,int R,int k) {
if (l==r) return l;
int m = (l + r) / 2;
int cnt = sum[ls[R]] - sum[ls[L]];
if (k <= cnt) return query(l,m,ls[L],ls[R],k);
else return query(m+1,r,rs[L],rs[R],k-cnt);
}

int main () {
int T,n,m;
scanf("%d",&T);
while (T--) {
tot = 0;
scanf("%d%d",&n,&m);
for (int i=1; i<=n; ++i)
scanf("%d",&num[i]),t[i] = num[i];
sort(t+1,t+n+1);
int c = unique(t+1,t+n+1)-(t+1);
build(1,c,Root[0]);
for (int i=1; i<=n; ++i) {
int x = lower_bound(t+1,t+c+1,num[i])-t;
update(1,c,Root[i],Root[i-1],x);
}
while (m--) {
int l,r,k;
scanf("%d%d%d",&l,&r,&k);
printf("%d\n",t[query(1,c,Root[l-1],Root[r],k)]);
}
}
return 0;
}
View Code

 

内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: