【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 }
				
时间: 2024-10-25 16:06:23

【POJ 2104】K-th Number的相关文章

【POJ 2104】 K-th Number 主席树模板题

达神主席树讲解传送门:http://blog.csdn.net/dad3zz/article/details/50638026 2016-02-23:真的是模板题诶,主席树模板水过.今天新校网不好,没有评测,但我立下flag这个代码一定能A.我的同学在自习课上考语文,然而机房党都跑到机房来避难了\(^o^)/~ #include<cstdio> #include<cstring> #include<algorithm> #define for1(i,a,n) for(i

【POJ 1019】 Number Sequence

[POJ 1019] Number Sequence 二分水题 放组合数学里...可能有什么正规姿势吧Orz 112123123412345...这种串 分成长度1 2 3 4 5...的串 注意有多位数 把长度累加到一个数组里 注意要累加 因为查询的时候查的是原串中对应位置的数 因此要累加上前一次的长度 然后二分处该串前的总长 用查询的位置-之前串的总长 就是在最长的串中的位置 因此还要打个最长串的表 这些我都写一个循环里了 看着有点乱 可以拆开写... 代码如下: #include <ios

【POJ 3254】 Corn Fields(状压DP)

[POJ 3254] Corn Fields(状压DP) Corn Fields Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 10891   Accepted: 5705 Description Farmer John has purchased a lush new rectangular pasture composed of M by N (1 ≤ M ≤ 12; 1 ≤ N ≤ 12) square parce

【POJ 3071】 Football(DP)

[POJ 3071] Football(DP) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4350   Accepted: 2222 Description Consider a single-elimination football tournament involving 2n teams, denoted 1, 2, -, 2n. In each round of the tournament, all tea

【POJ 3034】 Whac-a-Mole(DP)

[POJ 3034] Whac-a-Mole(DP) Time Limit: 2000MS   Memory Limit: 65536K Total Submissions: 3621   Accepted: 1070 Description While visiting a traveling fun fair you suddenly have an urge to break the high score in the Whac-a-Mole game. The goal of the W

【POJ 1151】 Atlantis(离散化+扫描线)

[POJ 1151] Atlantis(离散化+扫描线) Time Limit: 1000MS   Memory Limit: 10000K Total Submissions: 20223   Accepted: 7634 Description There are several ancient Greek texts that contain descriptions of the fabled island Atlantis. Some of these texts even inclu

【POJ 2411】Mondriaan&#39;s Dream(状压dp)

[POJ 2411]Mondriaan's Dream(状压dp) Time Limit: 3000MS   Memory Limit: 65536K Total Submissions: 14107   Accepted: 8152 Description Squares and rectangles fascinated the famous Dutch painter Piet Mondriaan. One night, after producing the drawings in hi

【POJ 1739】Tony&#39;s Tour

Tony's Tour Time Limit: 1000MS   Memory Limit: 30000K Total Submissions: 3545   Accepted: 1653 Description A square township has been divided up into n*m(n rows and m columns) square plots (1<=N,M<=8),some of them are blocked, others are unblocked.

【POJ 2486】 Apple Tree(树型dp)

[POJ 2486] Apple Tree(树型dp) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 8981   Accepted: 2990 Description Wshxzt is a lovely girl. She likes apple very much. One day HX takes her to an apple tree. There are N nodes in the tree. Each