K-th Number
Time Limit: 20000MS | Memory Limit: 65536K | |
Total Submissions: 34935 | Accepted: 11134 | |
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 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).
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
划分树
AC代码:
#include<iostream>
#include<cstdio>
#include<algorithm>
#define MAX 100005
using namespace std;
class TreeNode
{
public:
int left;
int right;
int mid;
};
int ToLeft[][MAX];
int val[][MAX];
TreeNode node[*MAX];
int sorted[MAX];
void BuildTree(int k, int d, int l, int r)
{
node[k].left = l;
node[k].right = r;
node[k].mid = (l + r) >> ;
int mid = (l + r) >> ;
if(l == r)
return ;
int lsame = mid - l + ;
for(int i = l;i <= r; i ++)
{
if(val[d][i] < sorted[mid])
lsame --;
}
int lpos = l;
int rpos = mid + ;
for(int i = l;i <= r;i ++)
{
if(i == l)
ToLeft[d][i] == ;
else
ToLeft[d][i] = ToLeft[d][i-];
if(val[d][i] < sorted[mid])
{
ToLeft[d][i] ++;
val[d+][lpos++] = val[d][i];
}
else if(val[d][i] > sorted[mid])
val[d+][rpos++] = val[d][i];
else
{
if(lsame)
{
ToLeft[d][i] ++;
val[d+][lpos++] = val[d][i];
lsame --;
}
else
val[d+][rpos++] = val[d][i];
}
}
BuildTree(k << , d + , l, mid);
BuildTree(k << |, d+, mid + , r);
} int Query(int l, int r, int k, int d, int idx)
{
if(l == r)
return val[d][l];
int s;
int ss;
if(node[idx].left == l)
{
s = ToLeft[d][r];
ss = ;
}
else
{
s = ToLeft[d][r] - ToLeft[d][l-];
ss = ToLeft[d][l-];
}
if(s >= k)
{
int newl = node[idx].left + ss;
int newr = node[idx].left + ss + s - ;
return Query(newl, newr, k, d + , idx << );
}
else
{
int bb = l - node[idx].left - ss;
int b = r- l - s + ;
int newl = node[idx].mid + bb + ;
int newr = node[idx].mid + bb + b;
return Query(newl, newr, k - s, d + , idx << |);
}
} int main(int argc, char const *argv[])
{
int n, m;
int l, r, k;
//freopen("in.c", "r", stdin);
while(~scanf("%d%d", &n, &m))
{
for(int i = ;i <= n;i ++)
{
scanf("%d", &val[][i]);
sorted[i] = val[][i];
}
sort(sorted+, sorted+n+);
BuildTree(, , , n);
for(int i = ;i < m;i ++)
{
scanf("%d%d%d", &l, &r, &k);
printf("%d\n", Query(l, r, k, , ));
}
}
return ;
}