POJ 2104 K-th Number【整体二分 + 树状数组】

题目链接：

http://poj.org/problem?id=2104

分析：

时间复杂度：

T(C,S)=T(C/2,S0+T(C/2,SS0))+O(f(s))

T(C,n)O(f(n)logC)

代码：

#include<cstdio>
#include<iostream>
#include<cstring>
using namespace std;
const int maxn = 1e5 + 5, maxm = 1e4 + 5, oo = 0x3f3f3f3f;
int n, m;
int cnt;
struct QUERY{
int x, y, k;
int id, type;
};
QUERY q[maxn + maxm], q1[maxn + maxm], q2[maxn + maxm];
int bit[maxn];
int ans[maxn];
{
while(i <= n){
bit[i] += x;
i += i &-i;
}
}
int sum(int i)
{
int res = 0;
while(i){
res += bit[i];
i -= i & -i;
}
return res;
}
void init()
{
memset(bit, 0, sizeof(bit));
cnt = 1;
}
void solve(int ql, int qr, int l, int r)
{
if(ql > qr) return;
if(l == r){
for(int i = ql; i <= qr; i++){
if(q[i].type == 2) ans[q[i].id] = l;
}
return ;
}
int m = l + r >> 1;
int p1 = 0, p2 = 0;
for(int i = ql; i <= qr; i++){
if(q[i].type == 1){
if(q[i].x <= m){
q1[p1++] = q[i];
}else q2[p2++] = q[i];
}else{
int res = sum(q[i].y) - sum(q[i].x - 1);
if(res >= q[i].k) q1[p1++] = q[i];
else{
q[i].k -= res;
q2[p2++] = q[i];
}
}
}
//清空标记
for(int i = 0; i < p1; i++){
}
for(int i = 0; i < p1; i++){
q[ql + i] = q1[i];
}
for(int i = 0; i < p2; i++){
q[ql + p1 + i] = q2[i];
}
solve(ql, ql + p1 - 1, l , m);
solve(ql + p1, qr, m + 1, r);
}
int main (void)
{
scanf("%d%d", &n, &m);
init();
int x, y, k;
for(int i = 0; i < n; i++){
scanf("%d", &x);
q[cnt++] = (QUERY){x, 1, oo, i + 1, 1};
}
for(int i = 0; i < m; i++){
scanf("%d%d%d", &x, &y, &k);
q[cnt++] = (QUERY){x, y, k, i + 1, 2};
}
solve(1, cnt - 1, -oo, oo);
for(int i = 1; i <= m; i++){
printf("%d\n", ans[i]);
}
return 0;
}