题目链接

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. Charles抓包(http/https请求)

    Charles安装 HTTP抓包 HTTPS抓包 1. Charles安装官网下载安装Charles:https://www.charlesproxy.com/download/当然由于国情可以使用破 ...

  2. GC 案例收集整理

    1.数组动态扩容  现象:系统一直在做cms gc,但是老生代一直不降下去,但是执行一次jmap -histo:live之后,也就是主动触发一次full gc之后,通过jstat -gcutil来看老 ...

  3. 02-认识js

    <!DOCTYPE html> <html lang="en"> <head> <meta charset="UTF-8&quo ...

  4. 提示microsoft incremental linker已停止工作解决方法

    解决方案一:项目->属性->链接器->常规 下面的“启用增量链接”,将“是(/INCREMENTAL)”改为“否(/INCREMENTAL:NO)”.不过这又引入了另外一个警 告:F ...

  5. DataLossError (see above for traceback): file is too short to be an sstable [[Node: save/RestoreV2 = RestoreV2[dtypes=[DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, DT_FLOAT, ..., DT_FLOAT, DT_FLOAT, DT_F

    DataLossError (see above for traceback): file is too short to be an sstable [[Node: save/RestoreV2 = ...

  6. Linux 启动dubbo管控台:

  7. Centos7解决在同一局域网内无法使用ssh连接

    参考: https://www.cnblogs.com/liyuanhong/articles/5785368.html 一.修改网卡设置 nano /etc/sysconfig/network-sc ...

  8. Android开发 Camera2开发_1_拍照功能开发

    介绍 google已经在Android5.1之后取消了对Camera1的更新,转而提供了功能更加强大的Camera2.虽然新版本依然可以使用Camera1但是,不管是各种机型适配还是拍照参数自定义都是 ...

  9. Gym 100712H

    Gym 100712Hhttps://vjudge.net/problem/195715/origin先缩点,再建立新图,然后跑两遍dfs求树上最长路 #include<iostream> ...

  10. 后缀数组(SA)及height数组

    最近感觉自己越来越蒟蒻了--后缀数组不会,费用流不会-- 看着别人切一道又一道的题,我真是很无奈啊-- 然后,我花了好长时间,终于弄懂了后缀数组. 后缀数组是什么? 后缀SASASA数组 给你一个字符 ...