题目链接

The Famous ICPC Team Again

Time Limit: 30000/15000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)
Total Submission(s): 796    Accepted Submission(s): 388

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
今天的最后一题,我现在在写解题报告,,,有种要猝死的感觉,特么今天敲代码敲的精疲力尽。
A了5题,全是数据结果,什么线段树,归并树, 划分树,并查集,快速选择。。。
对了,这题就是用划分树做的。。。1A, 题意是求区间中位数,转化为求区间第k大,即(R - L) / 2;
Accepted Code:
 /*************************************************************************
> File Name: 4251.cpp
> Author: Stomach_ache
> Mail: sudaweitong@gmail.com
> Created Time: 2014年08月02日 星期六 23时02分48秒
> Propose:
************************************************************************/ #include <cmath>
#include <string>
#include <cstdio>
#include <fstream>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std; const int maxn = ;
int n, m;
int a[maxn], nums[maxn];
int toLeft[][maxn];
int tr[][maxn]; void build(int d, int l, int r) {
int mid = (l + r) / ;
int le = , ls = l, rs = mid + ;
for (int i = mid; i >= l; i--) {
if (nums[i] == nums[mid]) le++;
else break;
}
for (int i = l; i <= r; i++) {
if (i == l) toLeft[d][i] = ;
else toLeft[d][i] = toLeft[d][i-]; if (tr[d][i] < nums[mid]) {
toLeft[d][i]++;
tr[d+][ls++] = tr[d][i];
} else if (tr[d][i] > nums[mid]) {
tr[d+][rs++] = tr[d][i];
} else {
if (le) {
le--;
toLeft[d][i]++;
tr[d+][ls++] = tr[d][i];
} else {
tr[d+][rs++] = tr[d][i];
}
}
}
if (l == r) return ;
build(d+, l, mid);
build(d+, mid+, r);
} int query(int d, int l, int r, int ql, int qr, int k) {
if (l == r) return tr[d][l];
int s = (ql == l ? : toLeft[d][ql-]);
int ss = toLeft[d][qr];
int mid = (l + r) / ;
if (ss - s >= k) return query(d+, l, mid, l+s, l+ss-, k);
else return query(d+, mid+, r, mid++(ql-l-s), mid++(qr-l-ss), k-(ss-s));
} int main(void) {
int cas = ;
while(~scanf("%d", &n)) {
memset(toLeft, , sizeof(toLeft));
memset(tr, , sizeof(tr));
for (int i = ; i <= n; i++) {
scanf("%d", a + i);
nums[i] = a[i];
tr[][i] = a[i];
}
sort(nums + , nums + n + );
build(, , n);
printf("Case %d:\n", cas++);
scanf("%d", &m);
while (m--) {
int l, r;
scanf("%d %d", &l, &r);
int ans = query(, , n, l, r, (r-l)/+);
printf("%d\n", ans);
}
} return ;
}
 

Hdu 4251 区间中位数(划分树)的更多相关文章

  1. hdu 5700区间交(线段树)

    区间交 Time Limit: 8000/4000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others)Total Submiss ...

  2. hdu 2665 Kth number(划分树模板)

    http://acm.hdu.edu.cn/showproblem.php?pid=2665 [ poj 2104 2761 ]  改变一下输入就可以过 http://poj.org/problem? ...

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

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

  4. HDU 4417 - Super Mario ( 划分树+二分 / 树状数组+离线处理+离散化)

    题意:给一个数组,每次询问输出在区间[L,R]之间小于H的数字的个数. 此题可以使用划分树在线解决. 划分树可以快速查询区间第K小个数字.逆向思考,判断小于H的最大的一个数字是区间第几小数,即是答案. ...

  5. hdu 2665 Kth number_划分树

    题意:求区间[a,b]的第k大 因为多次询问要用到划分树 #include <iostream> #include<cstdio> #include<algorithm& ...

  6. HDU 2665 Kth number(划分树)

    Kth number Time Limit: 15000/5000 MS (Java/Others) Memory Limit: 32768/32768 K (Java/Others) Total S ...

  7. HDU 4417 Super Mario(划分树)

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

  8. hdu 4417,poj 2104 划分树(模版)归并树(模版)

    这次是彻底把划分树搞明确了,与此同一时候发现了模版的重要性.敲代码一个字符都不能错啊~~~ 划分树具体解释:点击打开链接 题意:求一组数列中随意区间不大于h的个数. 这个题的做法是用二分查询  求给定 ...

  9. HDU 5700 区间交 线段树暴力

    枚举左端点,然后在线段树内,更新所有左边界小于当前点的区间的右端点,然后查线段树二分查第k大就好 #include <cstdio> #include <cstring> #i ...

随机推荐

  1. mapduce简介

    原文引自:http://www.cnblogs.com/shishanyuan/p/4639519.html 1.环境说明 部署节点操作系统为CentOS,防火墙和SElinux禁用,创建了一个shi ...

  2. 从GoogLeNet至Inception v3

    从GoogLeNet至Inception v3 一.CNN发展纵览 我们先来看一张图片: 1985年,Rumelhart和Hinton等人提出了后向传播(Back Propagation,BP)算法( ...

  3. What is python .. (“dot dot”) notation syntax?

    What you have is a float literal without the trailing zero, which you then access the __truediv__met ...

  4. Odoo文档管理/知识管理应用实践 - 上传附件

    测试环境: Odoo8.0 Odoo中的文档管理/知识管理可用于保存采购.销售.生产等一系列业务流程中产生的文件.凭证,可关联到具体的每一笔业务操作:也能用于管理公司的合同.资料,创建知识库以分享内部 ...

  5. springcloud-sleuth实现日志的链路追踪

    1.需要将spring-cloud-starter-sleuth的依赖加入即可(注意:最好使用maven或gradle工具) 代码参考:https://github.com/Pinshuducha/s ...

  6. CODE[VS]4633:Mz树链剖分练习

    Description 给定一棵结点数为n的树,初始点权均为0,有依次q个操作,每次操作有三个参数a,b,c,当a=1时,表示给b号结点到c号结点路径上的所有点(包括b,c,下同)权值都增加1,当a= ...

  7. poj 3263

    传送门 解题思路 如果x与y互相看见,那么他们一定比之间的高,所以给他们之间的高度-1,最后得到的答案是所有牛的高度+h,之间-1会T,用差分数组或线段树维护即可. 代码 #include<io ...

  8. SQL语句中GROUP BY的问题

    今天查询数据库时用到集合函数sum(drp),遇到问题: 百度后,确定如下问题:当select后面查询字段有sum(drp)以外的字段时,必须使用group by函数,对数据进行排序,且查询字段中除s ...

  9. httpclient遇到java.net.URISyntaxException: Illegal character in scheme name at index 0:

    正准备按照大佬的解决办法处理, 看会一条回复,说url有空格 检查了一下,还真是有空格 去除url中的空格,问题解除

  10. KOA 学习(九)koa-static

    配置静态资源的中间件 const Koa = require('koa'); const app = new Koa(); app.use(require('koa-static')(root, op ...