POJ2104 K-th Number 【歸併樹】
阿新 • • 發佈:2019-01-29
K-th Number
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.
題意:給定一個包含n個不同數的數列a1, a2, ..., an 和m個三元組表示的查詢。對於每個查詢(i, j, k), 輸出ai, ai+1, ... ,aj的升序排列中第k個數 。
Time Limit: 20000MS | Memory Limit: 65536K | |
Total Submissions: 38379 | Accepted: 12480 | |
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 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
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
題意:給定一個包含n個不同數的數列a1, a2, ..., an 和m個三元組表示的查詢。對於每個查詢(i, j, k), 輸出ai, ai+1, ... ,aj的升序排列中第k個數 。
題解:用線段樹,每個節點維護一個區間並且保證內部升序,對於每次查詢x,返回該區間小於x的數的個數。就這樣不斷二分,直到找到x為止。
#include <stdio.h>
#include <string.h>
#include <algorithm>
#include <vector>
#define maxn 100005
#define lson l, mid, rt << 1
#define rson mid + 1, r, rt << 1 | 1
using namespace std;
vector<int> T[maxn << 2];
int N, Q;
void build(int l, int r, int rt) {
if(l == r) {
int val;
scanf("%d", &val);
T[rt].push_back(val);
return;
}
int mid = (l + r) >> 1;
build(lson);
build(rson);
T[rt].resize(r - l + 1); // Attention
merge(T[rt<<1].begin(), T[rt<<1].end(), T[rt<<1|1].begin(), T[rt<<1|1].end(), T[rt].begin());
}
int query(int L, int R, int val, int l, int r, int rt) {
if(L == l && R == r) {
return upper_bound(T[rt].begin(), T[rt].end(), val) - T[rt].begin();
}
int mid = (l + r) >> 1;
if(R <= mid) return query(L, R, val, lson);
else if(L > mid) return query(L, R, val, rson);
return query(L, mid, val, lson) + query(mid + 1, R, val, rson);
}
int main() {
int a, b, c, k, left, right, mid;
scanf("%d%d", &N, &Q);
build(1, N, 1);
while(Q--) {
scanf("%d%d%d", &a, &b, &k);
left = -1; right = N - 1;
while(right - left > 1) { // binary search
mid = (left + right) >> 1;
c = query(a, b, T[1][mid], 1, N, 1);
if(c >= k) right = mid;
else left = mid;
}
printf("%d\n", T[1][right]);
}
return 0;
}