K-th Number
Time Limit: 20000MS | Memory Limit: 65536K | |
Total Submissions: 36371 | Accepted: 11661 | |
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.
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 10 9 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).
The second line contains n different integer numbers not exceeding 10 9 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
题意:给n个数m个询问,询问[l, r]区间排序后的第k个数是什么
划分树
#include <cstdio>
#include <iostream>
#include <vector>
#include <algorithm>
#include <cstring>
#include <string>
#include <map>
#include <cmath>
#include <queue>
#include <set>
using namespace std;
//#define WIN
#ifdef WIN
typedef __int64 LL;
#define iform "%I64d"
#define oform "%I64d\n"
#else
typedef long long LL;
#define iform "%lld"
#define oform "%lld\n"
#endif
#define S64I(a) scanf(iform, &(a))
#define P64I(a) printf(oform, (a))
#define REP(i, n) for(int (i)=0; (i)<n; (i)++)
#define REP1(i, n) for(int (i)=1; (i)<=(n); (i)++)
#define FOR(i, s, t) for(int (i)=(s); (i)<=(t); (i)++)
const int INF = 0x3f3f3f3f;
const double eps = 10e-9;
const double PI = (4.0*atan(1.0));
const int maxn = 100000 + 20;
struct P_Tree {
int n, order[maxn];
int val[20][maxn], num[20][maxn];
LL sum[maxn], lsum[20][maxn], isum;
void init(int * A, int len) {
n = len;
sum[0] = 0;
for(int i=0; i<20; i++) val[i][0] = 0, num[i][0] = 0, lsum[i][0] = 0;
for(int i=1; i<=n; i++) {
order[i] = A[i];
val[0][i] = order[i];
sum[i] = sum[i-1] + order[i];
}
sort(order+1, order+1+n);
build(0, 1, n);
}
void build(int dept, int L, int R) {
if(L == R) return ;
int M = L + (R-L) / 2;
int same = M - L + 1;
int ln = L;
int rn = M+1;
for(int i=L; i<=R; i++)
if(val[dept][i] < order[M]) same--;
for(int i=L; i<=R; i++) {
int flag = 0;
if(val[dept][i] < order[M] || (val[dept][i] == order[M] && same)) {
flag = 1;
val[dept+1][ln++] = val[dept][i];
lsum[dept][i] = lsum[dept][i-1] + val[dept][i];
if(val[dept][i] == order[M]) same--;
} else {
val[dept+1][rn++] = val[dept][i];
lsum[dept][i] = lsum[dept][i-1];
}
num[dept][i] = num[dept][i-1] + flag;
}
build(dept+1, L, M);
build(dept+1, M+1, R);
}
int query(int dept, int L, int R, int st, int ed, int k) {
if(L == R) return val[dept][L];
int M = L + (R-L) / 2;
int lx = num[dept][st-1] - num[dept][L-1]; // [L, st-1]划到左边的数量
int ly = num[dept][ed] - num[dept][st-1]; // [st, ed]划到左边的数量
int rx = st-1 - L + 1 - lx; // [L, st-1]划到右边的数量
int ry = ed - st + 1 - ly; // [st, ed]划到右边的数量
if(ly >= k) return query(dept+1, L, M, L+lx, L+lx+ly-1, k);
else {
isum += lsum[dept][ed] - lsum[dept][st-1];
st = M + 1 + rx;
ed = M + 1 + rx + ry - 1;
return query(dept+1, M+1, R, st, ed, k-ly);
}
}
};
P_Tree ptree;
int A[maxn];
int main() {
int n, m;
while(scanf("%d%d", &n, &m) != EOF) {
for(int i=1; i<=n; i++) {
scanf("%d", &A[i]);
}
ptree.init(A, n);
for(int i=0; i<m; i++) {
int st, ed, k;
scanf("%d%d%d", &st, &ed, &k);
int ret = ptree.query(0, 1, n, st, ed, k);
printf("%d\n", ret);
}
}
return 0;
}