The Famous ICPC Team Again
Time Limit: 30000/15000 MS (Java/Others)Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 733Accepted Submission(s): 351
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.
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
Source
Recommend
We have carefully selected several similar problems for you:
42554247425242464248
题意:
给你一个数组。问你两个端点a,b。问你在[a,b]中的中间值。比如1,2,5的中间值为2。
思路:
划分树裸题。会就很简单。不会就无从下手。划分树思想还是类似线段树分治的思想。二分区间再慢慢定位。
由于网上都没怎么细讲。大都直接给的代码。我还是花了一下午研究才茅塞顿开的。明白了就很简单。虽然是
水题1A还是很爽的。呵呵~又多学了个数据结构了。
详细见代码:
#include<algorithm>
#include<iostream>
#include<string.h>
#include<sstream>
#include<stdio.h>
#include<math.h>
#include<vector>
#include<string>
#include<queue>
#include<set>
#include<map>
using namespace std;
const int INF=0x3f3f3f3f;
const int maxn=100010;
int n,m,md;
int seg[20][maxn],lnum[20][maxn],sa[maxn];
void init()
{
memset(lnum,0,sizeof lnum);
sort(sa,sa+n);
}
void btree(int L,int R,int d)
{
int i,lm,ls,rs,mid;
md=max(d,md);
if(L==R)
return;
mid=(L+R)>>1;
lm=mid-L+1;
ls=L;
rs=mid+1;
for(i=L;i<=R;i++)
if(seg[d][i]<sa[mid])
lm--;
for(i=L;i<=R;i++)
{
if(i==L)
lnum[d][i]=0;
else
lnum[d][i]=lnum[d][i-1];
if(seg[d][i]==sa[mid])
{
if(lm>0)
{
lnum[d][i]++;
seg[d+1][ls++]=seg[d][i];
lm--;
}
else
seg[d+1][rs++]=seg[d][i];
}
else if(seg[d][i]<sa[mid])
{
lnum[d][i]++;
seg[d+1][ls++]=seg[d][i];
}
else
seg[d+1][rs++]=seg[d][i];
}
btree(L,mid,d+1);
btree(mid+1,R,d+1);
}
int qu(int L,int R,int l,int r,int d,int k)
{
if(L==R)
return seg[d][L];
int s,ss,b,bb,mid;
if(l==L)
ss=0;
else
ss=lnum[d][l-1];//[1,l-1]进入左树的个数
s=lnum[d][r]-ss;//[l,r]进入左树的个数
//printf("d %d ss %d s %d k %d\n",d,ss,s,k);
mid=(L+R)>>1;
if(s>=k)//在左树中
return qu(L,mid,L+ss,L+ss+s-1,d+1,k);//注意边界。可以确定L+ss-1不为所求所以从L+ss
else
{
bb=l-L-ss;//[1,l-1]进入右树的个数
b=r-l+1-s;//[l,r]进入右树的个数
return qu(mid+1,R,mid+bb+1,mid+bb+b,d+1,k-s);//mid+1+bb+b-1
}
}
int main()
{
int a,b,i,j,cas=1;
while(~scanf("%d",&n))
{
for(i=1;i<=n;i++)
{
scanf("%d",&seg[0][i]);
sa[i]=seg[0][i];
}
md=0;
init();
btree(1,n,0);
// for(i=0;i<=md;i++)
// {
// for(j=1;j<=n;j++)
// printf("%d ",seg[i][j]);
// printf("\n");
// }
scanf("%d",&m);
printf("Case %d:\n",cas++);
while(m--)
{
scanf("%d%d",&a,&b);
printf("%d\n",qu(1,n,a,b,0,(b-a+2)/2));
}
}
return 0;
}
分享到:
相关推荐
hdu 1166线段树代码
离线OJ题库(HDU ZJU等,部分有答案),需联网。
一个十分简单的程序,能够ac杭电hdu的第2050题,无注释,简单明了
hdu 1166线段树
收集的部分HDOJ杭电ACM题的代码 大牛勿下 全是基础供初级acmer使用
HDU的一题........HDU DP动态规
HDU的1250,主要是利用高精度加法,但是代码有点繁琐,效率不是很高
ACM HDU题目分类,我自己总结的大概只有十来个吧
杭电ACMhdu1163
HDU1059的代码
HDU 动态规划(46道题目
hdu1001解题报告
100道 acm C语言 hdu 解题报告
hdu 1574 passed sorce
ACM ICPC HDOJ 1005
NULL 博文链接:https://128kj.iteye.com/blog/1734821
hdu2101AC代码
hdu acm 教案 搜索入门 hdu acm 教案 搜索入门
hdu 1005.比较简单的一道题,有兴趣的可以看看。
hdu-acm源代码(上百题)hdu-acm源代码、hdu-acm源代码hdu-acm源代码