K-th Number
Time Limit: 20000MS Memory Limit: 65536K
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.
Source
Northeastern Europe 2004, Northern Subregion
题目大意
给n个数的序列,m次查询[L,R]内的第k小
题解
- 主席树裸题
- 贴一个比较个人感觉比较清楚的http://im.librazy.org/article/837/note-chairtree-via-functional-segtree/
- 如果还是不懂可以看看CLJ的论文,或者范浩强的WC讲稿
var
w:array[0..2000000,1..3]of longint;
x:array[0..100000,1..2]of longint;
root,y:array[0..100000]of longint;
i,j,k:longint;
n,m:longint;
len,ans,a,b:longint;
procedure sort(l,r: longint);
var i,j,a,z: longint;
begin
i:=l; j:=r; a:=x[(l+r) div 2,1];
repeat
while x[i,1]<a do inc(i);
while a<x[j,1] do dec(j);
if not(i>j) then
begin
z:=x[i,1]; x[i,1]:=x[j,1]; x[j,1]:=z;
z:=x[i,2]; x[i,2]:=x[j,2]; x[j,2]:=z;
inc(i); dec(j);
end;
until i>j;
if l<j then sort(l,j);
if i<r then sort(i,r);
end;
procedure init(l,r,x:longint;var y:longint;v:longint);
var mid:longint;
begin
y:=len+1; inc(len); w[y,1]:=w[x,1]+1;
if l=r then exit;
w[y,2]:=w[x,2]; w[y,3]:=w[x,3];
mid:=(l+r)>>1;
if (v<=mid) then begin w[y,2]:=len+1; init(l,mid,w[x,2],w[y,2],v) end
else begin w[y,3]:=len+1; init(mid+1,r,w[x,3],w[y,3],v); end;
end;
function query(a,b,l,r,k:longint):longint;
var mid,t:longint;
begin
if l=r then exit(l);
t:=w[w[b,2],1]-w[w[a,2],1];
mid:=(l+r)>>1;
if t>=k then exit(query(w[a,2],w[b,2],l,mid,k))
else exit(query(w[a,3],w[b,3],mid+1,r,k-t));
end;
begin
readln(n,m);
for i:=1 to n do
begin
read(x[i,1]);
x[i,2]:=i;
end;
sort(1,n); {x[i,1]}
for i:=1 to n do
y[x[i,2]]:=i;
len:=0;
for i:=1 to n do
init(1,n,root[i-1],root[i],y[i]);
for i:=1 to m do
begin
readln(a,b,k);
ans:=query(root[a-1],root[b],1,n,k);
writeln(x[ans,1]);
end;
end.