K-th Number【POJ2104】——主席樹
Time Limit: 20000MS Memory Limit: 65536K
Case Time Limit: 2000MS
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
裸主席樹的題。
主席樹的主體是線段樹,準確的說,是很多棵線段樹,存的是一段數字區間出現次數(所以要先離散化可能出現的數字)。舉個例子,假設我每次都要求整個序列內的第 k 小,那麼對整個序列構造一個線段樹,然後線上段樹上不斷找第 k 小在當前數字區間的左半部分還是右半部分。這個操作和平衡樹的 Rank 操作一樣,只是這裡將離散的數字搞成了連續的數字。
先假設沒有修改操作:
對於每個字首 S1…i,儲存這樣一個線段樹 Ti,組成主席樹。這樣不是會 MLE 麼?最後再講。
注意,這個線段樹對一條線段,儲存的是這個數字區間的出現次數,所以是可以互相加減的!還有,由於每棵線段樹都要儲存同樣的數字,所以它們的大小、形態也都是一樣的!這實在是兩個非常好的性質,是平衡樹所不具備的。
對於詢問 (i,j),我只要拿出 Tj 和 Ti-1,對每個節點相減就可以了。說的通俗一點,詢問 i..j 區間中,一個數字區間的出現次數時,就是這些數字在 Tj 中出現的次數減去在 Ti-1 中出現的次數。
那麼有修改操作怎麼辦呢?
如果將詢問看成求一段序列的數字和,那麼上面那個相當於求出了字首和。加入修改操作後,就要用樹狀陣列等來維護字首和了。於是那個 “很好的性質” 又一次發揮了作用,由於主席樹可以互相加減,所以可以用樹狀陣列來套上它。做法和維護字首和長得基本一樣,不說了。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>
#include <queue>
#include <algorithm>
using namespace std;
const int Max = 1e5;
typedef struct node
{
int l,r,data;
}Tree;
Tree Tr[Max*20];
int top;
int Arr[Max+10],srt[Max+10];
int root[Max+10];
int BS(int *a,int L,int R,int goal)//二分查詢
{
int ans = -1;
while(L<=R)
{
int mid = (L + R)>>1;
if(a[mid] == goal) return mid;
if(a[mid]<goal) L = mid+1,ans = mid;
else R = mid-1;
}
return ans;
}
int Creat()
{
Tr[top].l = Tr[top].r = -1;
Tr[top].data = 0;
return top++;
}
void Init(int &a,int L,int R)//初始化線段樹
{
a = Creat();
if(L == R) return ;
int mid = (L + R)>>1;
Init(Tr[a].l,L,mid);
Init(Tr[a].r,mid+1,R);
}
void Build(int pre,int now,int L,int R,int goal)//建主席樹
{
if(L == R)
{
Tr[now].data = Tr[pre].data+1;
return ;
}
int mid = (L + R)>>1;
if(goal <= mid)
{
Tr[now].l = Creat();
Tr[now].r = Tr[pre].r;
Build(Tr[pre].l,Tr[now].l,L,mid,goal);
}
else
{
Tr[now].l = Tr[pre].l;
Tr[now].r = Creat();
Build(Tr[pre].r,Tr[now].r,mid+1,R,goal);
}
Tr[now].data = Tr[Tr[now].l].data + Tr[Tr[now].r].data;
}
int Query(int l,int r,int L,int R,int k)//查詢區間第k大。
{
if(L == R) return L;
int mid = (L+R) >>1;
if(k<=Tr[Tr[r].l].data-Tr[Tr[l].l].data)
{
return Query(Tr[l].l,Tr[r].l,L,mid,k);
}
else
{
return Query(Tr[l].r,Tr[r].r,mid+1,R,k -(Tr[Tr[r].l].data-Tr[Tr[l].l].data));
}
}
int main()
{
int n,m;
while(~scanf("%d %d",&n,&m))
{
for(int i = 1; i<= n;i++)
{
scanf("%d",&Arr[i]);
srt[i] = Arr[i];
}
sort(srt+1,srt+n+1);
int N = 1;
for(int i = 2;i<=n;i++)
{
if(srt[N] != srt[i]) srt[++N] = srt[i];
}
for(int i = 1;i<=n;i++)
{
Arr[i] = BS(srt,1,N,Arr[i]);
}
top = 0 ;
root[0] = -1;
Init(root[0],1,N);
for(int i = 1;i<=n;i++)
{
root[i] = Creat();
Build(root[i-1],root[i],1,N,Arr[i]);
}
int l,r,k;
while(m--)
{
scanf("%d %d %d",&l,&r,&k);
int ans = Query(root[l-1],root[r],1,N,k);
printf("%d\n",srt[ans]);
}
}
return 0;
}