Hdu 4251区間中位数(区分木)

15634 ワード

タイトルリンク
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个题目,全部是データ结果,什么セグメント树,归并树,划分树,并查集,快速选择...
ところで、この問題は区分木で作られています..1 A、問題は区間中位数を求め、区間k番目に大きい(R-L)/2を求めることを意味します.
Accepted Code:
 1 /*************************************************************************  2  > File Name: 4251.cpp  3  > Author: Stomach_ache  4  > Mail: [email protected]  5  > Created Time: 2014 08 02      23 02 48   6  > Propose:  7  ************************************************************************/

 8 

 9 #include <cmath>

10 #include <string>

11 #include <cstdio>

12 #include <fstream>

13 #include <cstring>

14 #include <iostream>

15 #include <algorithm>

16 using namespace std; 17 

18 const int maxn = 100002; 19 int n, m; 20 int a[maxn], nums[maxn]; 21 int toLeft[20][maxn]; 22 int tr[20][maxn]; 23 

24 void build(int d, int l, int r) { 25     int mid = (l + r) / 2; 26     int le = 0, ls = l, rs = mid + 1; 27     for (int i = mid; i >= l; i--) { 28           if (nums[i] == nums[mid]) le++; 29         else break; 30  } 31     for (int i = l; i <= r; i++) { 32           if (i == l) toLeft[d][i] = 0; 33         else toLeft[d][i] = toLeft[d][i-1]; 34         

35         if (tr[d][i] < nums[mid]) { 36               toLeft[d][i]++; 37             tr[d+1][ls++] = tr[d][i]; 38         } else if (tr[d][i] > nums[mid]) { 39               tr[d+1][rs++] = tr[d][i]; 40         } else { 41               if (le) { 42                   le--; 43                 toLeft[d][i]++; 44                 tr[d+1][ls++] = tr[d][i]; 45             } else { 46                   tr[d+1][rs++] = tr[d][i]; 47  } 48  } 49  } 50     if (l == r) return ; 51     build(d+1, l, mid); 52     build(d+1, mid+1, r); 53 } 54 

55 int query(int d, int l, int r, int ql, int qr, int k) { 56       if (l == r) return tr[d][l]; 57     int s = (ql == l ? 0 : toLeft[d][ql-1]); 58     int ss = toLeft[d][qr]; 59     int mid = (l + r) / 2; 60     if (ss - s >= k) return query(d+1, l, mid, l+s, l+ss-1, k); 61     else return query(d+1, mid+1, r, mid+1+(ql-l-s), mid+1+(qr-l-ss), k-(ss-s)); 62 } 63 

64 int main(void) { 65       int cas = 1; 66       while(~scanf("%d", &n)) { 67           memset(toLeft, 0, sizeof(toLeft)); 68         memset(tr, 0, sizeof(tr)); 69           for (int i = 1; i <= n; i++) { 70               scanf("%d", a + i); 71             nums[i] = a[i]; 72             tr[0][i] = a[i]; 73  } 74         sort(nums + 1, nums + n + 1); 75         build(0, 1, n); 76         printf("Case %d:
", cas++); 77 scanf("%d", &m); 78 while (m--) { 79 int l, r; 80 scanf("%d %d", &l, &r); 81 int ans = query(0, 1, n, l, r, (r-l)/2+1); 82 printf("%d
", ans); 83 } 84 } 85 86 return 0; 87 }