#include<bits/stdc++.h>
#define int long long
using namespace std;
int a[2000005], tot, vis[1000005], root;
struct node{
int val, ls, rs, pri, siz, num;
}tr[1000005];
int newnode(int val){
tr[++tot].ls = tr[tot].rs = 0;
tr[tot].pri = rand();
tr[tot].val = val;
tr[tot].num = tr[tot].siz = 1;
return tot;
}
void update(int p){
tr[p].siz = tr[tr[p].ls].siz + tr[tr[p].rs].siz + tr[p].num;
}
void zig(int &p){
int q = tr[p].ls;
tr[p].ls = tr[q].rs;
tr[q].rs = p;
tr[q].siz = tr[p].siz;
update(p);
p = q;
}
void zag(int &p){
int q = tr[p].rs;
tr[p].rs = tr[q].ls;
tr[q].ls = p;
tr[q].siz = tr[p].siz;
update(p);
p = q;
}
void Insert(int &p, int val){
if(!p){
p = newnode(val);
return;
}
tr[p].siz++;
if(val == tr[p].val){
tr[p].num++;
return;
}
if(val < tr[p].val){
Insert(tr[p].ls, val);
if(tr[p].pri < tr[tr[p].ls].pri) zig(p);
}
else{
Insert(tr[p].rs, val);
if(tr[p].pri < tr[tr[p].rs].pri) zag(p);
}
}
int GetValByRank(int p, int rank){
if(!p) return 0;
// cout << "p" << p << "\n";
if(tr[tr[p].ls].siz >= rank) return GetValByRank(tr[p].ls, rank);
else if(tr[tr[p].ls].siz + tr[p].num < rank) return GetValByRank(tr[p].rs, rank - tr[tr[p].ls].siz - tr[p].num);
else return tr[p].val;
}
signed main(){
int n, m;
cin >> n >> m;
for(int i = 1; i <= n; i++)
cin >> a[i];
int x = 1, t = 1;
for(int i = 1; i <= m; i++){
cin >> vis[i];
}
while(t <= m){
while(x <= vis[t]){
Insert(root, a[x]);
x++;
}
cout << GetValByRank(root, t++) << "\n";
}
}
调了好久嘤嘤嘤