hdu 4251 The Famous ICPC Team Again划分树入门题
The Famous ICPC Team Again
Time Limit: 30000/15000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 1091 Accepted Submission(s): 530
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.
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.
each query, output a single line containing an integer that denotes the
difficultness of the problem that Mr. B should choose.
3
3
2
Case 2:
6
6
4
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std; const int MAXN=;
int tree[][MAXN];//表示每层每个位置的值
int sorted[MAXN];//已经排序的数
int toleft[][MAXN];//toleft[p][i]表示第i层从1到i有多少个数分入左边 void build(int l,int r,int dep)
{
if(l==r)return;
int mid=(l+r)>>;
int same=mid-l+;//表示等于中间值而且被分入左边的个数
for(int i=l;i<=r;i++)
if(tree[dep][i]<sorted[mid])
same--;
int lpos=l;
int rpos=mid+;
for(int i=l;i<=r;i++)
{
if(tree[dep][i]<sorted[mid])//比中间的数小,分入左边
tree[dep+][lpos++]=tree[dep][i];
else if(tree[dep][i]==sorted[mid]&&same>)
{
tree[dep+][lpos++]=tree[dep][i];
same--;
}
else //比中间值大分入右边
tree[dep+][rpos++]=tree[dep][i];
toleft[dep][i]=toleft[dep][l-]+lpos-l;//从1到i放左边的个数 }
build(l,mid,dep+);
build(mid+,r,dep+); } //查询区间第k大的数,[L,R]是大区间,[l,r]是要查询的小区间
int query(int L,int R,int l,int r,int dep,int k)
{
if(l==r)return tree[dep][l];
int mid=(L+R)>>;
int cnt=toleft[dep][r]-toleft[dep][l-];//[l,r]中位于左边的个数
if(cnt>=k)
{
//L+要查询的区间前被放在左边的个数
int newl=L+toleft[dep][l-]-toleft[dep][L-];
//左端点加上查询区间会被放在左边的个数
int newr=newl+cnt-;
return query(L,mid,newl,newr,dep+,k);
}
else
{
int newr=r+toleft[dep][R]-toleft[dep][r];
int newl=newr-(r-l-cnt);
return query(mid+,R,newl,newr,dep+,k-cnt);
}
} int main(){
int T;
int n,m;
int s,t,k;
int cnt=;
while(scanf("%d",&n)!=EOF)
{
cnt++;
//scanf("%d%d",&n,&m);
memset(tree,,sizeof(tree));//这个必须
for(int i=;i<=n;i++)//从1开始
{
scanf("%d",&tree[][i]);
sorted[i]=tree[][i];
}
sort(sorted+,sorted+n+);
build(,n,);
scanf("%d",&m);
printf("Case %d:\n",cnt);
while(m--)
{
scanf("%d%d",&s,&t);
k=+(t-s)/;//此处即为欲求的中间值属于第几大
printf("%d\n",query(,n,s,t,,k));
}
}
return ;
}
hdu 4251 The Famous ICPC Team Again划分树入门题的更多相关文章
- HDU 4251 The Famous ICPC Team Again(划分树)
The Famous ICPC Team Again Time Limit: 30000/15000 MS (Java/Others) Memory Limit: 32768/32768 K ( ...
- HDU 4251 The Famous ICPC Team Again 主席树
The Famous ICPC Team Again Problem Description When Mr. B, Mr. G and Mr. M were preparing for the ...
- HDOJ 4251 The Famous ICPC Team Again
划分树水题..... The Famous ICPC Team Again Time Limit: 30000/15000 MS (Java/Others) Memory Limit: 3276 ...
- 【HDOJ】4251 The Famous ICPC Team Again
划分树模板题目,主席树也可解.划分树. /* 4251 */ #include <iostream> #include <sstream> #include <strin ...
- HDU 4247 A Famous ICPC Team
Problem Description Mr. B, Mr. G, Mr. M and their coach Professor S are planning their way to Warsaw ...
- hdu 2191 珍惜现在,感恩生活 多重背包入门题
背包九讲下载CSDN 背包九讲内容 多重背包: hdu 2191 珍惜现在,感恩生活 多重背包入门题 使用将多重背包转化为完全背包与01背包求解: 对于w*num>= V这时就是完全背包,完全背 ...
- 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 Con ...
- poj 2104 K-th Number (划分树入门 或者 主席树入门)
题意:给n个数,m次询问,每次询问L到R中第k小的数是哪个 算法1:划分树 #include<cstdio> #include<cstring> #include<alg ...
- hdu 1465:不容易系列之一(递推入门题)
不容易系列之一 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Sub ...
随机推荐
- Java-String字符串相关
字符串String: 封装char[] 字符数组,不可变(因为在底层代码中,值用final关键字修饰) 字符串的字面值: 如果第一次用到一个字符串字面值,会在内存中"字符串常量池" ...
- JQ中的问题
$(function(){$(document).bind("click", function (e) {$(e.target).closest("p").cs ...
- 详解HTML中的表格标签
详细代码如下: <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" ...
- [windows]命令行关机或重启电脑
1.关机:菜单--〉运行--〉输入:cmd--〉输入:shutdown -s -t 0 2.重启:菜单--〉运行--〉输入:cmd--〉输入:shutdown -r -t 0 (注:“-r”代表重启, ...
- Java中枚举类型Enum的一种使用方式
枚举类定义如下: public enum Status { SCUUESS("1", "成功"), FAILED("2", "失败 ...
- TLint for 虎扑体育应用源码项目
虎扑非官方客户端TLint全新Material Design设计,简洁美观支持论坛全部操作,浏览帖子.点亮.回复.引用.收藏等多项个性化设置(不同主题,不同阅读模式) TLint For 虎扑体育 更 ...
- css3 省略号
overflow: hidden; text-overflow: ellipsis; white-space: nowrap; 也无需给元素设置固定宽度!
- bzoj 2658
首先考虑容斥 我们计算出所有没有点在其中的矩形,然后用所有矩形减去这些矩形即可 然后考虑如何计算没有点在其中的矩形 采用扫描线的思想,从上向下一行一行扫,假设我们扫到的行编号是$a$,然后考虑如果左右 ...
- Vue项目经验
Vue项目经验 setInterval路由跳转继续运行并没有及时进行销毁比如一些弹幕,走马灯文字,这类需要定时调用的,路由跳转之后,因为组件已经销毁了,但是setInterval还没有销毁,还在继续后 ...
- 博弈论入门 Bash 、Nim 、Wythoff's Game结论及c++代码实现
SG函数先不说,给自己总结下三大博弈.和二进制及黄金分割联系密切,数学真奇妙,如果不用考试就更好了. 1.Bash Game:n个物品,最少取1个,最多取m个,先取完者胜. 给对手留下(m+1)的倍数 ...