您的位置:首页 > 其它

【划分树】K-th Number POJ2104 POJ2761

2013-04-04 08:44 211 查看
K-th Number

Time Limit: 20000MS Memory Limit: 65536K
Case 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.
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;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: