【POJ2104】【整体二分+树状数组】区间第k大

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
【分析】
比上一道题目还简单..
 /*
宋代郑思肖
《画菊》
花开不并百花丛,独立疏篱趣未穷。
宁可枝头抱香死,何曾吹落北风中。
*/
#include <iostream>
#include <cstdio>
#include <algorithm>
#include <cstring>
#include <vector>
#include <utility>
#include <iomanip>
#include <string>
#include <cmath>
#include <queue>
#include <assert.h>
#include <map>
#include <ctime>
#include <cstdlib>
#include <stack>
#define LOCAL
const int MAXN = + ;
const int MAXM = + ;
const int INF = 0x7fffffff;
const int SIZE = ;
const int maxnode = + ;
using namespace std;
typedef long long ll;
using namespace std;
struct DATA{
int val, x;//x代表位置
bool operator < (const DATA &b)const{
return val < b.val;
}
}data[MAXN];
struct QUESTION{
int l, r;
int k;
}q[MAXM];
int c[MAXN], id[MAXN], Ans[MAXN];
int tmp[MAXN];
bool mark[MAXN];
int Max = -INF, Min = INF, pos, n, m;
//树状数组
inline int lowbit(int x){return x&-x;}
int sum(int x){
int tmp = ;
while (x > ){
tmp += c[x];
x -= lowbit(x);
}
return tmp;
}
void add(int x, int val){
while (x <= n){
c[x] += val;
x += lowbit(x);
}
return;
} void init(){
memset(mark, , sizeof(mark));
memset(c, , sizeof(c));
scanf("%d%d", &n, &m);
for (int i = ; i <= n; i++){
scanf("%d", &data[i].val);
data[i].x = i;
Max = max(Max, data[i].val);
Min = min(Min, data[i].val);
}
//排序
sort(data + , data + + n);
}
void solve(int l, int r, int L, int R){
if (l > r || L == R) return;
int mid = (L + R) >> ;
while (data[pos + ].val <= mid && pos < n){
add(data[pos + ].x, );
pos++;
}
while (data[pos].val > mid){
add(data[pos].x, -);
pos--;
}
int cnt = ;//记录找到答案的回答个数
for (int i = l; i <= r; i++){
if (sum(q[id[i]].r) - sum(q[id[i]].l - ) > q[id[i]].k - ){
Ans[id[i]] = mid;
mark[i] = ;
cnt++;
}else mark[i] = ;
}
int l1 = l, l2 = l + cnt;
for (int i = l; i <= r; i++)
if (mark[i]) tmp[l1++] = id[i];
else tmp[l2++] = id[i]; for (int i = l; i <= r; i++) id[i] = tmp[i];
solve(l, l1 - , L, mid);
solve(l1, l2 - , mid + , R);
}
void work(){
pos = ;//初始化即data中的下标
for (int i = ; i <= m; i++){
scanf("%d%d%d", &q[i].l, &q[i].r, &q[i].k);
}
for (int i = ; i <= m; i++) id[i] = i;
solve(, m, Min, Max + );
for (int i = ; i <= m; i++) printf("%d\n", Ans[i]);
} int main(){ init();
work();
return ;
}
上一篇:sublime text3编译运行C,Java程序的一些配置


下一篇:ROWID面试题-删除表中重复数据(重复数据保留一个)