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): 1440 Accepted Submission(s): 708
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.
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
3
3
2
Case 2:
6
6
4
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <time.h>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#define met(a,b) memset(a,b,sizeof a)
#define pb push_back
#define lson(x) ((x<<1))
#define rson(x) ((x<<1)+1)
using namespace std;
typedef long long ll;
const int N=1e5+;
const int M=N*N+;
struct P_Tree {
int n;
int tree[][N];
int sorted[N];
int toleft[][N];
void init(int len) {
n=len;
for(int i=; i<; i++)tree[i][]=toleft[i][]=;
for(int i=; i<=n; i++) {
scanf("%d",&sorted[i]);
tree[][i]=sorted[i];
}
sort(sorted+,sorted+n+);
build(,n,);
}
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+);
}
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-];
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 {
//r+区间后分入左边的数的个数
int newr=r+toleft[dep][R]-toleft[dep][r];
//右端点减去区间分入右边的数的个数
int newl=newr-(r-l-cnt);
return query(mid+,R,newl,newr,dep+,k-cnt);//注意
}
}
}tre;
int main() {
int iCase=;
int n,m;
int u,v;
while(~scanf("%d",&n)) {
tre.init(n);
scanf("%d",&m);
printf("Case %d:\n",++iCase);
while(m--) {
scanf("%d%d",&u,&v);
int k=(v-u)/+;
printf("%d\n",tre.query(,n,u,v,,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 ...
- 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 ...
- Hdu 4251 区间中位数(划分树)
题目链接 The Famous ICPC Team Again Time Limit: 30000/15000 MS (Java/Others) Memory Limit: 32768/3276 ...
- HDU 3473 Minimum Sum 划分树,数据结构 难度:1
http://acm.hdu.edu.cn/showproblem.php?pid=3473 划分树模板题目,需要注意的是划分树的k是由1开始的 划分树: 参考:http://blog.csdn.ne ...
- HDU 4251 --- 主席树(划分树是正解)
题意:查询区间中位数 思路:模板题,相当于区间第K大的数,主席树可以水过,但划分树是正解.但还没搞明白划分树,先上模板 #include <iostream> #include <c ...
随机推荐
- Android之测试相关知识点
程序员在开发的过程中一定要进行严格的测试: --->相关概念 * 根据是否知道源代码可以分为: 黑盒测试:只关心程序执行的过程和结果并不知道程序源代码. 白盒测试: 根据源代码写测试方法 或者 ...
- Python保护变量、私有变量、私有方法
保护变量.私有变量.私有方法介绍: _xxx: 单下划线开头叫保护变量,意思是只有类对象和子类对象自己能访问到这些变量,此变量不能通过from XXX import xxx 导入: __xxx : 双 ...
- 更换checkbox的原有样式
通常情况下,各个浏览器对的样式不一致,并且不那么美观.所以有时候设计需要我们更换原有的样式: html: <span><input type="checkbox" ...
- 关于tap设备
$QEMU_PATH \ -nographic \ -drive file=./rootfs.ext4,format=raw \ -net nic,vlan=0 -net tap,vlan=0,ifn ...
- SQL Server 重新编译存储过程的方式有三种
SQL Server 中,强制重新编译存储过程的方式有三种: sp_recompile 系统存储过程强制在下次执行存储过程时对其重新编译.具体方法是:从过程缓存中删除现有计划,强制在下次运行该过程时创 ...
- [TJOI2017][bzoj4889] 不勤劳的图书管理员 [线段树套线段树]
题面 传送门 思路 考虑两本书的位置交换对答案的贡献: (为了方便描述,用"左边那本"和"右边那本"称呼两本我们要交换的书,"中间那本"是我 ...
- [POI2015][bzoj4383] Pustynia [线段树优化建图+拓扑排序]
题面 bzoj权限题传送门 luogu传送门 思路 首先,这个题目显然可以从所有小的点往大的连边,然后如果没环就一定可行,从起点(入读为0)开始构造就好了 但是问题来了,如果每个都连的话,本题中边数是 ...
- 51nod 1040 最大公约数之和 | 数论
给出一个n,求1-n这n个数,同n的最大公约数的和 n<=1e9 考虑枚举每个因数,对答案贡献的就是个数*大小
- oracle 查看表空间的脚本
SELECT upper(f.tablespace_name) "表空间名", d.Tot_grootte_Mb "表空间大小(M)", d.Tot_groot ...
- SICAU-OJ: 数字游戏
数字游戏 题意:给出一个长度为n的数字,然后抹去k个数,使得剩下的数最大. 题解: 贪心的思想:让答案串中每一位尽可能大. 我们肯定要用完这k次的,假设有一个答案字符串ans,我们现在遍历给出的串,假 ...