# [POJ 2104] K-th Number【莫队+树状数组】

• 2018-01-28
• 0
• 0

## Problem:

 Time Limit: 20000MS Memory Limit: 65536K Case Time Limit: 2000MS

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```

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

## Code: O(nlogn+n1.5+mn0.5+mlogm+mlog2n) [2332K, 2485MS]

```#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<cmath>
#include<iostream>
#include<algorithm>
using namespace std;

inline void getint(int &num){
char ch; int sgn = 1;
while(!isdigit(ch = getchar())) if(ch == '-') sgn = -1;
num = ch - '0';
while(isdigit(ch = getchar())) num = (num << 1) + (num << 3) + ch - '0';
num *= sgn;
}

int n, m, a[100005], bl[100005];
int disc[100005], ans[5005];

struct Query{
int id, l, r, k;
} q[5005];

inline bool cmp(const Query &q1, const Query &q2){
if(bl[q1.l] == bl[q2.l]) return q1.r < q2.r;
return q1.l < q2.l;
}

struct BIT{
int node[100005];

inline void add(int u, int v) {while(u < n) node[u] += v, u += u & -u;}

inline int query(int u) {int res = 0; while(u) res += node[u], u -= u & -u; return res;}

inline int inv_rank(int k){
int l = 1, r = n;
while(l < r){
int mid = l + r >> 1;
if(query(mid) >= k) r = mid;
else l = mid + 1;
}
return l;
}
} b;

inline void update(int pos, int val) {b.add(a[pos], val);}

inline void Mo(){
sort(q + 1, q + m + 1, cmp);
for(register int i = 1, l = 1, r = 0; i <= m; i++){
while(r < q[i].r) update(++r, 1);
while(r > q[i].r) update(r--, -1);
while(l < q[i].l) update(l++, -1);
while(l > q[i].l) update(--l, 1);
ans[q[i].id] = disc[b.inv_rank(q[i].k)];  // Must take the original value
}
}

int main(){
getint(n), getint(m);
int blocksize = int(sqrt(n));
for(register int i = 1; i <= n; i++) bl[i] = (i - 1) / blocksize + 1;
for(register int i = 1; i <= n; i++) getint(a[i]), disc[i] = a[i];
for(register int i = 1; i <= m; i++)
getint(q[i].l), getint(q[i].r), getint(q[i].k), q[i].id = i;
sort(disc + 1, disc + n + 1);
int dsize = unique(disc + 1, disc + n + 1) - disc - 1;
for(register int i = 1; i <= n; i++)
a[i] = lower_bound(disc + 1, disc + dsize + 1, a[i]) - disc;
// Discretization
Mo();
for(register int i = 1; i <= m; i++) printf("%d\n", ans[i]);
return 0;
}
```

#### 评论

darkleafin.cf
(该域名已过期且被抢注。。)
darkleafin.github.io

49750

https://github.com/Darkleafin

OPEN AT 2017.12.10

Please refresh the page if the code cannot be displayed normally.

https://visualgo.net/en

- Theme by Qzhai