【POJ 2104】K-th Number

Description

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

 

题目大意:

  求区间第k小。

 

分析:

  可持久化线段树,按序列中的数的顺序来创建新版本,每个数在线段树中所在的位置为数在所有数中第几小。

 

代码:

 1 #include <cstdio>
 2 #include <algorithm>
 3 
 4 const int Max_N = 2000000;
 5 
 6 struct SegmentTree
 7 {
 8     int Child[2];
 9     int Count, Number;
10 } tree[Max_N];
11 
12 int Root[Max_N], treeSize;
13 int num[Max_N], id[Max_N], back[Max_N];
14 int n, m, qi, qj, qk;
15 
16 #define MID ((left + right) >> 1)
17 
18 int Build (int left, int right)
19 {
20     int i = ++treeSize;
21     tree[i].Count = 0;
22     if (left < right)
23     {
24         tree[i].Child[0] = Build (left, MID);
25         tree[i].Child[1] = Build (MID + 1, right);
26     }
27     return i;
28 }
29 
30 int Modify (int left, int right, int pos, int key, int pre)
31 {
32     int i = ++treeSize;
33     if (left < right)
34     {
35         int ch = (pos <= MID) ? 0 : 1;
36         tree[i].Child[!ch] = tree[pre].Child[!ch];
37         ch ? left = MID + 1 : right = MID;
38         tree[i].Child[ch] = Modify (left, right, pos, key, tree[pre].Child[ch]);
39         tree[i].Count = tree[tree[i].Child[0]].Count + tree[tree[i].Child[1]].Count;
40     }else tree[i].Number = key, tree[i].Count = 1;
41     return i;
42 }
43 
44 #define LEFTSIZE (tree[tree[late].Child[0]].Count - tree[tree[early].Child[0]].Count)
45 
46 int Query (int left, int right, int early, int late, int k)
47 {
48     if (left == right) return tree[late].Number;
49     int ch = (k <= LEFTSIZE) ? 0 : 1;
50     ch ? left = MID + 1 : right = MID;
51     return Query (left, right, tree[early].Child[ch], tree[late].Child[ch], ch ? k - LEFTSIZE : k);
52 }
53 
54 bool cmp (int a, int b)
55 {
56     return num[a] < num[b];
57 }
58 
59 int main ()
60 {
61     scanf ("%d %d", &n, &m);
62     for (int i = 1; i <= n; i++)
63         scanf ("%d", &num[i]), id[i] = i;
64     std::sort (id + 1, id + n + 1, cmp);
65     Root[0] = Build (1, n);
66     for (int i = 1; i <= n; i++)
67         back[id[i]] = i;
68     for (int i = 1; i <= n; i++)
69         Root[i] = Modify (1, n, back[i], num[i], Root[i - 1]);
70     for (int i = 0; i < m; i++)
71     {
72         scanf ("%d %d %d", &qi, &qj, &qk);
73         printf ("%d\n", Query (1, n, Root[qi - 1], Root[qj], qk));
74     }
75 }

 


posted @ 2015-04-02 20:06  Lightning34  阅读(129)  评论(0编辑  收藏  举报