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

来源:互联网 发布:软件产品出口退税政策 编辑:程序博客网 时间:2024/05/20 21:49

转自zP1nG

传送门

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

整体二分:

整体二分就是将所有询问一起二分,然后获得每个询问的答案。
整体二分的过程实质上是个按照数值来划分操作序列的过程。
对每一个询问我们都需要判定一下,以决定它被划分到哪一个答案的区间里。这个判定过程就是通过比较比二分的mid大的数的个数和k。
以下摘自许昊然论文-《浅谈数据结构题的几个非经典解法》

询问的答案具有二分性显然是前提。我们发现,因为修改判定标准的贡献相互独立,且贡献的值(如果有的话)与判定标准无关,所以如果我们已经计算过某一些修改对询问的贡献,那么这个贡献将永远不会改变,我们没有必要当判定标准改变时再次计算这些部分修改的贡献,只要记录下当前的总贡献,在进一步二分时,直接加上新的贡献即可。 

这样处理的复杂度可以不再与序列总长度直接相关了,而可以只与当前处理的序列的长度相关。

时间复杂度:

定义T(C,S)表示待二分区间长度为C,待二分序列长度为S,不妨设单次处理时间复杂度O(f(n)),则有

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

解之得
T(C,n)O(f(n)logC)

思路

首先考虑一次询问的情况,我们可以二分答案,然后通过验证比答案大的数有多少个来不断地缩小答案范围直至得到一个准确的答案。而对于多个询问我们同样可以这么做,只不过对每一个询问我们都需要判定一下,以决定它被划分到哪一个答案的区间里。这个判定过程就是通过比较比mid大的数的个数和k。同时如果比二分的mid大的数的个数小于k了,我们是要去寻找小的答案,那么这些比mid大的数在以后的递归里始终会对答案有贡献,所以我们没必要去做重复的工作,只需要把这些数的个数累积到贡献里,以后递归的时候就不用考虑这些数。我们可以把数列里的数也和询问一起递归,这样这些数也会被分到属于的答案区间里,并且只对相应区间里的询问有影响。
整体二分的过程实质上是个按照数值来划分操作序列的过程,于是复杂度也就和操作序列的长度线性相关,那么我们在中间维护一些信息的时候,就一定不能有和数列长线性相关的东西,否则会破坏其时间复杂度。
具体的复杂度证明请见2013年集训队XHR论文。

代码

#include <bits/stdc++.h>#define lowbit(x) (x&(-x))#define INF 0x3f3f3f3f#define N 100005#define M 5005using namespace std;int n,m,pos;int Max=-INF,Min=INF;int id[N],ans[N],tmp[N];bool mark[N];struct DATA{    int x,v;    bool operator < (const DATA&r)const{return v<r.v;}}data[N];struct Ques{    int l,r,k;}q[M];int tree[N];inline void add(int x,int num){    while(x<=n){        tree[x]+=num;        x+=lowbit(x);    }}inline int search(int x){    int re=0;    while(x){        re+=tree[x];        x-=lowbit(x);    }    return re;}void solve(int l,int r,int L,int R){    if(l>r || L==R)     return;    int mid=(L+R)>>1;    while(data[pos+1].v<=mid && pos<n){        add(data[pos+1].x,1);        ++pos;    }    while(data[pos].v>mid){        add(data[pos].x,-1);        --pos;    }    int cnt=0;    for(int i=l;i<=r;++i){        if(search(q[id[i]].r)-search(q[id[i]].l-1)>q[id[i]].k-1){            ans[id[i]]=mid;            mark[i]=1;            ++cnt;        }        else    mark[i]=0;    }    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-1,L,mid);    solve(l1,l2-1,mid+1,R);}int main(){    scanf("%d%d",&n,&m);    for(int i=1;i<=n;++i){        scanf("%d",&data[i].v);        data[i].x=i;        Max=max(Max,data[i].v);        Min=min(Min,data[i].v);    }    sort(data+1,data+n+1);    for(int i=1;i<=m;++i)       scanf("%d%d%d",&q[i].l,&q[i].r,&q[i].k);    for(int i=1;i<=m;++i)       id[i]=i;    solve(1,m,Min,Max+1);    for(int i=1;i<=m;++i)       printf("%d\n",ans[i]);    return 0;}