K-th Number
You are working for Macrohard company in data structures department. After failing your previous task about key insertion you were asked to write a new data structure that would be able to return quickly k-th order statistics in the array segment.
That is, given an array a[1...n] of different integer numbers, your program must answer a series of questions Q(i, j, k) in the form: "What would be the k-th number in a[i...j] segment, if this segment was sorted?"
For example, consider the array a = (1, 5, 2, 6, 3, 7, 4). Let the question be Q(2, 5, 3). The segment a[2...5] is (5, 2, 6, 3). If we sort this segment, we get (2, 3, 5, 6), the third number is 5, and therefore the answer to the question is 5.
Input
The first line of the input file contains n --- the size of the array, and m --- the number of questions to answer (1 <= n <= 100 000, 1 <= m <= 5 000).
The second line contains n different integer numbers not exceeding 109 by their absolute values --- the array for which the answers should be given.
The following m lines contain question descriptions, each description consists of three numbers: i, j, and k (1 <= i <= j <= n, 1 <= k <= j - i + 1) and represents the question Q(i, j, k).
Output
For each question output the answer to it --- the k-th number in sorted a[i...j] segment.
Sample Input
7 3
1 5 2 6 3 7 4
2 5 3
4 4 1
1 7 3
Sample Output
5
6
3
Hint
This problem has huge input,so please use c-style input(scanf,printf),or you may got time limit exceed.
Source
Northeastern Europe 2004, Northern Subregion
题目类型:划分树
算法分析:划分树的简单应用
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 |
/************************************************* Author :supermaker Created Time :2016/1/9 23:59:36 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 tree[30][maxn];//表示每层每个位置的值 int sorted[maxn];//已经排序的数 int toleft[30][maxn];//toleft[p][i]表示第i层从1到i有多少个数分入左边 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;//从1到i放左边的个数 } build (dep + 1, l, mid); build (dep + 1, mid + 1, r); } //查询区间第k大的数,[L,R]是大区间,[l,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 cnt = toleft[dep][r] - toleft[dep][l-1];//[l,r]中位于左边的个数 if (cnt >= k) { //L+要查询的区间前被放在左边的个数 int newl = L + toleft[dep][l-1] - toleft[dep][L-1]; //左端点加上查询区间会被放在左边的个数 int newr = newl + cnt - 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 - cnt); return query (dep + 1, mid + 1, R, newl, newr, k - cnt); } } int main() { //CFF; //CPPFF; int n, q; while (scanf ("%d%d", &n, &q) != EOF) { for (int i = 1; i <= n; i++) { scanf ("%d", &tree[0][i]); sorted[i] = tree[0][i]; } sort (sorted + 1, sorted + 1 + n); build (0, 1, n); for (int i = 1; i <= q; i++) { int ll, rr, kk; scanf ("%d%d%d", &ll, &rr, &kk); printf ("%d\n", query (0, 1, n, ll, rr, kk)); } } return 0; } |
- hdu4251:下一篇 »