poj 2104 K-th Number[整体二分]

来源:互联网 发布:添加的sql语句怎么写 编辑:程序博客网 时间:2024/06/10 08:01

K-th Number
Time Limit: 20000MS Memory Limit: 65536K
Total Submissions: 57079 Accepted: 19712
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.
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


【分析】

网上的二笔题解…无脑在题目上写个第K大的标签,结果这题是第k小,写了一遍无脑交上去就WA…

整体二分。讲解参见bzoj 2738 矩阵乘法(这题也是整体二分蛤,与题目没半毛钱关系…)

总体上讲整体二分可以取代一些码农数据结构,写起来常数小思维难度一般来说不是很大…所以能用CDQ或者整体二分就不要用奇葩数据结构了(虽然树套树什么的我一无所知不得已只能用CDQ)


【代码】

//poj 2104 K-th number#include<iostream>#include<cstring>#include<cstdio>#include<algorithm> #define inf 1e9#define ll long long#define M(a) memset(a,0,sizeof a)#define fo(i,j,k) for(i=j;i<=k;i++)using namespace std;const int mxn=100005;int n,m,now;int c[mxn],ans[mxn];struct node {int v,pos;} a[mxn];struct query {int l,r,k,id;} q[mxn],tmp[mxn];inline bool comp(node x,node y){    return x.v<y.v;}inline int lowbit(int x) {return x&-x;}inline void add(int u,int v){    for(int i=u;i<=n;i+=lowbit(i))      c[i]+=v;}inline int getsum(int u){    int sum=0;    for(int i=u;i>=1;i-=lowbit(i))      sum+=c[i];    return sum;}inline void CDQ(int L,int R,int l,int r){//  printf("%d %d %d %d\n",L,R,l,r);    if(L>R) return;    int i,j,l1=L,l2=R,mid=(l+r>>1)+1;    if(l==r)    {        fo(i,L,R) ans[q[i].id]=l;        return;    }    while((!now || a[now].v<mid) && now<n) ++now,add(a[now].pos,1);    while(now && a[now].v>=mid) add(a[now].pos,-1),--now;    fo(i,L,R)    {        int sum=getsum(q[i].r)-getsum(q[i].l-1);        if(sum>=q[i].k) tmp[l1++]=q[i];        else tmp[l2--]=q[i];    }    reverse(tmp+l2+1,tmp+R+1);    fo(i,L,R) q[i]=tmp[i];    CDQ(L,l1-1,l,mid-1);    CDQ(l2+1,R,mid,r);}int main(){    int i,j;    scanf("%d%d",&n,&m);    fo(i,1,n)    {        a[i].pos=i;        scanf("%d",&a[i].v);    }    sort(a+1,a+n+1,comp);    fo(i,1,m)    {        q[i].id=i;        scanf("%d%d%d",&q[i].l,&q[i].r,&q[i].k);    }    CDQ(1,m,-inf,inf);    fo(i,1,m) printf("%d\n",ans[i]);    return 0;}