HDU4251-The Famous ICPC Team Again(划分树)
Posted ldxsuanfa
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了HDU4251-The Famous ICPC Team Again(划分树)相关的知识,希望对你有一定的参考价值。
Problem Description
When Mr. B, Mr. G and Mr. M were preparing for the 2012 ACM-ICPC World Final Contest, Mr. B had collected a large set of contest problems for their daily training. When they decided to take training, Mr. B would choose one of them
from the problem set. All the problems in the problem set had been sorted by their time of publish. Each time Prof. S, their coach, would tell them to choose one problem published within a particular time interval. That is to say, if problems had been sorted
in a line, each time they would choose one of them from a specified segment of the line.
Moreover, when collecting the problems, Mr. B had also known an estimation of each problem’s difficultness. When he was asked to choose a problem, if he chose the easiest one, Mr. G would complain that “Hey, what a trivial problem!”; if he chose the hardest one, Mr. M would grumble that it took too much time to finish it. To address this dilemma, Mr. B decided to take the one with the medium difficulty. Therefore, he needed a way to know the median number in the given interval of the sequence.
Moreover, when collecting the problems, Mr. B had also known an estimation of each problem’s difficultness. When he was asked to choose a problem, if he chose the easiest one, Mr. G would complain that “Hey, what a trivial problem!”; if he chose the hardest one, Mr. M would grumble that it took too much time to finish it. To address this dilemma, Mr. B decided to take the one with the medium difficulty. Therefore, he needed a way to know the median number in the given interval of the sequence.
?
Input
For each test case, the first line contains a single integer n (1 <= n <= 100,000) indicating the total number of problems. The second line contains n integers xi (0 <= xi <= 1,000,000,000), separated by single space, denoting the
difficultness of each problem, already sorted by publish time. The next line contains a single integer m (1 <= m <= 100,000), specifying number of queries. Then m lines follow, each line contains a pair of integers, A and B (1 <= A <= B <= n), denoting that
Mr. B needed to choose a problem between positions A and B (inclusively, positions are counted from 1). It is guaranteed that the number of items between A and B is odd.
?
Output
For each query, output a single line containing an integer that denotes the difficultness of the problem that Mr. B should choose.
?
Sample Input
5
5 3 2 4 1
3
1 3
2 4
3 5
5
10 6 4 8 2
3
1 3
2 4
3 5
?
Sample Output
Case 1:
3
3
2
Case 2:
6
6
4
?裸的 划分树。。
。
。
#include <iostream>
#include <cstdio>
#include <cstring>
#include <vector>
#include <string>
#include <algorithm>
#include <queue>
using namespace std;
const int maxn = 100000+10;
int n,m;
struct node{
int lson,rson;
int mid(){
return (lson+rson)>>1;
}
}tree[maxn*4];
int num[maxn];
int seg[25][maxn];
int lftnum[25][maxn];
void build(int L,int R,int rt,int dep){
tree[rt].lson = L;
tree[rt].rson = R;
if(L==R) return;
int mid = tree[rt].mid(),key = num[mid];
int lcnt = mid-L+1;
for(int i = L; i <= R; i++){
if(seg[dep][i] < key){
lcnt--;
}
}
int sta = L, ed = mid+1;
for(int i = L;i <= R; i++){
if(i==L){
lftnum[dep][i] = 0;
}else{
lftnum[dep][i] = lftnum[dep][i-1];
}
if(seg[dep][i] < key){
lftnum[dep][i]++;
seg[dep+1][sta++] = seg[dep][i];
}
else if(seg[dep][i] > key){
seg[dep+1][ed++] = seg[dep][i];
}
else{
if(lcnt>0){
lftnum[dep][i]++;
lcnt--;
seg[dep+1][sta++] = seg[dep][i];
}else{
seg[dep+1][ed++] = seg[dep][i];
}
}
}
build(L,mid,rt<<1,dep+1);
build(mid+1,R,rt<<1|1,dep+1);
}
int query(int L,int R,int rt,int dep,int k){
if(tree[rt].lson ==tree[rt].rson) return seg[dep][tree[rt].lson];
int ucnt,ncnt;
int mid = tree[rt].mid();
if(tree[rt].lson == L){
ncnt = 0;
ucnt = lftnum[dep][R];
}else{
ncnt = lftnum[dep][L-1];
ucnt = lftnum[dep][R] - lftnum[dep][L-1];
}
if(ucnt >= k){
L = tree[rt].lson + ncnt;
R = tree[rt].lson + ncnt + ucnt-1;
return query(L,R,rt<<1,dep+1,k);
}else{
int a = L - tree[rt].lson - ncnt;
int b = R - L - ucnt + 1;
L = mid+a+1;
R = mid+a+b;
return query(L,R,rt<<1|1,dep+1,k-ucnt);
}
}
int main(){
int T = 1;
while(cin >> n){
memset(seg,0,sizeof seg);
memset(lftnum,0,sizeof lftnum);
for(int i = 1; i <= n; i++){
scanf("%d",&num[i]);
seg[0][i] = num[i];
}
sort(num+1,num+n+1);
build(1,n,1,0);
scanf("%d",&m);
printf("Case %d:
",T++);
while(m--){
int a,b;
scanf("%d%d",&a,&b);
printf("%d
",query(a,b,1,0,(b-a)/2+1));
}
}
return 0;
}
以上是关于HDU4251-The Famous ICPC Team Again(划分树)的主要内容,如果未能解决你的问题,请参考以下文章
rfc4251: The Secure Shell (SSH) Protocol Architecture
POJ2092 LA3157 HDU1347 ZOJ2250 Grandpa is Famous排序