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

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

  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 Problem Description   When Mr. B, Mr. G and Mr. M were preparing for the ...

  3. HDOJ 4251 The Famous ICPC Team Again

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

  4. 【HDOJ】4251 The Famous ICPC Team Again

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

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

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

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

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

  8. HDU 3473 Minimum Sum 划分树,数据结构 难度:1

    http://acm.hdu.edu.cn/showproblem.php?pid=3473 划分树模板题目,需要注意的是划分树的k是由1开始的 划分树: 参考:http://blog.csdn.ne ...

  9. HDU 4251 --- 主席树(划分树是正解)

    题意:查询区间中位数 思路:模板题,相当于区间第K大的数,主席树可以水过,但划分树是正解.但还没搞明白划分树,先上模板 #include <iostream> #include <c ...

随机推荐

  1. 服务过美国总统竞选的非传统投票UI [解析及DEMO]

    上篇文章和大家介绍了需求情况和难点分析,大家可以看这个链接了解详细    服务过美国总统竞选的非传统投票UI      =================正文开始=================== ...

  2. Java中Set的contains()方法——hashCode与equals方法的约定及重写原则

    转自:http://blog.csdn.net/renfufei/article/details/14163329 翻译人员: 铁锚 翻译时间: 2013年11月5日 原文链接: Java hashC ...

  3. JMeter学习笔记(三) 录制脚本

    jmeter测试脚本,可以通过其他工具进行录制,例如 BadBoy,我之前使用过此工具,安装以及使用都比较简单的,大家可以在网上搜索一下. 在此整理一下jmeter自带的录制功能,进行录制脚本. 1. ...

  4. shell监控脚本

    序言: 前几天一好友问我服务器监控怎么做?你们公司的监控是怎么做的?有什么开源的监控软件推荐?常见的开源的监控软件当然首先推荐ZABBIX,分布式够强大,而且很多公司都在用,我问他具体什么需求,能监控 ...

  5. 聊聊、Mybatis API

    API Mybatis 到底解决了什么问题,持久化框架是什么,没出现 Mybatis 之前我们又是怎么来操作数据库的呢?对于 Java语言 来说,JDBC标准 是比较底层的了,但并非最底层的,可以说 ...

  6. 使用Cookie保存用户和密码然后自动登录

    login.html <!DOCTYPE html> <html lang="en"> <head> <meta charset=&quo ...

  7. 【bzoj1976】[BeiJing2010组队]能量魔方 Cube 网络流最小割

    题目描述 一个n*n*n的立方体,每个位置为0或1.有些位置已经确定,还有一些需要待填入.问最后可以得到的 相邻且填入的数不同的点对 的数目最大. 输入 第一行包含一个数N,表示魔方的大小. 接下来 ...

  8. 【bzoj5017】[Snoi2017]炸弹 线段树优化建图+Tarjan+拓扑排序

    题目描述 在一条直线上有 N 个炸弹,每个炸弹的坐标是 Xi,爆炸半径是 Ri,当一个炸弹爆炸时,如果另一个炸弹所在位置 Xj 满足:  Xi−Ri≤Xj≤Xi+Ri,那么,该炸弹也会被引爆.  现在 ...

  9. JZOJ 5279 香港记者

    一句话题意:一个带点权边权的无向图,输出从1号点到n号点的最短路中字典序最小的一条路径 样例输入: 8 9 1 2 3 4 5 6 7 8 1 2 2 2 3 3 3 8 3 1 4 3 4 5 2 ...

  10. 看了就学会之React redux入门示例

    环境准备 为了方便,这里使用create-react-app搭建react环境 create-react-app mydemo 弹出配置 如果需要自定义react的配置,需要运行下面的命令把配置文件弹 ...