Kth number
Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total Submission(s): 13712 Accepted Submission(s): 4154
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]
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
Source
网上资料很多了。
#include <string.h>
#include <iostream>
#include <vector>
#include <cmath>
#include <stdio.h>
#include <algorithm>
using namespace std;
typedef long long LL;
const int maxn = 100000+4;
struct node
{
int l,r,sum;
}tree[maxn*20];
int root[maxn],a[maxn];
vector<int>v;
int T,n,m,x,y,k,tot;
int getid(int x) { return lower_bound(v.begin(),v.end(),x)-v.begin()+1;}
void insert(int &x,int y,int l,int r,int pos)
{
tree[++tot]=tree[y];
tree[tot].sum++;
x=tot;
if(l==r) return;
int mid = (l+r)>>1;
if(pos<=mid) insert(tree[x].l, tree[y].l, l, mid, pos);
if(mid+1<=pos) insert(tree[x].r,tree[y].r,mid+1,r,pos);
}
int query(int x,int y,int l,int r,int k)
{
if(l==r) return l;
int mid = (l+r)>>1;
if(tree[tree[x].l].sum-tree[tree[y].l].sum>=k){
return query(tree[x].l,tree[y].l,l,mid,k);
}
return query(tree[x].r, tree[y].r, mid+1, r, k-(tree[tree[x].l].sum-tree[tree[y].l].sum));
}
int main()
{
scanf("%d",&T);
while(T--){
tot=0;
v.clear();
memset(root,0,sizeof root);
memset(tree,0,sizeof tree);
scanf("%d %d",&n,&m);
for(int i=1;i<=n;i++) scanf("%d",&a[i]),v.push_back(a[i]);
sort(v.begin(),v.end());v.erase(unique(v.begin(),v.end()),v.end());
for(int i=1;i<=n;i++) insert(root[i],root[i-1],1,n,getid(a[i]));
for(int i=1;i<=m;i++){
scanf("%d%d%d",&x,&y,&k);
printf("%d\n",v[query(root[y],root[x-1],1,n,k)-1]);
}
}
}