【划分树】K-th Number POJ2104 POJ2761
2013-04-04 08:44
211 查看
K-th Number
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
Sample Output
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
很裸的划分树
第一次写,蛋疼。。。。。。
具体就不说了,马上去写学习报告
代码中有很多+1-1在一起,大多是为了好理解就没有约掉,如果不习惯就约掉了在看吧
POJ2761换了一个背景,其他一模一样
测评情况(POJ)
Time Limit: 20000MS | Memory Limit: 65536K | |
Case Time Limit: 2000MS |
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
很裸的划分树
第一次写,蛋疼。。。。。。
具体就不说了,马上去写学习报告
代码中有很多+1-1在一起,大多是为了好理解就没有约掉,如果不习惯就约掉了在看吧
POJ2761换了一个背景,其他一模一样
测评情况(POJ)
/*http://blog.csdn.net/jiangzh7 By Jiangzh*/ #include<cstdio> #include<algorithm> using namespace std; const int N=100000+10; int n,m; int sorted ; struct Parti_Tree{int val,left;}val[20] ; void build_tree(int d,int l,int r) { if(l==r) return; int m=(l+r)>>1; int same=m-l+1; int lcnt=l,rcnt=m+1; for(int i=l;i<=r;i++) if(val[d][i].val<sorted[m]) same--; for(int i=l;i<=r;i++) { int flag=0; if((val[d][i].val<sorted[m])||(val[d][i].val==sorted[m]&&same>0)) { flag=1;//表示当前数分到了左子树 val[d+1][lcnt++]=val[d][i]; if(val[d][i].val==sorted[m]) same--; } else{ val[d+1][rcnt++]=val[d][i]; } val[d][i].left=val[d][i-1].left+flag; } build_tree(d+1,l,m); build_tree(d+1,m+1,r); } int query(int d,int l,int r,int x,int y,int k) { if(l==r) return val[d][l].val; int m=(l+r)>>1; int lx=val[d][x-1].left-val[d][l-1].left;//[l,x-1]中进入左子树的有lx个 int ly=val[d][y].left-val[d][x-1].left;//[x,y]中进入左子树的有ly个 int rx=(x-1)-l+1-lx;//[l,x-1]中进入右子树的有rx个 int ry=y-x+1-ly;//[x,y]中进入右子树的有ry个 if(ly>=k) return query(d+1,l,m,l-1+lx+1,l-1+lx+ly,k); else return query(d+1,m+1,r,m+1-1+rx+1,m+1-1+rx+ry,k-ly); } void work() { int x,y,k; scanf("%d%d%d",&x,&y,&k); printf("%d\n",query(0,1,n,x,y,k)); } int main() { freopen("poj2104.in","r",stdin); freopen("poj2104.out","w",stdout); scanf("%d%d",&n,&m); for(int i=1;i<=n;i++) { scanf("%d",&sorted[i]); val[0][i].val=sorted[i]; } sort(sorted+1,sorted+1+n); build_tree(0,1,n); while(m--) work(); return 0; }
相关文章推荐
- POJ2104 POJ2761 K-th Number, 划分树
- poj2761_划分树
- 划分树-POJ2104,POJ2761,HDU2665,HDU3743
- 图论, 1.各种方案的最短路径,最小生成树,拓扑排序, 2.隐式图的搜索,N-皇后问题,数独,马踏棋盘,中文划分,回文划分.
- 领土划分
- [从头读历史] 第296节 时间简史 层次的划分和度量
- Spark Stage 的划分
- 百练 04 简单的整数划分问题
- 计算几何 ( 判断线段相交 )——土地划分( FZU 1015 )
- 关于JVM内存区域划分
- “另类”计算IP地址和子网划分
- 特老的文章:三层应该怎么划分。不知大家还有用否
- 【动态规划】数的划分
- IP协议与地址划分
- Java虚拟机-内存区域划分
- 一段把年按星期划分并上传到数据库的代码
- 算法训练 数的划分
- 买酱油与软件工程阶段划分
- Ignatius and the Princess III(n的m划分)
- 信息部门人员角色划分及任职资格