P1081改的,换行也没问题啊。
#include<iostream>
#include<cstdio>
#include<algorithm>
#include<queue>
using namespace std;
#define int long long
#define ri register int
#define rd(n) n=read()
inline int read(void)
{
register int res=0,f=0;
register char c=getchar();
while(c<'0'||c>'9'){f^=(c=='-');c=getchar();}
while(c>='0'&&c<='9'){res=(res<<3)+(res<<1)+(c^48);c=getchar();}
return f?-res:res;
}
inline void fprint(int n)
{
if(n<0){putchar('-');n=-n;}
if(n>9) fprint(n/10);
putchar(n%10+'0');
}
inline void print(int n)
{
fprint(n);
putchar('\n');
}
const int N=1000005;
int t,n,m,k,x,y,z,ans,res,a[N],b[N],cnt;
priority_queue<int>ql;
priority_queue<int,vector<int>,greater<int>>qr;
signed main(void)
{
rd(t);
for(res=1;res<=t;++res)
{
while(ql.size()) ql.pop();
while(qr.size()) qr.pop();
rd(n); rd(m);
for(ri i=1;i<=n;++i) rd(a[i]);
for(ri i=1;i<=m;++i) rd(b[i]);
k=1; cnt=1;
for(ri i=1;i<=n;++i)
{
x=a[i];
if((int)ql.size()<k)
{
if(qr.empty()) ql.emplace(x);
else
{
y=qr.top();
if(x>y)
{
qr.pop();
qr.emplace(x);
ql.emplace(y);
}
else ql.emplace(x);
}
}
else
{
y=ql.top();
if(x>=y) qr.emplace(x);
else
{
ql.pop();
qr.emplace(y);
ql.emplace(x);
}
}
//cout<<i<<' '<<qr.size()<<"\n";
//cout<<ql.top()<<" ss\n";
while(b[cnt]==i)
{
if(res==t&&cnt==m) fprint(ql.top());
else print(ql.top());
if(b[cnt+1]==i)
{
y=qr.top();
qr.pop();
ql.emplace(y);
}
++cnt; ++k;
}
}
}//putchar('1');
return 0;
}
/*
2
7 4
3 1 -4 2 8 -1000 2
1 2 6 6
7 4
3 1 -4 2 8 -1000 2
1 2 6 6
*/