码迷,mamicode.com
首页 > 其他好文 > 详细

【poj2104】K-th Number

时间:2017-01-18 14:09:22      阅读:255      评论:0      收藏:0      [点我收藏+]

标签:roo   线段   模板   lin   soft   size   this   previous   void   

题目描述

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.

输入

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).

输出

For each question output the answer to it --- the k-th number in sorted a[i...j] segment.

样例输入

7 3

1 5 2 6 3 7 4

2 5 3

4 4 1

1 7 3

样例输出

5

6

3


题目大意

给你n个数和m组询问,对于每组询问输出区间[i,j]中第k大的数是多少。

题解

主席树模板题。

先将数据离散化,然后加入到n棵动态开点线段树中。

由于每次只修改一个值,所以其余的子节点可以直接继承上一个子节点。

查询时,直接作差即可。

 1 #include <cstdio>
 2 #include <algorithm>
 3 using namespace std;
 4 struct data
 5 {
 6     int num , pos;
 7 }a[100010];
 8 int lp[4000010] , rp[4000010] , sum[4000010] , root[100010] , val[100010] , tot , cnt;
 9 bool cmp1(data a , data b)
10 {
11     return a.num < b.num;
12 }
13 bool cmp2(data a , data b)
14 {
15     return a.pos < b.pos;
16 }
17 void pushup(int x)
18 {
19     sum[x] = sum[lp[x]] + sum[rp[x]];
20 }
21 void ins(int &x , int &y , int l , int r , int p)
22 {
23     y = ++tot;
24     if(l == r)
25     {
26         sum[y] = sum[x] + 1;
27         return;
28     }
29     int mid = (l + r) >> 1;
30     if(p <= mid) rp[y] = rp[x] , ins(lp[x] , lp[y] , l , mid , p);
31     else lp[y] = lp[x] , ins(rp[x] , rp[y] , mid + 1 , r , p);
32     pushup(y);
33 }
34 int query(int x , int y , int l , int r , int p)
35 {
36     if(l == r) return val[l];
37     int mid = (l + r) >> 1;
38     if(sum[lp[y]] - sum[lp[x]] >= p) return query(lp[x] , lp[y] , l , mid , p);
39     else return query(rp[x] , rp[y] , mid + 1 , r , p - sum[lp[y]] + sum[lp[x]]);
40 }
41 int main()
42 {
43     int n , m , i , x , y , z;
44     scanf("%d%d" , &n , &m);
45     for(i = 1 ; i <= n ; i ++ )
46         scanf("%d" , &a[i].num) , a[i].pos = i;
47     sort(a + 1 , a + n + 1 , cmp1);
48     val[0] = 0x80000000;
49     for(i = 1 ; i <= n ; i ++ )
50     {
51         if(a[i].num > val[cnt]) val[++cnt] = a[i].num;
52         a[i].num = cnt;
53     }
54     sort(a + 1 , a + n + 1 , cmp2);
55     for(i = 1 ; i <= n ; i ++ )
56         ins(root[i - 1] , root[i] , 1 , cnt , a[i].num);
57     for(i = 1 ; i <= m ; i ++ )
58     {
59         scanf("%d%d%d" , &x , &y , &z);
60         printf("%d\n" , query(root[x - 1] , root[y] , 1 , cnt , z));
61     }
62 }

 

【poj2104】K-th Number

标签:roo   线段   模板   lin   soft   size   this   previous   void   

原文地址:http://www.cnblogs.com/GXZlegend/p/6296381.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!