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

POJ 2104 K-th Number

时间:2015-08-16 22:59:15      阅读:180      评论:0      收藏:0      [点我收藏+]

标签:

K-th Number

Time Limit: 20000ms
Case Time Limit: 2000ms
Memory Limit: 65536KB
This problem will be judged on PKU. Original ID: 2104
64-bit integer IO format: %lld      Java class name: Main
 
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

Source

 
解题:解决这种问题其实最快的 划分树,
好吧,初学函数式线段树,水一题。。。
 
技术分享
 1 #include <iostream>
 2 #include <cstdio>
 3 #include <cstring>
 4 #include <algorithm>
 5 using namespace std;
 6 const int maxn = 100010;
 7 struct Node {
 8     int L,R,sum;
 9 } tree[maxn<<5];
10 struct A{
11     int x,id;
12     bool operator<(const A& rhs) const{
13         return x < rhs.x;
14     }
15 }a[maxn];
16 int root[maxn],rk[maxn],tot,n,m;
17 void update(int val,int &v,int L,int R) {
18     tree[tot] = tree[v];
19     v = tot++;
20     ++tree[v].sum;
21     if(L == R) return;
22     int mid = (L + R)>>1;
23     if(val <= mid) update(val,tree[v].L,L,mid);
24     else update(val,tree[v].R,mid+1,R);
25 }
26 int query(int i,int j,int k,int L,int R) {
27     if(L == R) return L;
28     int tmp = tree[tree[j].L].sum - tree[tree[i].L].sum;
29     int mid = (L + R)>>1;
30     if(k <= tmp) return query(tree[i].L,tree[j].L,k,L,mid);
31     else return query(tree[i].R,tree[j].R,k - tmp,mid+1,R);
32 }
33 int main() {
34     while(~scanf("%d%d",&n,&m)){
35         for(int i = 1; i <= n; ++i){
36             scanf("%d",&a[i].x);
37             a[i].id = i;
38         }
39         sort(a+1,a+1+n);
40         for(int i = 1; i <= n; ++i)
41             rk[a[i].id] = i;
42         tot = 1;
43         memset(tree,0,sizeof tree);
44         root[0] = 0;
45         for(int i = 1; i <= n; ++i){
46             root[i] = root[i-1];
47             update(rk[i],root[i],1,n);
48         }
49         while(m--){
50             int i,j,k;
51             scanf("%d%d%d",&i,&j,&k);
52             printf("%d\n",a[query(root[i-1],root[j],k,1,n)].x);
53         }
54     }
55     return 0;
56 }
View Code

 

POJ 2104 K-th Number

标签:

原文地址:http://www.cnblogs.com/crackpotisback/p/4734998.html

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