HDU4251-The Famous ICPC Team Again(划分树)
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.
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.
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 <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:\n",T++);
while(m--){
int a,b;
scanf("%d%d",&a,&b);
printf("%d\n",query(a,b,1,0,(b-a)/2+1));
}
}
return 0;
}
HDU4251-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 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 4251 区间中位数(划分树)
题目链接 The Famous ICPC Team Again Time Limit: 30000/15000 MS (Java/Others) Memory Limit: 32768/3276 ...
- hdu4417 Super Mario 树阵离线/划分树
http://acm.hdu.edu.cn/showproblem.php?pid=4417 Super Mario Time Limit: 2000/1000 MS (Java/Others) ...
- HDU 4417 Super Mario(划分树)
Super Mario Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others)Total ...
随机推荐
- skyline中遍历fly工程树
在skyline二次开发过程中,做空间分析总是要去读取工程中的shp图层.假设想获取工程树中“建筑物”图层,图层结构如下: 传统的方法是: var itemId=SGWorld.ProjectTree ...
- (二)通过JAVA调用SAP接口 (增加一二级参数)
(二)通过JAVA调用SAP接口 (增加一二级参数) 一.建立sap连接 请参考我的上一篇博客 JAVA连接SAP 二.测试项目环境准备 在上一篇操作下已经建好的环境后,在上面的基础上新增类即可 三. ...
- 【DM642学习笔记二】dsp基础实验:发光二级管的显示 led.c
1,OSDFPGA配置一个专用的8位寄存器控制指示灯亮灭,访问地址为90080017h,由电路图可知低电平点亮. 2,程序运行时,可直接editmemory.即修改90080017h地址的值(可在Ed ...
- Merge array and hash in ruby if key appears in array
I have two arrays one = [1,2,3,4,5,6,7] and two = [{1=>'10'},{3=>'22'},{7=>'40'}] Two will ...
- HBase性能优化方法总结 (转)
AutoFlush 通过调用HTable.setAutoFlushTo(false)方法可以将HTable写客户端自动flush关闭,这样可以批量写入数据到HBase,而不是有一条put就执行一次更新 ...
- AGC035D
AGC035D Add and Remove 题意 给出\(n\)个数,每次删除一个不在两端的数,然后把它的权值加到相邻的两个数上. 问操作\(n-2\)次后,所剩的两数之和的最小值 \(n\le18 ...
- [Array]217.Contains Duplicate
Given an array of integers, find if the array contains any duplicates. Your function should return t ...
- bzoj 1123 [POI2008]BLO——点双连通分量
题目:https://www.lydsy.com/JudgeOnline/problem.php?id=1123 点双连通分量缩点,然后各种各样. 结果不会写了.比如新连边.记录一个点是割点缩成的点还 ...
- ACdream 1112
题目链接 Alice and Bob Time Limit: 6000/3000MS (Java/Others)Memory Limit: 256000/128000KB (Java/Others) ...
- Leetcode54. Spiral Matrix螺旋矩阵
给定一个包含 m x n 个元素的矩阵(m 行, n 列),请按照顺时针螺旋顺序,返回矩阵中的所有元素. 示例 1: 输入: [ [ 1, 2, 3 ], [ 4, 5, 6 ], [ 7, 8, 9 ...