Advertisement
Guest User

Untitled

a guest
May 6th, 2016
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 0.93 KB | None | 0 0
  1. #include<bits/stdc++.h>
  2. using namespace std;
  3.  
  4. int i,op[20005],q,n,st,dr,pivot,rs,aib[20005],aux;
  5. long long a[20005];
  6. set<long long> S;
  7. map<long long,int> Mnorm;
  8. map<int,long long> Mrest;
  9.  
  10. void update(int poz) {
  11.     while(poz<=n) ++aib[poz],poz+=-poz&poz;
  12. }
  13.  
  14. int query(int poz) {
  15.     int ans=0;
  16.  
  17.     while(poz) ans+=aib[poz],poz-=-poz&poz;
  18.  
  19.     return ans;
  20. }
  21.  
  22. int main()
  23. {
  24.   ios_base::sync_with_stdio(0); cin.tie(0);
  25.  
  26.   cin>>q;
  27.   for(i=1;i<=q;++i) cin>>op[i]>>a[i],S.insert(a[i]);
  28.  
  29.   for(auto it:S) Mnorm[it]=++n,Mrest[n]=it;
  30.  
  31.   for(i=1;i<=q;++i)
  32.   if(op[i]==1) update(Mnorm[a[i]]);
  33.   else {
  34.          rs=-1; st=1; dr=n;
  35.          while(st<=dr)
  36.          {
  37.            pivot=(st+dr)/2; aux=query(pivot);
  38.  
  39.            if(aux==a[i]) rs=pivot;
  40.  
  41.            if(aux>=a[i]) dr=pivot-1;
  42.            else st=pivot+1;
  43.          }
  44.  
  45.          if(rs==-1) cout<<"-1\n";
  46.          else cout<<Mrest[rs]<<'\n';
  47.        }
  48.  
  49.  return 0;
  50. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement