• K-th Number


    K-th Number
    Time Limit: 20000MS   Memory Limit: 65536K
         
    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.
    分析:整体二分入门题;
       与主席树相比,内存小,时间复杂度多一个log;
    代码:
    #include <iostream>
    #include <cstdio>
    #include <cstdlib>
    #include <cmath>
    #include <algorithm>
    #include <climits>
    #include <cstring>
    #include <string>
    #include <set>
    #include <bitset>
    #include <map>
    #include <queue>
    #include <stack>
    #include <vector>
    #include <cassert>
    #include <ctime>
    #define rep(i,m,n) for(i=m;i<=(int)n;i++)
    #define inf 0x3f3f3f3f
    #define mod 1000000007
    #define vi vector<int>
    #define pb push_back
    #define mp make_pair
    #define fi first
    #define se second
    #define ll long long
    #define pi acos(-1.0)
    #define pii pair<int,int>
    #define sys system("pause")
    #define ls (rt<<1)
    #define rs (rt<<1|1)
    #define all(x) x.begin(),x.end()
    const int maxn=2e5+10;
    const int N=2e5+10;
    using namespace std;
    ll gcd(ll p,ll q){return q==0?p:gcd(q,p%q);}
    ll qmul(ll p,ll q,ll mo){ll f=0;while(q){if(q&1)f=(f+p)%mo;p=(p+p)%mo;q>>=1;}return f;}
    ll qpow(ll p,ll q,ll mo){ll f=1;while(q){if(q&1)f=f*p%mo;p=p*p%mo;q>>=1;}return f;}
    int n,m,k,t,ret[maxn],d[maxn],cnt;
    void add(int x,int y)
    {
        while(x<=n)d[x]+=y,x+=x&(-x);
    }
    int get(int x)
    {
        int ret=0;
        while(x)ret+=d[x],x-=x&(-x);
        return ret;
    }
    struct node
    {
        int x,y,z,id;
    }qu[maxn],ql[maxn],qr[maxn];
    void solve(int L,int R,int l,int r)
    {
        if(L>R)return;
        int i,j;
        if(l==r)
        {
            rep(i,L,R)
            {
                if(qu[i].id>0)ret[qu[i].id]=l;
            }
            return;
        }
        int mid=l+r>>1;
        int tot1=0,tot2=0;
        rep(i,L,R)
        {
            if(qu[i].id<0)
            {
                if(qu[i].z<=mid)
                {
                    add(-qu[i].id,1);
                    ql[tot1++]=qu[i];
                }
                else qr[tot2++]=qu[i];
            }
            else
            {
                int num=get(qu[i].y)-get(qu[i].x-1);
                if(num>=qu[i].z)
                {
                    ql[tot1++]=qu[i];
                }
                else
                {
                    qu[i].z-=num;
                    qr[tot2++]=qu[i];
                }
            }
        }
        rep(i,0,tot1-1)
        {
            qu[L+i]=ql[i];
            if(ql[i].id<0)add(-ql[i].id,-1);
        }
        rep(i,0,tot2-1)qu[L+tot1+i]=qr[i];
        solve(L,L+tot1-1,l,mid);
        solve(L+tot1,R,mid+1,r);
    }
    int main(){
        int i,j;
        scanf("%d%d",&n,&m);
        rep(i,1,n)
        {
            scanf("%d",&j);
            qu[++cnt]=node{0,0,j,-i};
        }
        rep(i,1,m)
        {
            int x,y,z;
            scanf("%d%d%d",&x,&y,&z);
            qu[++cnt]=node{x,y,z,i};
        }
        solve(1,cnt,-1e9,1e9);
        rep(i,1,m)printf("%d
    ",ret[i]);
        return 0;
    }
  • 相关阅读:
    WeChat-SmallProgram:组件 scroll-view 横向和纵向 案例
    Codeforces Round #277 (Div. 2) D. Valid Sets 树形DP
    Codeforces Round #243 (Div. 2) E. Sereja and Two Sequences DP
    Codeforces Round #263 (Div. 2) D. Appleman and Tree 树形dp
    Codeforces Round #259 (Div. 2) D. Little Pony and Harmony Chest 状压DP
    Codeforces Round #274 (Div. 2) E. Riding in a Lift DP
    HDOJ 6069 素数筛
    在平面中,一个点绕任意点旋转θ度后的点的坐标
    HDOJ 5724 博弈SG函数
    Codeforces Round #422 (Div. 2) C. Hacker, pack your bags! 排序+贪心
  • 原文地址:https://www.cnblogs.com/dyzll/p/7478530.html
Copyright © 2020-2023  润新知