您的位置:首页 > 其它

划分树(模板)poj2104

2014-08-07 21:45 411 查看
Language:
Default

K-th Number

Time Limit: 20000MS

Memory Limit: 65536K

Total Submissions: 37013

Accepted: 11911

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


裸地划分树

#include<iostream>
#include<cstdio>
#include<cstring>
#include<vector>
#include<cmath>
#include<queue>
#include<stack>
#include<map>
#include<set>
#include<algorithm>
using namespace std;
const int maxn=100010;
int n,m;
int a[maxn];

struct P_TREE
{
    int n,order[maxn];
    int val[20][maxn],num[20][maxn];
    void init(int len)
    {
        n=len;
        for(int i=1;i<=n;i++)order[i]=a[i];
        for(int i=0;i<20;i++){val[i][0]=num[i][0]=0;}
        for(int i=1;i<=n;i++)val[0][i]=order[i];
        sort(order+1,order+1+n);
        build(0,1,n);
    }
    void build(int ind,int l,int r)
    {
        if(l==r)return ;
        int mid=(l+r)>>1;
        int ln=l,rn=mid+1,same=r-l+1;
        for(int i=l;i<=r;i++)
            if(val[ind][i]<order[mid])same--;
        for(int i=l;i<=r;i++)
        {
            int flag=0;
            if(val[ind][i]<order[mid]||(val[ind][i]==order[mid]&&same))
            {
                flag=1;
                val[ind+1][ln++]=val[ind][i];
                if(val[ind][i]==order[mid])same--;
            }
            else val[ind+1][rn++]=val[ind][i];
            num[ind][i]=num[ind][i-1]+flag;
        }
        build(ind+1,l,mid);
        build(ind+1,mid+1,r);
    }
    int query(int ind,int l,int r,int q1,int q2,int k)
    {
        if(k==0)return -1;
        if(q2-q1+1<k)return (1<<30);
        if(l==r)return val[ind][l];
        int mid=(l+r)>>1;
        int lx=num[ind][q1-1]-num[ind][l-1];
        int ly=num[ind][q2]-num[ind][q1-1];
        int rx=q1-1-l+1-lx;
        int ry=q2-q1+1-ly;
        if(ly>=k)return query(ind+1,l,mid,l+lx,l+lx+ly-1,k);
        else
        {
            int st=mid+1+rx;
            int en=mid+1+rx+ry-1;
            return query(ind+1,mid+1,r,st,en,k-ly);
        }
    }

}tree;

int main()
{
    while(scanf("%d%d",&n,&m)!=EOF)
    {
        for(int i=1;i<=n;i++)scanf("%d",&a[i]);
        tree.init(n);
        while(m--)
        {
            int x,y,k;
            scanf("%d%d%d",&x,&y,&k);
            int ans=tree.query(0,1,n,x,y,k);
            printf("%d\n",ans);
        }
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: