The Famous ICPC Team Again
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
Source
Fudan Local Programming Contest 2012
题目类型:划分树
算法分析:划分树的简单应用,每次查询的是区间第(区间元素个数tot+1) / 2大的元素
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 |
/************************************************* Author :supermaker Created Time :2016/1/14 23:07:29 File Location :C:\Users\abcd\Desktop\TheEternalPoet **************************************************/ #pragma comment(linker, "/STACK:102400000,102400000") #include <set> #include <bitset> #include <list> #include <map> #include <stack> #include <queue> #include <deque> #include <string> #include <vector> #include <ios> #include <iostream> #include <fstream> #include <sstream> #include <iomanip> #include <algorithm> #include <utility> #include <complex> #include <numeric> #include <functional> #include <cmath> #include <ctime> #include <climits> #include <cstdarg> #include <cstdio> #include <cstdlib> #include <cstring> #include <cctype> #include <cassert> using namespace std; #define CFF freopen ("aaa.txt", "r", stdin) #define CPPFF ifstream cin ("aaa.txt") #define DB(ccc) cout << #ccc << " = " << ccc << endl #define PB push_back #define MP(A, B) make_pair(A, B) typedef long long LL; typedef unsigned long long ULL; typedef double DB; typedef pair <int, int> PII; typedef pair <int, bool> PIB; const int INF = 0x7F7F7F7F; const int MOD = 1e9 + 7; const double EPS = 1e-10; const double PI = 2 * acos (0.0); const int maxn = 1e5 + 6666; int sorted[maxn], tree[30][maxn], toleft[30][maxn]; void build (int dep, int l, int r) { if (l == r) return ; int mid = (l + r) >> 1; int same = mid - l + 1; for (int i = l; i <= r; i++) if (tree[dep][i] < sorted[mid]) same--; int lpos = l, rpos = mid + 1; for (int i = l; i <= r; i++) { if (tree[dep][i] < sorted[mid]) tree[dep+1][lpos++] = tree[dep][i]; else if (tree[dep][i] == sorted[mid] && same > 0) { tree[dep+1][lpos++] = tree[dep][i]; same--; } else tree[dep+1][rpos++] = tree[dep][i]; toleft[dep][i] = toleft[dep][l-1] + (lpos - l); } build (dep + 1, l, mid); build (dep + 1, mid + 1, r); } int query (int dep, int L, int R, int l, int r, int k) { if (l == r) return tree[dep][l]; int mid = (L + R) >> 1; int tot = toleft[dep][r] - toleft[dep][l-1]; if (tot >= k) { int newl = L + toleft[dep][l-1] - toleft[dep][L-1]; int newr = newl + tot - 1; return query (dep + 1, L, mid, newl, newr, k); } else { int newr = r + toleft[dep][R] - toleft[dep][r]; int newl = newr - (r - l - tot); return query (dep + 1, mid + 1, R, newl, newr, k - tot); } } int main() { //CFF; //CPPFF; int n, flag = 1; while (scanf ("%d", &n) != EOF) { memset (tree, 0, sizeof (tree)); memset (toleft, 0, sizeof (toleft)); for (int i = 1; i <= n; i++) { scanf ("%d", &tree[0][i]); sorted[i] = tree[0][i]; } sort (sorted + 1, sorted + 1 + n); printf ("Case %d:\n", flag++); build (0, 1, n); int q; scanf ("%d", &q); for (int i = 1; i <= q; i++) { int u, v; scanf ("%d%d", &u, &v); int tot = v - u + 1; tot = (tot + 1) / 2; printf ("%d\n", query (0, 1, n, u, v, tot)); } } return 0; } |