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): 1091 Accepted Submission(s): 530
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.
3
3
2
Case 2:
6
6
4
#include<stdio.h>
#include<iostream>
#include<string.h>
#include<algorithm>
using namespace std; const int MAXN=;
int tree[][MAXN];//表示每层每个位置的值
int sorted[MAXN];//已经排序的数
int toleft[][MAXN];//toleft[p][i]表示第i层从1到i有多少个数分入左边 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+); } //查询区间第k大的数,[L,R]是大区间,[l,r]是要查询的小区间
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-];//[l,r]中位于左边的个数
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
{
int newr=r+toleft[dep][R]-toleft[dep][r];
int newl=newr-(r-l-cnt);
return query(mid+,R,newl,newr,dep+,k-cnt);
}
} int main(){
int T;
int n,m;
int s,t,k;
int cnt=;
while(scanf("%d",&n)!=EOF)
{
cnt++;
//scanf("%d%d",&n,&m);
memset(tree,,sizeof(tree));//这个必须
for(int i=;i<=n;i++)//从1开始
{
scanf("%d",&tree[][i]);
sorted[i]=tree[][i];
}
sort(sorted+,sorted+n+);
build(,n,);
scanf("%d",&m);
printf("Case %d:\n",cnt);
while(m--)
{
scanf("%d%d",&s,&t);
k=+(t-s)/;//此处即为欲求的中间值属于第几大
printf("%d\n",query(,n,s,t,,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 ...
- hdu 2191 珍惜现在,感恩生活 多重背包入门题
背包九讲下载CSDN 背包九讲内容 多重背包: hdu 2191 珍惜现在,感恩生活 多重背包入门题 使用将多重背包转化为完全背包与01背包求解: 对于w*num>= V这时就是完全背包,完全背 ...
- 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 ...
- poj 2104 K-th Number (划分树入门 或者 主席树入门)
题意:给n个数,m次询问,每次询问L到R中第k小的数是哪个 算法1:划分树 #include<cstdio> #include<cstring> #include<alg ...
- hdu 1465:不容易系列之一(递推入门题)
不容易系列之一 Time Limit: 2000/1000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others)Total Sub ...
随机推荐
- hihocoder1822 战舰日常任务
思路: 使用堆即可. 实现: #include <iostream> #include <map> #include <vector> #include <c ...
- Git 学习教程【转+总结】
之前是在用SVN,现在因为小伙伴比较喜欢Git,所以也开始学习Git,很感谢 时光穿梭机 - 廖雪峰 的无私奉献.本文用来记录我在学习Git过程中的收获和笔记,廖雪峰大神的Git教程参考这里. 1.G ...
- 清空iptables
/sbin/iptables -P INPUT ACCEPT /sbin/iptables -F iptables -L
- UVA1663 Purifying Machine (匈牙利算法,二分图最大匹配)
模版集合个数减少是因为匹配串集合中没被匹配过的一对串匹配了.所以就是找一个二分图最大匹配. 因为集合X和Y是不好分开的,但是可以直接跑,两个集合都会跑一遍,所以一个匹配会被算两次,返回的时候除以2就行 ...
- ios push Payload
https://developer.apple.com/library/content/documentation/NetworkingInternet/Conceptual/RemoteNotifi ...
- 换个语言学一下 Golang (5)——运算符
运算符用于在程序运行时执行数学或逻辑运算. Go 语言内置的运算符有: 算术运算符 关系运算符 逻辑运算符 位运算符 赋值运算符 其他运算符 接下来让我们来详细看看各个运算符的介绍. 算术运算符 下表 ...
- Linux部署多个tomcat
Linux部署多个tomcat 1.环境:1.1. Centos 5.01.2.Tomcat 5.5.17 2.需要解决一下几个问题2.1.不同的tomcat启动和关闭监听不同的端口2.2.不同的to ...
- QT5:先导篇 全局定义
一.简介 <QtGlobal>头文件包含了Qt类库的一些全局定义,包含基本数据类型 函数和宏 二.全局变量定义 <QtGlobal>定义的数据类型: Qt数据类型 ...
- 初涉平衡树「treap」
treap:一种平衡的二叉搜索树 什么是treap(带旋) treap=tree+heap,这大家都知道.因为二叉搜索树(BST)非常容易被卡成一条链而影响效率,所以我们需要一种更加平衡的树形结构,从 ...
- js 做的随机8位验证码
开发思路: 画出放置验证码的模块.一个写有“看不清…”的小块,以及输入验证码的文本框 获取各个模块 封装一个函数Yan_ma(),设置验证码为8位,里面含有数字,小写字母,小写字母和中文.每种类型出现 ...