前往小程序,Get更优阅读体验!
立即前往
首页
学习
活动
专区
工具
TVP
发布
社区首页 >专栏 >POJ 2104 K-th Number(主席树)

POJ 2104 K-th Number(主席树)

作者头像
attack
发布2018-04-12 14:21:15
6010
发布2018-04-12 14:21:15
举报

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

代码语言:javascript
复制
7 3
1 5 2 6 3 7 4
2 5 3
4 4 1
1 7 3

Sample Output

代码语言:javascript
复制
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

主席树的模板题。

代码语言:javascript
复制
 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<queue>
 6 #include<algorithm>
 7 using namespace std;
 8 const int MAXN=10000001;
 9 void read(int &n)
10 {
11     char c='+';int x=0;bool flag=0;
12     while(c<'0'||c>'9')
13     {c=getchar();if(c=='-')flag=1;}
14     while(c>='0'&&c<='9')
15     {x=x*10+c-48,c=getchar();}
16     flag==1?n=-x:n=x;
17 }
18 int n,m;
19 int a[MAXN];
20 int hash[MAXN];
21 int root[MAXN/2];
22 int now,tot;// 所有节点的总出现次数 
23 int ls[MAXN],rs[MAXN],cnt[MAXN];
24 void build(int &cur,int l,int r)
25 {
26     cur=tot++;// 总的节点数量 
27     cnt[cur]=0; 
28     if(l!=r)
29     {
30         int mid=(l+r)/2;
31          build(ls[cur],l,mid);
32          build(rs[cur],mid+1,r);    
33     }
34 }
35 void update(int pre,int pos,int &cur,int l,int r)
36 {
37     cur=tot++;
38     cnt[cur]=cnt[pre]+1;
39     ls[cur]=ls[pre];rs[cur]=rs[pre];
40     if(l==r)
41         return ;
42     int mid=(l+r)/2;
43     if(pos<=mid)
44         update(ls[pre],pos,ls[cur],l,mid);
45     else    
46         update(rs[pre],pos,rs[cur],mid+1,r);
47 }
48 int query(int lt,int rt,int l,int r,int k)
49 {
50     if(l==r)
51         return l;
52     int now=cnt[ls[rt]]-cnt[ls[lt]];
53     int mid=(l+r)/2;
54     if(k<=now)
55         return query(ls[lt],ls[rt],l,mid,k);
56     else 
57         return query(rs[lt],rs[rt],mid+1,r,k-now);
58 }
59 int main()
60 {
61     while(scanf("%d%d",&n,&m)==2)
62     {
63         for(int i=1;i<=n;i++)
64         {
65             read(a[i]);
66             hash[i]=a[i];
67         }
68         sort(hash+1,hash+n+1);
69         int size=unique(hash+1,hash+n+1)-hash-1;
70         for(int i=1;i<=n;i++)
71             a[i]=lower_bound(hash+1,hash+size+1,a[i])-hash;// 排序+离散化 
72         
73         tot=0;
74         build(root[0],1,size);
75         
76         for(int i=1;i<=n;i++)
77             update(root[i-1],a[i],root[i],1,size);// 建出所有的树 
78         
79         for(int i=1;i<=m;i++)
80         {
81             int x,y,z;
82             read(x);read(y);read(z);
83             printf("%d\n",hash[query(root[x-1],root[y],1,size,z)]);
84         }    // 查询 
85     }
86     
87     return 0;
88 }
本文参与 腾讯云自媒体分享计划,分享自作者个人站点/博客。
原始发表:2017-07-08 ,如有侵权请联系 cloudcommunity@tencent.com 删除

本文分享自 作者个人站点/博客 前往查看

如有侵权,请联系 cloudcommunity@tencent.com 删除。

本文参与 腾讯云自媒体分享计划  ,欢迎热爱写作的你一起参与!

评论
登录后参与评论
0 条评论
热度
最新
推荐阅读
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档