poj 2104 K-th Number(划分树)
来源:互联网 发布:绿色傲剑金蛇剑法数据 编辑:程序博客网 时间:2024/06/05 02:48
K-th Number
Time Limit: 20000MS Memory Limit: 65536KTotal Submissions: 32249 Accepted: 10022Case 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.
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).
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 31 5 2 6 3 7 42 5 34 4 11 7 3
Sample Output
563
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
划分树资料:http://www.notonlysuccess.com/index.php/divide-tree/
http://www.cnblogs.com/pony1993/archive/2012/07/17/2594544.html
http://blog.csdn.net/frog1902/article/details/9616459
#define lson d+1,l,m#define rson d+1,m+1,r#include <cstdio>#include <algorithm>const long M=110000;long a[M],tree[30][M],toleft[30][M];long n,m;void build(long d,long l,long r){long m=(l+r)>>1,lsame=(m-l+1);long lpos=l,rpos=m+1;for (long i=l;i<=m;++i)if (a[i]<a[m]) lsame--;for (long i=l;i<=r;++i){if (i==l) toleft[d][i]=0;else toleft[d][i]=toleft[d][i-1];if (tree[d][i]==a[m]){if (lsame){lsame--;toleft[d][i]++;tree[d+1][lpos++]=tree[d][i];}else tree[d+1][rpos++]=tree[d][i];}else if (tree[d][i]<a[m]){toleft[d][i]++;tree[d+1][lpos++]=tree[d][i];}else tree[d+1][rpos++]=tree[d][i];}if (l==r) return;build(lson); build(rson);}long query(long L,long R,long K,long d,long l,long r){if (l==r) return tree[d][l];long m=(l+r)>>1;long s,ss,rs,rss;long newl,newr;if (L==l){s=0; ss=toleft[d][R];}else{s=toleft[d][L-1];ss=toleft[d][R]-s;}if (K<=ss) return query(l+s,l+s+ss-1,K,lson);else{rs=L-l-s; rss=R-L+1-ss;newl=m+1+rs; newr=m+rs+rss;return query(newl,newr,K-ss,rson);}}int main(){while (~scanf("%d%d",&n,&m)){for (long i=1;i<=n;++i){scanf("%d",&a[i]);tree[0][i]=a[i];}std::sort(a+1,a+1+n);build(0,1,n);for (long i=1;i<=m;++i){long l,r,k;scanf("%d%d%d",&l,&r,&k);printf("%d\n",query(l,r,k,0,1,n));}}return 0;}
- POJ 2104 K-th Number 划分树
- poj 2104 K-th Number--划分树
- poj 2104 K-th Number(划分树)
- POJ 2104 K-th Number 划分树
- [POJ 2104] K-th Number [划分树]
- poj 2104 K-th Number(划分树)
- POJ 2104 K-th Number 划分树
- POJ 2104 K-th Number 划分树
- K-th Number - POJ 2104 划分树
- POJ 2104 K-th Number【划分树】
- POJ 2104 K-th Number (划分树)
- POJ 2104K-th Number 划分树
- poj 2104K-th Number(划分树)
- POJ 2104 K-th Number 划分树
- poj 2104 K-th Number(划分树)
- POJ 2104 K-th Number (主席树 || 划分树)
- POJ 2104 K-th Number (划分树 / 主席树)
- POJ-2104(K-th Number 划分树)
- 使用ant打jar包
- java 简单的文件上传下载服务器模型(IO + 多线程)
- VC工具Depends
- Sift相关资料
- 2013暑期多校联合训练\第三场\Problem H
- poj 2104 K-th Number(划分树)
- RationalRose2003--WIN7破解
- UVA 1424 - Salesmen (dp)
- POJ-1724-ROADS
- Serializable 和 Parcelable 区别
- http Content-Type一览表
- hdu(1194)
- 提高网页加载速度最简单的5种方式
- hdu 4612 (双联通+树形DP)