• AC日记——K-th Number poj 2104


    K-th Number
    Time Limit: 20000MS   Memory Limit: 65536K
    Total Submissions: 52348   Accepted: 17985
    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
     
     
    思路:
      可持久化线段树模板;
     
    来,上代码:
      结构体数组版(ac):
    #include <cstdio>
    #include <algorithm>
    
    using namespace std;
    
    struct TreeNode {
        int l,r,dis;
    };
    
    class PersistentLineSegmentTree {
        private:
            int n,m,if_z,tot,num[100001],num_[100001],size;
            
            char Cget;
            
            TreeNode root[100001],node[100001*20];
            
            inline void read_int(int &now_)
            {
                now_=0,if_z=1,Cget=getchar();
                while(Cget>'9'||Cget<'0')
                {
                    if(Cget=='-') if_z=-1;
                    Cget=getchar();
                }
                while(Cget>='0'&&Cget<='9')
                {
                    now_=now_*10+Cget-'0';
                    Cget=getchar();
                }
                now_*=if_z;
            }
            
        public:
            PersistentLineSegmentTree()
            {
                read_int(n),read_int(m);
                for(int i=1;i<=n;i++)
                {
                    read_int(num[i]);
                    num_[i]=num[i];
                }
                sort(num+1,num+n+1);
                size=unique(num+1,num+n+1)-num-1;
                Build(1,size,root[0]);
                for(int i=1;i<=n;i++)
                {
                    num_[i]=lower_bound(num+1,num+size+1,num_[i])-num;
                    Down(1,size,root[i-1],root[i],num_[i]);
                }
                int lit,rit,k;
                for(int i=1;i<=m;i++)
                {
                    read_int(lit),read_int(rit),read_int(k);
                    printf("%d
    ",num[Query(1,size,root[lit-1],root[rit],k)]);
                }
            }
            
            inline void Up(TreeNode &now)
            {
                now.dis=node[now.l].dis+node[now.r].dis;
            }
            
            void Build(int l,int r,TreeNode &now)
            {
                if(l==r) return ;
                int mid=(l+r)>>1;
                now.l=++tot;
                Build(l,mid,node[now.l]);
                now.r=++tot;
                Build(mid+1,r,node[now.r]);
            }
            
            void Down(int l,int r,TreeNode &pre,TreeNode &now,int to)
            {
                if(l==r)
                {
                    now.dis++;
                    return ;
                }
                int mid=(l+r)>>1;
                if(to>mid)
                {
                    now.l=pre.l;
                    now.r=++tot;
                    Down(mid+1,r,node[pre.r],node[now.r],to);
                }
                else
                {
                    now.r=pre.r;
                    now.l=++tot;
                    Down(l,mid,node[pre.l],node[now.l],to);
                }
                Up(now);
            }
            
            int Query(int l,int r,TreeNode &pre,TreeNode &suc,int K)
            {
                if(l==r)
                {
                    return l;
                }
                int mid=(l+r)>>1;
                int dis=node[suc.l].dis-node[pre.l].dis;
                if(K>dis) return Query(mid+1,r,node[pre.r],node[suc.r],K-dis);
                else return Query(l,mid,node[pre.l],node[suc.l],K);
            }
    };
    
    class PersistentLineSegmentTree tree;
    
    int main()
    {
        return 0;
    }

    指针版(正确但超时):

    #include <cstdio>
    #include <cstring>
    #include <iostream>
    #include <algorithm>
    
    using namespace std;
    
    struct TreeNode {
        int l,r,mid,dis;
        
        TreeNode *left,*right;
    };
    
    class PersistentLineSegmentTree {
        private:
            int n,m,if_z,num[100001],num_[100001],size;
            
            char Cget;
            
            TreeNode *root[100001],*null;
            
            inline void read_int(int &now_)
            {
                now_=0,if_z=1,Cget=getchar();
                while(Cget>'9'||Cget<'0')
                {
                    if(Cget=='-') if_z=-1;
                    Cget=getchar();
                }
                while(Cget>='0'&&Cget<='9')
                {
                    now_=now_*10+Cget-'0';
                    Cget=getchar();
                }
                now_*=if_z;
            }
            
        public:
            PersistentLineSegmentTree()
            {
                null=new TreeNode;
                null->left=null;
                null->right=null;
                root[0]=null;
                read_int(n),read_int(m);
                for(int i=1;i<=n;i++)
                {
                    read_int(num[i]);
                    num_[i]=num[i];
                    root[i]=null;
                }
                sort(num+1,num+n+1);
                size=unique(num+1,num+n+1)-num-1;
                Build(root[0],1,size);
                for(int i=1;i<=n;i++)
                {
                    num_[i]=lower_bound(num+1,num+size+1,num_[i])-num;
                    Down(root[i-1],root[i],num_[i]);
                }
                int lit,rit,k;
                for(int i=1;i<=m;i++)
                {
                    read_int(lit),read_int(rit),read_int(k);
                    printf("%d
    ",num[Query(root[lit-1],root[rit],k)]);
                }
            }
            
            void Build(TreeNode *&now,int l,int r)
            {
                if(now==null)
                {
                    now=new TreeNode;
                    now->l=l;
                    now->r=r;
                    now->dis=0;
                    now->mid=(l+r)>>1;
                    now->left=null;
                    now->right=null;
                }
                if(l==r) return ;
                Build(now->left,l,now->mid);
                Build(now->right,now->mid+1,r);
            }
            
            inline void Up(TreeNode *&now)
            {
                now->dis=now->left->dis+now->right->dis;
            }
            
            void Down(TreeNode *&pre,TreeNode *&now,int to)
            {
                now=new TreeNode;
                now->l=pre->l;
                now->r=pre->r;
                now->mid=pre->mid;
                now->left=null;
                now->right=null;
                if(now->l==now->r)
                {
                    now->dis=pre->dis+1;
                    return ;
                }
                if(to>now->mid)
                {
                    now->left=pre->left;
                    Down(pre->right,now->right,to);
                }
                else
                {
                    now->right=pre->right;
                    Down(pre->left,now->left,to);
                }
                Up(now);
            }
            
            int Query(TreeNode *&pre,TreeNode *&now,int K)
            {
                if(now->l==now->r) return now->l;
                int dis=now->left->dis-pre->left->dis;
                if(dis<K) return Query(pre->right,now->right,K-dis);
                else return Query(pre->left,now->left,K);
            }
    };
    class PersistentLineSegmentTree tree;
    
    int main()
    {
        return 0;
    }
  • 相关阅读:
    HDU6168 Numbers
    HDU6170 Two strings
    UVA11426 GCD
    hihocoder1560 H国的身份证号码II
    HDU6156 Palindrome Function
    UVA10917 Walk Through the Forest
    UVA11374 Airport Express
    hihocoder1323 回文字符串
    hihocoder1543 SCI表示法
    CodeForces501C Misha and Forest
  • 原文地址:https://www.cnblogs.com/IUUUUUUUskyyy/p/6294347.html
Copyright © 2020-2023  润新知