luogu P1801 黑匣子_NOI导刊2010提高(06)

题目描述

Black Box是一种原始的数据库。它可以储存一个整数数组,还有一个特别的变量i。最开始的时候Black Box是空的.而i等于0。这个Black Box要处理一串命令。

命令只有两种:

ADD(x):把x元素放进BlackBox;

GET:i加1,然后输出Blackhox中第i小的数。

记住:第i小的数,就是Black Box里的数的按从小到大的顺序排序后的第i个元素。例如:

我们来演示一下一个有11个命令的命令串。(如下图所示)

现在要求找出对于给定的命令串的最好的处理方法。ADD和GET命令分别最多200000个。现在用两个整数数组来表示命令串:

1.A(1),A(2),…A(M):一串将要被放进Black Box的元素。每个数都是绝对值不超过2000000000的整数,M$200000。例如上面的例子就是A=(3,1,一4,2,8,-1000,2)。

2.u(1),u(2),…u(N):表示第u(j)个元素被放进了Black Box里后就出现一个GET命令。例如上面的例子中u=(l,2,6,6)。输入数据不用判错。

输入输出格式

输入格式:

 

第一行,两个整数,M,N。

第二行,M个整数,表示A(l)

……A(M)。

第三行,N个整数,表示u(l)

…u(N)。

 

输出格式:

 

输出Black Box根据命令串所得出的输出串,一个数字一行。

 

输入输出样例

输入样例#1: 复制
7 4
3 1 -4 2 8 -1000 2
1 2 6 6
输出样例#1: 复制
3
3
1
2

说明

对于30%的数据,M≤10000;

对于50%的数据,M≤100000:

对于100%的数据,M≤200000。

  1 #include<algorithm>
  2 #include<iostream>
  3 #include<cstring>
  4 #include<cstdlib>
  5 #include<cstdio>
  6 #include<cmath>
  7 #define RG register
  8 #define il inline
  9 #define rep(i,a,b) for(register int i=a;i<=b;++i)
 10 #define ll long long
 11 #define db double
 12 using namespace std;
 13 const int N = 200000 + 5;
 14 struct Minh{
 15     int a[N],sz;
 16     Minh(){sz = 0;}
 17     il int top(){
 18     return a[1];
 19     }
 20     il void pop(){
 21     RG int now = 1,nxt;
 22     a[1]=a[sz--];
 23     while((nxt = now<<1)<=sz){
 24         if(nxt<sz&&a[nxt+1]<a[nxt]) nxt++;
 25         if(a[now]<=a[nxt]) break;
 26         swap(a[now],a[nxt]);
 27         now = nxt;
 28     }
 29     }
 30     il bool empty(){
 31     return sz==0;
 32     }
 33     il void push(RG int x){
 34     a[++sz]=x;
 35     RG int now=sz,pa;
 36     while(now>1){
 37         pa = now>>1;
 38         if(a[now]>=a[pa])break;
 39         swap(a[now],a[pa]);
 40         now = pa;
 41     }
 42     }
 43 };
 44 
 45 struct Maxh{
 46     int a[N],sz;
 47     Maxh(){sz = 0;}
 48     il int top(){
 49     return a[1];
 50     }
 51     il void pop(){
 52     RG int now = 1,nxt;
 53     a[1]=a[sz--];
 54     while((nxt = now<<1)<=sz){
 55         if(nxt<sz&&a[nxt+1]>a[nxt]) nxt++;
 56         if(a[now]>=a[nxt]) break;
 57         swap(a[now],a[nxt]);
 58         now = nxt;
 59     }
 60     }
 61     il bool empty(){
 62     return sz==0;
 63     }
 64     il void push(RG int x){
 65     a[++sz]=x;
 66     RG int now=sz,pa;
 67     while(now>1){
 68         pa = now>>1;
 69         if(a[now]<=a[pa])break;
 70         swap(a[now],a[pa]);
 71         now = pa;
 72     }
 73     }
 74 };
 75 
 76 Maxh qma; Minh qmi;
 77 il int gi(){
 78     RG int x=0,f=1; RG char ch=getchar(); while((ch<'0'||ch>'9')&&ch!='-') ch=getchar();
 79     if(ch=='-') f=-1,ch=getchar(); while(ch>='0'&&ch<='9') x=x*10+ch-'0',ch=getchar(); return x*f;
 80 }
 81 
 82 int a[N],m,n;
 83 int main(){// remember register
 84     freopen("heap.in","r",stdin);
 85     freopen("heap.out","w",stdout);
 86     m=gi(),n=gi(); rep(i,1,m) a[i]=gi();
 87     RG int j=1,p,y;
 88     for(RG int i=1;i<=n;i++){
 89     p=gi();
 90     while(j<=m&&j<=p){
 91         if(!qma.empty()){
 92         y=qma.top();
 93         if(y<=a[j])
 94             qmi.push(a[j]);
 95         else{
 96             qma.pop(); qma.push(a[j]);
 97             qmi.push(y);
 98         }
 99         }
100         else qmi.push(a[j]);
101         j++;
102     }
103     y=qmi.top(); qmi.pop();
104     printf("%d\n",y);
105     qma.push(y);
106     }
107     return 0;
108 }

 

posted @ 2017-11-07 17:15  Yinpz_23  阅读(147)  评论(0编辑  收藏  举报