您的位置:首页 > 其它

hdu 2665 Kth number 划分树

2012-10-23 12:56 323 查看

Kth number

Time Limit: 15000/5000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 2354    Accepted Submission(s): 800

[align=left]Problem Description[/align]
Give you a sequence and ask you the kth big number of a inteval.


[align=left]Input[/align]
The first line is the number of the test cases.
For
each test case, the first line contain two integer n and m (n, m <=
100000), indicates the number of integers in the sequence and the number
of the quaere.
The second line contains n integers, describe the sequence.
Each of following m lines contains three integers s, t, k.
[s, t] indicates the interval and k indicates the kth big number in interval [s, t]


[align=left]Output[/align]
For each test case, output m lines. Each line contains the kth big number.


[align=left]Sample Input[/align]

1
10 1
1 4 2 3 5 6 7 8 9 0
1 3 2


[align=left]Sample Output[/align]

2


[align=left]Source[/align]
HDU男生专场公开赛——赶在女生之前先过节(From WHU)


[align=left]Recommend[/align]
zty
//上次写的POJ kth number是每个数不同的
//而这次数可能重复,所以要修改下策略,感觉这个更好更强大

#include <iostream>
#include <stdio.h>
#include <math.h>
#include <string.h>
#include <algorithm>
using namespace std;
#define N 100002
#define lson l,m,lv+1
#define rson m+1,r,lv+1
int tr[22]
;
int num[22]
;
int st
;
int ar
;
void build(int l,int r,int lv)
{
if(l==r) return;
int m=(l+r)>>1;
int same=0,i;
for(i=m;i>=l;i--)
if(st[i]==st[m])
same++;
else
break;
int lid=l,rid=m+1;
for(i=l;i<=r;i++)
if(tr[lv][i]<st[m])
{
num[lv][i]=i==l?1:num[lv][i-1]+1;
tr[lv+1][lid++]=tr[lv][i];
}
else if(tr[lv][i]==st[m]&&same>0)
{
num[lv][i]=i==l?1:num[lv][i-1]+1;
tr[lv+1][lid++]=tr[lv][i];
same--;
}
else
{
num[lv][i]=i==l?0:num[lv][i-1];
tr[lv+1][rid++]=tr[lv][i];
}
build(lson);
build(rson);
}
void query(int L,int R,int k,int l,int r,int lv)//整个这个里面操作都简单了许多、、、
{
if(l==r)
{
printf("%d\n",tr[lv][l]);
return ;
}
int m=(l+r)>>1;
int cnt=num[lv][l+R-1]-(L==1?0:num[lv][l+L-2]);//求区间 里面第 L个数到 第 R个数有几个被划分到左边
if(cnt>=k)
{
int lid=L==1?1:(num[lv][l+L-2]+1);
int rid=cnt+lid-1;
query(lid,rid,k,lson);
}
else
{
int lid=L==1?1:(L-num[lv][l+L-2]); //建立新的查询区间
int rid=R-num[lv][l+R-1];
query(lid,rid,k-cnt,rson);
}
}
int main()
{
int T;
int n,m;
int i;
scanf("%d",&T);
while(T--)
{
scanf("%d %d",&n,&m);
for(i=1;i<=n;i++)
{
scanf("%d",&tr[0][i]);
st[i]=tr[0][i];
}
sort(st+1,st+i);
build(1,n,0);
int l,r,k;
for(i=0;i<m;i++)
{
scanf("%d %d %d",&l,&r,&k);
query(l,r,k,1,n,0);
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: