题目链接

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. 模板——Treap

    不得不说平衡树博大精深,除了Treap,还有splay,非旋Treap和可持久化数据结构,今天先讲讲Treap,也很感谢这位大佬的博客给予我帮助:http://www.360doc.com/conte ...

  2. tensorflow的object detection的data augmention的使用

    在protoc的目录下有data augmention的提示,而且注意是repeated,也就是你要这样写: 不能写在一个data_aumentation_options下面,至于有哪些选项可以用,可 ...

  3. 732F Tourist Reform

    // CF 732F Tourist Reform // 思路:两遍tarjan // 找强联通分量 #include <bits/stdc++.h> using namespace st ...

  4. Luogu P2845 [USACO15DEC]Switching on the Lights 开关灯(bfs)

    P2845 [USACO15DEC]Switching on the Lights 开关灯 题意 题目背景 来源:usaco-2015-dec \(Farm\ John\)最近新建了一批巨大的牛棚.这 ...

  5. 数位DP入门题——[hdu2089]不要62

    数位DP是我的噩梦. 现在初三了,却没AC过数位DP的题目. 感觉数位DP都是毒瘤-- 题目 hdu不用登录也可以进去,所以就不把题目copy到这里来了. 题目大意 求区间[n,m][n,m][n,m ...

  6. python 连接mssql数据库

    1.目标数据sql2008 R2 ComPrject=>TestModel 2.安装python 连接mssql 模块 运行 pip install pymssql-2.2.0.dev0-cp3 ...

  7. SQLServer-SQLServer2017:安装 SQL Server 的硬件和软件要求

    ylbtech-SQLServer-SQLServer2017:安装 SQL Server 的硬件和软件要求 1.返回顶部 1. 安装 SQL Server 的硬件和软件要求 2018/11/06 适 ...

  8. 简单的选项卡制作(原生JS)

    <!doctype html> <html> <head> <meta charset="utf-8"> <title> ...

  9. <form>(表单)标签和常用的类型

    1.定义和用法 <form> 标签用于为用户输入创建 HTML 表单. 表单能够包含 input 元素,比如文本字段.复选框.单选框.提交按钮等等. 表单还可以包含 menus.texta ...

  10. WPF 禁用中文

    <TextBox InputMethod.IsInputMethodEnabled="False" />