POJ 2104 K-th Number(主席树)
2018-06-17 22:15:15来源:未知 阅读 ()
Time Limit: 20000MS | Memory Limit: 65536K | |
Total Submissions: 57427 | Accepted: 19856 | |
Case Time Limit: 2000MS |
Description
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 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
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
Source
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 }
标签:
版权申明:本站文章部分自网络,如有侵权,请联系:west999com@outlook.com
特别注意:本站所有转载文章言论不代表本站观点,本站所提供的摄影照片,插画,设计作品,如需使用,请与原作者联系,版权归原作者所有
上一篇:1291 火车线路
下一篇:C++之STL总结精华笔记
- POJ-3278 2020-04-01
- Asteroids!_poj2225 2020-02-09
- poj-1753题题解思路 2020-01-26
- POJ1852 2019-11-11
- POJ2431 优先队列+贪心 - biaobiao88 2019-11-03
IDC资讯: 主机资讯 注册资讯 托管资讯 vps资讯 网站建设
网站运营: 建站经验 策划盈利 搜索优化 网站推广 免费资源
网络编程: Asp.Net编程 Asp编程 Php编程 Xml编程 Access Mssql Mysql 其它
服务器技术: Web服务器 Ftp服务器 Mail服务器 Dns服务器 安全防护
软件技巧: 其它软件 Word Excel Powerpoint Ghost Vista QQ空间 QQ FlashGet 迅雷
网页制作: FrontPages Dreamweaver Javascript css photoshop fireworks Flash