K-th Number
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 10 9 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.
Source
Northeastern Europe 2004, Northern Subregion
|
主席树
什么是主席树 ?
所谓主席树呢,就是对原来的数列[1..n]的每一个前缀[1..i](1≤i≤n)建立一棵线段树,线段树的每一个节点存某个前缀[1..i]中属于区间[L..R]的数一共有多少个(比如根节点是[1..n],一共i个数,sum[root] = i;根节点的左儿子是[1..(L+R)/2],若不大于(L+R)/2的数有x个,那么sum[root.left] = x)。若要查找[i..j]中第k大数时,设某结点x,那么x.sum[j] - x.sum[i - 1]就是[i..j]中在结点x内的数字总数。而对每一个前缀都建一棵树,会MLE,观察到每个[1..i]和[1..i-1]只有一条路是不一样的,那么其他的结点只要用回前一棵树的结点即可,时空复杂度为O(nlogn)。
下面给出一张图表示主席树的建树过程
红色的节点表示每次update新建的节点
每次增加log2(n)个节点 => 对应于主席树数组一般开32倍
假设我们建好了这样的主席树,那么怎么查询区间第K小呢.
root[i]表示处理完前i个数之后所形成的线段树,即具有了前缀和的性质,那么root[r] - root[l-1]即表示处理的[l, r]区间喽。当要查询区间[1,3]的时候,我们只要将root[3] 和 root[0]节点相减即可得到。
如图:
设左节点中存的个数为cnt,当k<=cnt时,我们直接查询左儿子中第k小的数即可,如果k>cnt,我们只要去查右儿子中第k-cnt小的数即可,这边是一道很简单的线段树了。
代码:
//#include<bits/stdc++.h>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<iostream>
#include<vector>
using namespace std;
const int maxn = 1e5+10;
int arr[maxn];
vector<int> ma;
int root[maxn],tot;
inline void init() {
memset(root,0,sizeof(root));
tot = 0;
ma.clear();
}
int getid(int x) {
return lower_bound(ma.begin(),ma.end(),x) - ma.begin() + 1;
}
struct SegTree
{
int lson[maxn<<5],rson[maxn<<5],sum[maxn<<5];
void push_up(int rt) {
sum[rt] = sum[lson[rt]] + sum[rson[rt]];
}
void build(int l,int r,int& rt) {
rt = ++tot;
if(l == r) {
sum[rt] = 0;
return;
}
int mid = (l+r)>>1;
build(l,mid,lson[rt]);
build(mid+1,r,rson[rt]);
push_up(rt);
}
void update(int pos,int val,int l,int r,int ord,int &rt) {
rt = ++tot;
lson[rt] = lson[ord];
rson[rt] = rson[ord];
if(l == r) {
/// sum[rt] += val; 唔!这里有个Bug,还好比赛前发现了
sum[rt] = sum[ord] + val;
return ;
}
int mid = (l+r)>>1;
if(pos <= mid) update(pos,val,l,mid,lson[ord],lson[rt]);
else update(pos,val,mid+1,r,rson[ord],rson[rt]);
push_up(rt);
}
int query(int k,int l,int r,int lrt,int rrt) {
if(l == r) return l;
int mid = (l + r)>>1;
int cnt = sum[lson[rrt]] - sum[lson[lrt]];
if(k <= cnt) return query(k,l,mid,lson[lrt],lson[rrt]);
return query(k-cnt,mid+1,r,rson[lrt],rson[rrt]);
}
}seg;
int main()
{
int n,q;
while(~scanf("%d%d",&n,&q))
{
init();
seg.build(1,n,root[0]);
for(int i=1;i<=n;i++) scanf("%d",&arr[i]),ma.push_back(arr[i]);
sort(ma.begin(),ma.end());ma.erase(unique(ma.begin(),ma.end()),ma.end());
for(int i=1;i<=n;i++) {
seg.update(getid(arr[i]),1,1,n,root[i-1],root[i]);
}
while(q--)
{
int l,r,k;
scanf("%d%d%d",&l,&r,&k);
int ans = seg.query(k,1,n,root[l-1],root[r]);
printf("%d\n",ma[ans-1]);
}
}
return 0;
}