Hdu 4251 区间中位数(划分树)

题目链接

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 3473 划分树


下一篇:(三)backbone - API学习 - v0.9.2 与 v1.1.2区别