Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 15072 Accepted Submission(s): 4518
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
解析:
这里有两个不错的大佬博客
点击打开链接
点击打开链接
#include
#include
#include
using namespace std;
const int MAXN = 1e5+5;
//const int MAXM = (1e5+5)*15;
typedef struct node
{
int x;
int id;
}node;
int a[MAXN];
int ran[MAXN];
int rt[MAXN*20],ls[MAXN*20],rs[MAXN*20],tot,sz;
int sum[MAXN*20];
bool cmp(node a,node b)
{
return a.x>1;
build(ls[root],l,mid);
build(rs[root],mid+1,r);
}
void update(int& root,int l,int r,int last,int x)
{
root=++tot;
ls[root]=ls[last];
rs[root]=rs[last];
sum[root]=sum[last]+1;
if(l==r) return ;
int mid=(l+r)>>1;
if(mid>=x) update(ls[root],l,mid,ls[last],x);
else update(rs[root],mid+1,r,rs[last],x);
}
int query(int ss,int tt,int l,int r,int k)
{
if(l==r) return l;
int cnt=sum[ls[tt]]-sum[ls[ss]];
int mid=(l+r)>>1;
if(k<=cnt) return query(ls[ss],ls[tt],l,mid,k);
else return query(rs[ss],rs[tt],mid+1,r,k-cnt);
}
int main()
{
int t;
int n,m;
scanf("%d",&t);
while(t--)
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++) scanf("%d",&a[i]),ran[i]=a[i];
tot=0;
sort(ran+1,ran+1+n);
sz=unique(ran+1,ran+1+n)-(ran+1);
build(rt[0],1,sz);
for(int i=1;i<=n;i++) a[i]=lower_bound(ran+1,ran+1+sz,a[i])-ran;
for(int i=1;i<=n;i++) update(rt[i],1,sz,rt[i-1],a[i]);
for(int i=0;i