题目链接

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. Elasticsearch template学习

    Elasticsearch template Elasticsearch存在一个关键问题就是索引的设置及字段的属性指定,最常见的问题就是,某个字段我们并不希望ES对其进行分词,但如果使用自动模板创建索 ...

  2. kafka分析

    目录 1,kafka简介 2, Kafka Server 2.1,kafka中zookeeper的作用 2.2, Broker 2.2.1,Broker高性能设计 2.2.2,Broker选举机制 2 ...

  3. HttpException (0x80004005): 无法连接到 SQL Server 会话数据库

    ASP.NET 项目运行时出现错误提示:[HttpException (0x80004005): 无法连接到 SQL Server 会话数据库.] ,后排查问题发现是由于项目的Session模式是使用 ...

  4. Nodejs之路(二)—— Nodejs再入门

    一.在Node中使用模板引擎 js代码: // art-template不仅可以在浏览器使用,也可以在node中使用 // 1.安装 npm install art-temlate模板引擎 // 2. ...

  5. hammer.js使用

    手势包括点击(tap),长按(press),滑动(swipe),方向(pan) 使用实例: <!DOCTYPE html> <html> <head> <me ...

  6. php循环语句(一)

    PHP 循环语句 什么是循环语句? 在不少实际问题中有许多具有规律性的重复操作,因此在程序中就需要重复执行某些语句.一组被重复执行的语句称之为循环体,能否继续重复,决定循环的终止条件.循环结构是在一定 ...

  7. c语言一个例子对比php学习随记

    今天开始学写一些简单的C例子,发现很多和PHP不一样的地方,做个笔记. #include <stdio.h> int main(void) { int a,b; printf(" ...

  8. BottomNavigationView(底部导航)

    BottomNavigationView 很早之前就在 Material Design 中出现了,但是直到 Android Support Library 25 中才增加了 BottomNavigat ...

  9. Django之数据库连接与建模

    Django数据库链接(这里以Mysql为例) 需要准备 Django1.10 pip install django==1.10 -i https://pypi.tuna.tsinghua.edu.c ...

  10. DAO设计模式总结

    1.DAO(Data Access Object,数据访问对象),主要的功能是用于进行数据操作的,在程序的标准开发框架中属于数据层的操作. 数据开发结构流程: 资源层是数据库的操作层,里面可以进行各种 ...