P1801 黑匣子[对顶堆]

没错我就是专门找对顶堆练习题的。现在感觉对顶堆使用面有点狭窄。这道题由于我询问是随时间单调增的,而且数据比较友好,应该是插入几次就询问一下的。而中位数那题也是经常询问的。如果查询的东西不单调,或者查询过少,对顶堆都会退化,可能一次维护就要$nlogn$,所以他只适用于经常询问单调增排名的问题。(←个人理解,不服可以来怼我)

所以还是主席树或者平衡树更稳一些啊,但码量问题。。不写了

 1 #include<iostream>
 2 #include<cstdio>
 3 #include<cstring>
 4 #include<cmath>
 5 #include<algorithm>
 6 #include<queue>
 7 #define dbg(x) cerr<<#x<<" = "<<x<<endl
 8 #define ddbg(x,y) cerr<<#x<<" = "<<x<<"   "<<#y<<" = "<<y<<endl
 9 using namespace std;
10 typedef long long ll;
11 template<typename T>inline char MIN(T&A,T B){return A>B?A=B,1:0;}
12 template<typename T>inline char MAX(T&A,T B){return A<B?A=B,1:0;}
13 template<typename T>inline T _min(T A,T B){return A<B?A:B;}
14 template<typename T>inline T _max(T A,T B){return A>B?A:B;}
15 template<typename T>inline T read(T&x){
16     x=0;int f=0;char c;while(!isdigit(c=getchar()))if(c=='-')f=1;
17     while(isdigit(c))x=x*10+(c&15),c=getchar();return f?x=-x:x;
18 }
19 const int N=200000+7;
20 priority_queue<int> q1;
21 priority_queue<int,vector<int>,greater<int> > q2;
22 int a[N];
23 int n,m,j,x;
24 
25 int main(){//freopen("test.in","r",stdin);//freopen("test.out","w",stdout);
26     read(n),read(m);j=0;
27     for(register int i=1;i<=n;++i)read(a[i]);
28     for(register int k=1;k<=m;++k){
29         read(x);
30         while(j<x)++j,(q1.empty()||a[j]<=q1.top()?q1.push(a[j]):q2.push(a[j]));
31         while(q1.size()<k)q1.push(q2.top()),q2.pop();
32         while(q1.size()>k)q2.push(q1.top()),q1.pop();
33         printf("%d\n",q1.top());
34     }
35     return 0;
36 }

猜你喜欢

转载自www.cnblogs.com/saigyouji-yuyuko/p/10634348.html