zoukankan      html  css  js  c++  java
  • POJ 2104 K-th Number

    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.
     
    题解:
    开n个线段树,主席树维护,n个分别表示[1,i]区间中[L,R]区间内数字的个数,相当于一个前缀和
    区间询问就是[x,y]实际上就是root[y]-root[x-1]两棵主席树的sum之差
     1 #include<iostream>
     2 #include<cstdio>
     3 #include<cstring>
     4 #include<algorithm>
     5 using namespace std;
     6 const int N=100005,M=5005;
     7 int gi()
     8     {
     9         int str=0,f=1;char ch=getchar();
    10         while(ch>'9' || ch<'0'){if(ch=='-')f=-1;ch=getchar();}
    11         while(ch>='0' && ch<='9')str=(str<<1)+(str<<3)+ch-48,ch=getchar();
    12         return str*f;
    13     }
    14 struct node{
    15     int x,id;
    16 }a[N];
    17 int bel[N],root[N];
    18 bool comp(const node &p,const node &q){return p.x<q.x;}
    19 struct Tree{
    20     int ls,rs,sum;
    21 }t[N*20];
    22 int tot=0;
    23 void insert(int &rt,int fa,int x,int l,int r)
    24     {
    25         rt=++tot;
    26         t[rt].sum=t[fa].sum;
    27         t[rt].sum++;
    28         if(l==r)return ;
    29         int mid=(l+r)>>1;
    30         if(x<=mid)
    31             {
    32                 insert(t[rt].ls,t[fa].ls,x,l,mid);
    33                 t[rt].rs=t[fa].rs;
    34             }
    35         else
    36             {
    37                 insert(t[rt].rs,t[fa].rs,x,mid+1,r);
    38                 t[rt].ls=t[fa].ls;
    39             }
    40     }
    41 int query(int rt,int fa,int k,int l,int r)
    42     {
    43         if(l==r)return a[l].x;
    44         int mid=(l+r)>>1,kt=t[t[rt].ls].sum-t[t[fa].ls].sum;
    45         if(k<=kt)return query(t[rt].ls,t[fa].ls,k,l,mid);
    46         else return query(t[rt].rs,t[fa].rs,k-kt,mid+1,r);
    47     }
    48 int main()
    49     {
    50         int n=gi(),m=gi();
    51         for(int i=1;i<=n;i++)a[i].x=gi(),a[i].id=i;
    52         sort(a+1,a+n+1,comp);
    53         for(int i=1;i<=n;i++)bel[a[i].id]=i;
    54          for(int i=1;i<=n;i++)
    55              insert(root[i],root[i-1],bel[i],1,n);
    56          int x,y,z;
    57          for(int i=1;i<=m;i++)
    58             {
    59                 x=gi();y=gi();z=gi();
    60                 printf("%d
    ",query(root[y],root[x-1],z,1,n));
    61             }
    62     }

     还有整体二分的:

    大概思路是讲离线询问,然后选定区间[L,R]找出每个询问中 mid=(L+R)/2 满足在这个区间[L,mid]内的个数 <=q[i].k

    就加入到左边,否则加入到右边,然后继续二分

     1 #include<iostream>
     2 #include<cstdio>
     3 #include<algorithm>
     4 #include<cmath>
     5 #include<cstring>
     6 using namespace std;
     7 const int N=100005,M=5005;
     8 int gi(){
     9     int str=0,f=1;char ch=getchar();
    10     while(ch>'9' || ch<'0'){if(ch=='-')f=-1;ch=getchar();}
    11     while(ch>='0' && ch<='9')str=(str<<1)+(str<<3)+ch-48,ch=getchar();
    12     return str*f;
    13 }
    14 int n,m;
    15 struct node{
    16     int x,id;
    17 }a[N];
    18 struct Ques{
    19     int x,y,k,id,cnt;
    20 }q[M],tmp[M];
    21 bool comp(const node &p,const node &q){return p.x<q.x;}
    22 int ans[M],liml=-2e9,limr=2e9,Tree[N*4];
    23 void add(int sta,int ad){
    24     for(int i=sta;i<=n;i+=(i&(-i)))Tree[i]+=ad;
    25 }
    26 int getsum(int sta){
    27     int sum=0;
    28     for(int i=sta;i>=1;i-=(i&(-i)))sum+=Tree[i];
    29     return sum;
    30 }
    31 void count(int ll,int rr,int dl,int dr)
    32     {
    33         int L=1,R=n,mid,as=n+1;
    34         while(L<=R)
    35             {
    36                 mid=(L+R)>>1;
    37                 if(dl<=a[mid].x)as=mid,R=mid-1;
    38                 else L=mid+1;
    39             }
    40         for(int i=as;i<=n && a[i].x<=dr;i++)
    41             add(a[i].id,1);
    42         for(int i=ll;i<=rr;i++)
    43                 q[i].cnt=getsum(q[i].y)-getsum(q[i].x-1);
    44         for(int i=as;i<=n && a[i].x<=dr;i++)
    45             add(a[i].id,-1);
    46     }
    47 void work(int ll,int rr,int dl,int dr)
    48     {
    49         if(dl==dr)
    50             {
    51                 for(int i=ll;i<=rr;i++)ans[q[i].id]=dl;
    52                 return ;
    53             }
    54         int mid=(dl+dr)>>1;
    55         count(ll,rr,dl,mid);
    56         int l=ll,r=rr;
    57         for(int i=ll;i<=rr;i++)
    58             {
    59                 if(q[i].cnt>=q[i].k)
    60                     tmp[l++]=q[i];
    61                 else
    62                    q[i].k-=q[i].cnt,tmp[r--]=q[i];
    63             }
    64         for(int i=ll;i<=rr;i++)q[i]=tmp[i];
    65         if(l!=ll)
    66             work(ll,l-1,dl,mid);
    67         if(r!=rr)
    68             work(r+1,rr,mid+1,dr);
    69     }
    70 int main()
    71     {
    72         n=gi();m=gi();
    73         for(int i=1;i<=n;i++)
    74             a[i].x=gi(),a[i].id=i;
    75         sort(a+1,a+n+1,comp);
    76         for(int i=1;i<=m;i++)
    77             {
    78                 q[i].x=gi();q[i].y=gi();q[i].k=gi();
    79                 q[i].id=i;
    80             }
    81         work(1,m,liml,limr);
    82         for(int i=1;i<=m;i++)printf("%d
    ",ans[i]);
    83         return 0;
    84     }
  • 相关阅读:
    L1-061 新胖子公式 (10 分)
    L1-060 心理阴影面积 (5 分)
    L1-059 敲笨钟 (20 分)
    Linux高性能服务器编程—进程池和线程池
    epoll系列系统调用
    18.5.2 多线程并发服务器端的实现
    10.4 基于多任务的并发服务器
    侯捷C++(complex类)
    Qt 事件机制
    C++篇49问49答
  • 原文地址:https://www.cnblogs.com/Yuzao/p/7109577.html
Copyright © 2011-2022 走看看