您的位置:首页 > 其它

hdu 2665 Kth number(划分树)

2014-06-18 21:24 411 查看

Kth number

Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)

Total Submission(s): 4602 Accepted Submission(s): 1468



[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刚学了划分树,来做这一题,只知道划分树的原理,代码是自己写的,不是通用的写法。G++AC C++超内存。。。
[code]#include <iostream>
#include <cstdio>
#include <vector>
#include <algorithm>
using namespace std;

const int maxn = 100000;
struct tree{
int l , r;
vector<int> element , lft;
}a[3*maxn];
vector<int> seq;
int n , m;

bool cmp(int a , int b){ return a<b;}

void build(int l , int r , int k){
a[k].l = l;
a[k].r = r;
if(l != r){
int mid = (l+r)/2;
int lson = 2*k , rson = 2*k+1;
a[lson].element.clear();
a[rson].element.clear();
a[lson].element.push_back(0);
a[rson].element.push_back(0);
a[lson].lft.clear();
a[rson].lft.clear();
a[lson].lft.push_back(0);
a[rson].lft.push_back(0);
for(int i = 1; i < a[k].element.size(); i++){
if(a[k].element[i] <= seq[mid]){
a[lson].element.push_back(a[k].element[i]);
a[k].lft.push_back(a[k].lft[i-1]+1);
}else{
a[rson].element.push_back(a[k].element[i]);
a[k].lft.push_back(a[k].lft[i-1]);
}
}
build(l , mid , 2*k);
build(mid+1 , r , 2*k+1);
}
}

int query(int l , int r , int k , int kth){
if(a[k].l==a[k].r){
return a[k].element[1];
}else{
int tem = a[k].lft[r]-a[k].lft[l-1];
if(tem >= kth){
return query(1+a[k].lft[l-1] , 1+a[k].lft[l-1]+tem-1 , 2*k , kth);
}else{
return query(1+(l-1-a[k].lft[l-1]) , (l-1-a[k].lft[l-1])+r-l+1-tem , 2*k+1 , kth-tem);
}
}
}

void initial(){
a[1].element.clear();
a[1].element.push_back(0);
a[1].lft.clear();
a[1].lft.push_back(0);
seq.clear();
}

void readcase(){
scanf("%d%d" , &n , &m);
seq.push_back(0);
for(int i = 0; i < n; i++){
int num;
scanf("%d" , &num);
if(seq[0] >= num) seq[0] = num-1;
seq.push_back(num);
a[1].element.push_back(num);
}
}

void computing(){
sort(seq.begin() , seq.end() , cmp);
build(1 , n , 1);
int s , t , k;
while(m--){
scanf("%d%d%d" , &s , &t , &k);
printf("%d\n" , query(s , t , 1 , k));
}
}

int main(){
int t;
scanf("%d" , &t);
while(t--){
initial();
readcase();
computing();
}
return 0;
}


[/code]
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: