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
 裸的 划分树。。

#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(划分树)的更多相关文章

  1. HDU 4251 The Famous ICPC Team Again(划分树)

    The Famous ICPC Team Again Time Limit: 30000/15000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  2. hdu 4251 The Famous ICPC Team Again划分树入门题

    The Famous ICPC Team Again Time Limit: 30000/15000 MS (Java/Others)    Memory Limit: 32768/32768 K ( ...

  3. 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 ...

  4. HDOJ 4251 The Famous ICPC Team Again

    划分树水题..... The Famous ICPC Team Again Time Limit: 30000/15000 MS (Java/Others)    Memory Limit: 3276 ...

  5. 【HDOJ】4251 The Famous ICPC Team Again

    划分树模板题目,主席树也可解.划分树. /* 4251 */ #include <iostream> #include <sstream> #include <strin ...

  6. 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 ...

  7. Hdu 4251 区间中位数(划分树)

    题目链接 The Famous ICPC Team Again Time Limit: 30000/15000 MS (Java/Others)    Memory Limit: 32768/3276 ...

  8. hdu4417 Super Mario 树阵离线/划分树

    http://acm.hdu.edu.cn/showproblem.php?pid=4417 Super Mario Time Limit: 2000/1000 MS (Java/Others)    ...

  9. HDU 4417 Super Mario(划分树)

    Super Mario Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total ...

随机推荐

  1. JS对象和数组深浅拷贝总结②

    在实际开发中遇到过太多次深拷贝浅拷贝的问题.总结一下~ JS数据存储和深浅拷贝实际运用① 这是之前写过的一篇文章,解决浅拷贝深拷贝的问题只说了一种方法,今天来补充一下. 介绍深拷贝和浅拷贝都在上一篇文 ...

  2. (转载)My97 datepicker使用指南

    这里先显示大家都可以看到的My97DatePicker的用法: WdatePicker日历控件使用方法(http://www.cnblogs.com/yuhanzhong/archive/2011/0 ...

  3. resin乱码

    一.jsp乱码 为何jsp 在resin下乱码,但在tomcat下却工作良好的问题 关于JSP页面中的pageEncoding和contentType两种属性的区别:       pageEncodi ...

  4. Eureka 客户端连接Eureka服务端时 报Cannot execute request on any known server 解决办法

    报Cannot execute request on any known server 这个错,总的来说就是连接Eureka服务端地址不对. 因为配置eureka.client.serviceUrl. ...

  5. PHP基于openssl实现的非对称加密操作

    使用非对称加密主要是借助openssl的公钥和私钥,用公钥加密私钥解密,或者私钥加密公钥解密. 1.安装openssl和php的openssl扩展 2.生成私钥:openssl genrsa 用于生成 ...

  6. Redis消息通知

    Redis的消息通知通过列表类型实现,分为两种模式:阻塞式.发布/订阅式: 阻塞式 顾名思义,消息生产者负责生产消息,并将消息放到队列的一端,消息的消费者负责消费消息,从队列的另一端取出消息,然后对其 ...

  7. hbase 聚合操作

    hbase本身提供了 聚合方法可以服务端聚合操作 hbase中的CoprocessorProtocol机制. CoprocessorProtocol的原理比较简单,近似于一个mapreduce框架.由 ...

  8. Blow up the city

    Blow up the city 时间限制: 1 Sec  内存限制: 128 MB 题目描述 Country A and B are at war. Country A needs to organ ...

  9. 【html、CSS、javascript-3】几个基本元素

    HTML 元素指的是从开始标签到结束标签的所有代码. 开始标签 元素内容 结束标签 <h1> h标签用来表示标题 </h1> <p> p标签表示一个段落 </ ...

  10. 微信Web开发者工具报错:net::ERR_BLOCKED_BY_CLIENT

    今天在微信调试琳德的拼团,用了挺久之后,输入什么测试地址:发现页面空白,console提示身份验证网址错误,错误类型为:net::ERR_BLOCKED_BY_CLIENT, 尝试了好多次.以为是我的 ...