您的位置:首页 > 其它

主席树模板题-poj2104

2017-08-09 13:45 465 查看
几个关于主席树理解的好博客:

http://www.cnblogs.com/Empress/p/4652449.html

http://www.cnblogs.com/zyf0163/p/4749042.html

http://blog.csdn.net/regina8023/article/details/41910615

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.

#include <cstdio>
#include <cstring>
#include <algorithm>
#include <map>
#include <vector>
#include <set>
#include <queue>
#include <iostream>
#include <cmath>
#include <cstdlib>
#include <string>
#define lson l,m
#define rson m+1,r
using namespace std;
const int maxn= 100009;

int L[maxn<<5],R[maxn<<5],sum[maxn<<5];
int tot;
int a[maxn],t[maxn],hush[maxn];

int build(int l,int r)
{
int rt=++tot;
sum[rt]=0;
if(l<r)
{
int m=l+r>>1;
L[rt]=build(lson);
R[rt]=build(rson);
}

return rt;
}

int update(int pre,int l,int r,int x)
{
int rt=++tot;
L[rt]=L[pre],R[rt]=R[pre],sum[rt]=sum[pre]+1;
if(l<r)
{
int m=l+r>>1;
if(x<=m)
L[rt]=update(L[pre],lson,x);
else R[rt]=update(R[pre],rson,x);
}
return rt;

}
int query(int u,int v,int l,int r,int k)
{
if(l>=r)
return l;
int m=l+r>>1;
int num=sum[L[v]]-sum[L[u]];
if(num>=k)
return query(L[u],L[v],lson,k);
else
return query(R[u],R[v],rson,k-num);
}
int main()
{
int n,m;
scanf("%d%d",&n,&m);
tot=0;
for(int i=1; i<=n; i++)
{
scanf("%d",&a[i]);
hush[i]=a[i];
}
sort(hush+1,hush+1+n);
int sz=unique(hush+1,hush+1+n)-hush-1;
t[0]=build(1,sz);
for(int i=1; i<=n; i++)
{
int x=lower_bound(hush+1,hush+1+sz,a[i])-hush;
t[i]=update(t[i-1],1,sz,x);
}
while(m--)
{
int l,r,k;
scanf("%d%d%d",&l,&r,&k);
int x=query(t[l-1],t[r],1,sz,k);
printf("%d\n",hush[x]);
}

return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: