zoukankan      html  css  js  c++  java
  • Evanyou Blog 彩带

      题目传送门

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

      分析:  
      没错,这是主席树的模板,但是这里我们也可以用整体二分来做(shui)。
      关于整体二分,这个博主也才刚学,知识点什么的也不多讲,只放模板代码,关于知识点可以参考一下这个julao的博客。
      Code:
    //It is made by HolseLee on 5th Oct 2018
    //POJ2104
    #include<cstdio>
    #include<cstring>
    #include<iostream>
    #include<algorithm>
    #define inf 0x3f3f3f3f
    using namespace std;
    
    const int N=2e5+7;
    int n,m,ans[N],cnt,c[N];
    struct Node {
        int x,y,k,pos,type;
        Node() {}
        Node(const int _x,const int _y,const int _k,const int _p,const int _t):
            x(_x), y(_y), k(_k), pos(_p), type(_t) {}
    }q[N],q1[N],q2[N];
    
    inline int read()
    {
        char ch=getchar(); int num=0; bool flag=false;
        while( ch<'0' || ch>'9' ) {
            if( ch=='-' ) flag=true; ch=getchar();
        }    
        while( ch>='0' && ch<='9' ) {
            num=num*10+ch-'0'; ch=getchar();
        }
        return flag ? -num : num;
    }
    
    inline int lowbit(int x)
    {
        return x&(-x);
    }
    
    inline void add(int pos,int x)
    {
        for(; pos<=n; pos+=lowbit(pos)) c[pos]+=x;
    }
    
    inline int quary(int pos)
    {
        int ret=0;
        for(; pos>0; pos-=lowbit(pos)) ret+=c[pos];
        return ret;
    }
    
    void solve(int l,int r,int L,int R)
    {
        if( l>r || L>R ) return;
        if( l==r ) {
            for(int i=L; i<=R; ++i) 
            if( q[i].type ) ans[q[i].pos]=l;
            return ;
        }
        int mid=(l+r)>>1, cnt1=0, cnt2=0;
        for(int i=L; i<=R; ++i) 
        if( q[i].type ) {
            int tmp=quary(q[i].y)-quary(q[i].x-1);
            if( tmp>=q[i].k ) q1[++cnt1]=q[i];
            else q[i].k-=tmp, q2[++cnt2]=q[i];
        } else {
            if( q[i].x<=mid ) q1[++cnt1]=q[i], add(q[i].pos,1);
            else q2[++cnt2]=q[i];
        }
        for(int i=1; i<=cnt1; ++i) 
        if(!q1[i].type) add(q1[i].pos,-1);
        for(int i=1; i<=cnt1; ++i) q[L+i-1]=q1[i];
        for(int i=1; i<=cnt2; ++i) q[L+cnt1+i-1]=q2[i];
        solve(l,mid,L,L+cnt1-1), solve(mid+1,r,L+cnt1,R);
    }
    
    int main()
    {
        n=read(), m=read();
        int x,y,z;
        for(int i=1; i<=n; ++i) 
            x=read(), q[++cnt]=Node(x,1,0,i,0);
        for(int i=1; i<=m; ++i) {
            x=read(), y=read(), z=read();
            q[++cnt]=Node(x,y,z,i,1);
        }
        solve(-inf,inf,1,cnt);
        for(int i=1; i<=m; ++i) 
            printf("%d
    ",ans[i]);
        return 0;
    }
  • 相关阅读:
    20201019 day39 复习10:数据结构之树状数组、线段树
    20201019 day39 模拟(十二)&&复习9:贪心综合练习(一)
    静态路由的简易配置
    动态路由RIP的简易配置
    STP实验(指定特定交换机为根桥)
    结合以太通道的VLAN配置
    跨交换机划分VLAN配置及VTP管理交换机的VLAN配置
    单交换机划分VLAN配置
    linux常用命令集(文件系统权限操作-共5个)
    linux常用命令集(系统管理操作-共25个)
  • 原文地址:https://www.cnblogs.com/cytus/p/9744152.html
Copyright © 2011-2022 走看看