fork download
  1. #include<bits/stdc++.h>
  2. #define f first
  3. #define ll long long int
  4. using namespace std;
  5. int main(){
  6. ios_base::sync_with_stdio(false);
  7. cin.tie(NULL);
  8. ll n,q;
  9. cin>>n>>q;
  10. ll a[n+1];
  11. set<ll> st;
  12. map<ll,ll> M;
  13. for(ll i=0;i<n;i++)
  14. cin>>a[i];
  15. sort(a,a+n);
  16. int i=1;
  17. for(auto v:a){
  18. M[v]=i;i++;
  19. }
  20. for(ll i=0;i<q;i++)
  21. {
  22. ll x;
  23. cin>>x;
  24. x--;
  25. if(st.size()==0)
  26. {
  27. cout<<a[x]<<"\n";
  28. st.insert(x);
  29. a[x]=-1;
  30. }
  31. else
  32. {
  33. int ind = distance(st.begin(),st.lower_bound(x));
  34. x=x+ind;
  35. while(x<n && a[x]==-1)x++;
  36. cout<<a[x]<<"\n";
  37. a[x]=-1;
  38. st.insert(x);
  39. }
  40. }
  41.  
  42. return 0;
  43. }
Success #stdin #stdout 0s 15240KB
stdin
10 5
79 72 46 40 6 79 17 28 84 27 
2
9
2
5
1
stdout
17
84
27
72
6