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 ...
随机推荐
- thinkphp分页集成
控制器: $User = M('webcase'); // 实例化 User 对象 $list = $User->order('id desc')->page($_GET['p'].', ...
- powershell 根据错误GUID查寻错误详情
使用azurepowershell 部署模板时,碰到了下面类似的问题: The template deployment 'ExampleDeployment-' is not valid accord ...
- springMVC中ajax和后台数据格式错误
前台ajax: $.ajax("${pageContext.request.contextPath}/hello",// 发送请求的URL字符串. { dataType : &qu ...
- 使用python查询天气
python主代码 weather.py import urllib2 import json from city import city cityname = raw_input('你想查哪个城市的 ...
- access处理重复创建表的方法。
第一种,使用MSysObjects表查找表名为当前创建表的名字的内容,相当于普通查询,但是access数据库有一个安全问题,就是有时候一开始是没有权限去调这些系统表的,这时可以再2007的access ...
- UVA - 1279 Asteroid Rangers (动点的最小生成树)
题意,有n个匀速动点,求最小生成树的改变次数. 一句话总结:动态问题的一般做法是先求出一个静态的解,然后求出解发生改变的事件,事件按照时间排序,依次处理. 先求出最开始的最小生成树(MST),当MST ...
- #include <> 和 #inlude ""的区别
#include < >引用的是编译器的类库路径里面的头文件#include " "引用的是你程序目录的相对路径中的头文件,在程序目录的相对路径中找不到该头文件时会继 ...
- 几句话总结一个算法之RNN、LSTM和GRU
RNN 一般神经网络隐层的计算是h=g(w * x),其中g是激活函数,相比于一般神经网络,RNN需要考虑之前序列的信息,因此它的隐藏h的计算除了当前输入还要考虑上一个状态的隐藏,h=g(w*x+w' ...
- C语言特点_01
C语言特点: 1.C语言的32个关键字 auto 局部变量(自动储存) break 无条件退出程序最内层循环 case switch语句中选择项 char 单字节整型数据 const 定义不可更改的常 ...
- ZJOI2018游记Round1
广告 ZJOI2018Round2游记 All Falls Down 非常感谢学弟学妹们捧场游记虽然这是一篇假游记 ZJOI Round1今天正式落下帷幕.在这过去的三天里遇到了很多朋友,见识了很多有 ...