码迷,mamicode.com
首页 > 其他好文 > 详细

POJ 2104 K-th Number (划分树)

时间:2017-02-03 20:49:50      阅读:194      评论:0      收藏:0      [点我收藏+]

标签:blog   style   enter   represent   nbsp   lsp   include   typedef   code   

                                                            K-th Number
Time Limit: 20000MS   Memory Limit: 65536K
Total Submissions: 52651   Accepted: 18091
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.
【分析】第一道划分树,懂了一丝,继续努力。。。
 
#include <iostream>
#include <cstring>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <cmath>
#include <time.h>
#include <string>
#include <map>
#include <stack>
#include <vector>
#include <set>
#include <queue>
#define met(a,b) memset(a,b,sizeof a)
#define pb push_back
#define lson(x) ((x<<1))
#define rson(x) ((x<<1)+1)
using namespace std;
typedef long long ll;
const int N=1e5+50;
const int M=N*N+10;
struct P_Tree{
    int n,order[N];
    int valu[20][N],num[20][N];
    ll sum[N],lsum[20][N],isum;
    void build(int l,int r,int ind){
        if(l==r)return;
        int mid=(l+r)/2;
        int same=mid-l+1,ln=l,rn=mid+1;
        for(int i=l;i<=r;i++){
            int flag=0;
            if((valu[ind][i]<order[mid])||(valu[ind][i]==order[mid]&&same)){
                flag=1;
                valu[ind+1][ln++]=valu[ind][i];
                lsum[ind][i]=lsum[ind][i-1]+valu[ind][i];
                if(valu[ind][i]==order[mid])same--;
            }
            else {
                valu[ind+1][rn++]=valu[ind][i];
                lsum[ind][i]=lsum[ind][i-1]+valu[ind][i];
            }
            num[ind][i]=num[ind][i-1]+flag;
        }
        build(l,mid,ind+1);
        build(mid+1,r,ind+1);
    }
    void init(int len){
        n=len;sum[0]=0;
        for(int i=0;i<20;i++)valu[i][0]=num[i][0]=lsum[i][0]=0;
        for(int i=1;i<=n;i++){
            scanf("%d",&order[i]);
            valu[0][i]=order[i];
            sum[i]=sum[i-1]+order[i];
        }
        sort(order+1,order+n+1);
        build(1,n,0);
    }
    int query(int s,int t,int k,int l,int r,int ind){
        if(l==r)return valu[ind][l];
        int mid=(l+r)/2;
        int lx=num[ind][s-1]-num[ind][l-1];
        int ly=num[ind][t]-num[ind][s-1];
        int rx=s-1-l+1-lx;
        int ry=t-s+1-ly;
        if(ly>=k)return query(l+lx,l+lx+ly-1,k,l,mid,ind+1);
        else {
            isum+=lsum[ind][t]-lsum[ind][s-1];
            s=mid+1+rx;
            t=mid+1+rx+ry-1;
            return query(s,t,k-ly,mid+1,r,ind+1);
        }
    }
}tree;
int main() {
    int n,m;
    while(~scanf("%d%d",&n,&m)){
        tree.init(n);
        while(m--){
            int u,v,w;
            scanf("%d%d%d",&u,&v,&w);
            printf("%d\n",tree.query(u,v,w,1,n,0));
        }
    }
}

 

POJ 2104 K-th Number (划分树)

标签:blog   style   enter   represent   nbsp   lsp   include   typedef   code   

原文地址:http://www.cnblogs.com/jianrenfang/p/6363393.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!