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 ...
随机推荐
- session中load()跟get()的区别
1.相同点:Session.load/get方法均可以根据指定的实体类和id从数据库读取记录,并返回与之对应的实体对象. 2.区别在于: (1)如果未能发现符合条件的记录,get方法返回null,而l ...
- php循环语句(二)
for 循环 for 循环用于您预先知道脚本需要运行的次数的情况. 语法 for (初始值; 条件; 增量) { 要执行的代码; } 参数: 初始值:主要是初始化一个变量值,用于设置一个计数器(但可以 ...
- jeecms技术预研
版权声明:本文版权归云焰所有,转载请注明. https://blog.csdn.net/yan_dk/article/details/27874461 1 Jeecms框架 1.1 ...
- 网易DMARC设置详解
网易企业邮箱DMARC协议设置详解 发布时间:2013-09-27 尊敬的企业用户: 据统计,全球范围内被投递的钓鱼邮件每天约达到1亿封,无孔不入的钓鱼邮件.诈骗邮件,威胁着用户的信息和财产安全.网易 ...
- struts2-环境搭建-访问流程-配置详解-常量配置-类详解
1 struts2概述 1.1 概念 1.2 struts2使用优势 自动封装参数 参数校验 结果的处理(转发|重定向) 国际化 显示等待页面 表单的防止重复提交 struts2具有更加先进的架构以 ...
- tensorflow/model下的各个参数的理解
首先,这个对应的proto就是 然后config里面的image_resizer等等 就是proto里面的image_resizer 等等,对应的参数可以在proto里面寻找解释和默认值以及类型 再比 ...
- 差分进化算法(DE)的C++面向对象方法实现
代码来源于网络,写得非常棒 /*DE_test *对相应的Matlab程序进行测试 */ #include <iostream> #include <cmath> #inclu ...
- TZOJ 3042 切蛋糕(并查集)
描述 KK是个心灵手巧的好姑娘,她做了一个大蛋糕请她的好朋友们来品尝.这个蛋糕分成n×n个正方形小格,每个小格包含一块水果.KK要把蛋糕切成若干块,显然她不会破坏任意一个小格.无聊的某同学在她切蛋糕时 ...
- Leetcode520Detect Capital检测大写字母
给定一个单词,你需要判断单词的大写使用是否正确. 我们定义,在以下情况时,单词的大写用法是正确的: 全部字母都是大写,比如"USA". 单词中所有字母都不是大写,比如"l ...
- 在rabbitmq操作页面上添加队列、交换器及绑定示图
1.添加队列 2.添加交换器 3.绑定