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.
大致题意:给你一个长度为n的序列,标号从1到n,然后有m次询问,每次询问一个区间[l,r]中第k大的数是多少
思路:参考博客http://blog.csdn.net/regina8023/article/details/41910615
http://www.cnblogs.com/zinthos/p/3899565.html
代码如下
//#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=1e5+5;
int a[maxn],hash[maxn],rt[maxn];//保存多颗线段树的根节点
int n,m;
int tot=0;
struct node
{
int l,r;//分别指向左右子树
int size;//该节点对应区间数的个数
}CT[maxn*30];//chairtree
int build(int l,int r)
{
int root=++tot;
CT[root].size=0;
if(l==r) return root;
int m=(l+r)>>1;
CT[root].l=build(l,m);
CT[root].r=build(m+1,r);
return root;
}
int Update(int l,int r,int root,int x)
{
int now=++tot;
int tmp=now;
CT[tot].size=CT[root].size+1;
while(l<r)
{
int m=(l+r)>>1;
if(x<=m)
{
CT[now].l=++tot;
CT[now].r=CT[root].r;
root=CT[root].l;
now=tot;
r=m;
}
else
{
CT[now].l=CT[root].l;
CT[now].r=++tot;
root=CT[root].r;
now=tot;
l=m+1;
}
CT[now].size=CT[root].size+1;
}
return tmp;
}
int query(int l,int r,int lx,int rx,int k)
{
while(l<r)
{
int m=(l+r)>>1;
if(CT[CT[rx].l].size-CT[CT[lx].l].size>=k)
{
r=m;
lx=CT[lx].l;
rx=CT[rx].l;
}
else
{
l=m+1;
k-=CT[CT[rx].l].size-CT[CT[lx].l].size;
lx=CT[lx].r;
rx=CT[rx].r;
}
}
return l;
}
int HASH(int x,int size)//查询x对应离散化后的坐标
{
return lower_bound(hash,hash+size,x)-hash;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]),hash[i]=a[i];
sort(hash+1,hash+n+1);
int size=unique(hash+1,hash+n+1)-hash-1;
rt[0]=build(1,size);
for(int i=1;i<=n;i++)//建立n棵线段树
rt[i]=Update(1,size,rt[i-1],HASH(a[i],size));
while(m--)
{
int l,r,k;
scanf("%d%d%d",&l,&r,&k);
printf("%d\n",hash[query(1,size,rt[l-1],rt[r],k)]);
}
return 0;
}
递归写法
//#include<bits/stdc++.h>
#include<iostream>
#include<cstdio>
#include<algorithm>
using namespace std;
const int maxn=1e5+5;
int a[maxn],hash[maxn],rt[maxn];//保存多颗线段树的根节点
int n,m;
int tot=0;
struct node
{
int l,r;//分别指向左右子树
int size;//该节点对应区间数的个数
}CT[maxn*30];//chairtree
int build(int l,int r)
{
int root=++tot;
CT[root].size=0;
if(l==r) return root;
int m=(l+r)>>1;
CT[root].l=build(l,m);
CT[root].r=build(m+1,r);
return root;
}
int Update(int l,int r,int root,int x,int val)
{
int tmp=++tot;
CT[tot].size=CT[root].size+val;
int m=(l+r)>>1;
if(l==r) return tmp;
else if(x<=m)
{
CT[tmp].l=Update(l,m,CT[root].l,x,val);
CT[tmp].r=CT[root].r;
}
else
{
CT[tmp].l=CT[root].l;
CT[tmp].r=Update(m+1,r,CT[root].r,x,val);
}
return tmp;
}
int query(int l,int r,int lx,int rx,int k)
{
if(l==r) return l;
int m=(l+r)>>1;
int wei=CT[CT[rx].l].size-CT[CT[lx].l].size;
if(k<=wei)
return query(l,m,CT[lx].l,CT[rx].l,k);
else
return query(m+1,r,CT[lx].r,CT[rx].r,k-wei);
}
int HASH(int x,int size)//查询x对应离散化后的坐标
{
return lower_bound(hash,hash+size,x)-hash;
}
int main()
{
scanf("%d%d",&n,&m);
for(int i=1;i<=n;i++)
scanf("%d",&a[i]),hash[i]=a[i];
sort(hash+1,hash+n+1);
int size=unique(hash+1,hash+n+1)-hash-1;
rt[0]=build(1,size);
for(int i=1;i<=n;i++)//建立n棵线段树
rt[i]=Update(1,size,rt[i-1],HASH(a[i],size),1);
while(m--)
{
int l,r,k;
scanf("%d%d%d",&l,&r,&k);
printf("%d\n",hash[query(1,size,rt[l-1],rt[r],k)]);
}
return 0;
}