Guest User

kth number in range

a guest
Mar 29th, 2021
676
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.03 KB | None | 0 0
  1. #include <bits/stdc++.h>
  2. #define x first
  3. #define y second
  4. #define pii pair<int,int>
  5. #define pb push_back
  6. #define all(a) begin(a),end(a)
  7.  
  8. using namespace std;
  9.  
  10. const char en='\n';
  11. const int N=100010,M=1<<17;
  12. int q,n,h[N],nod[N],cn[N],seg[M*2],req[N];
  13. vector<pii> qu[N];
  14.  
  15. int main()
  16. {
  17.     ios_base::sync_with_stdio(0);
  18.     cin.tie(0);
  19.     cin>>n>>q;
  20.     vector<int> v;
  21.     for (int i=0;i<n;++i)
  22.     {
  23.         cin>>h[i];
  24.         v.pb(h[i]);
  25.     }
  26.     sort(all(v));
  27.     v.erase(unique(all(v)),v.end());
  28.     for (int i=0;i<n;++i) h[i]=lower_bound(all(v),h[i])-v.begin();
  29.     for (int i=0;i<q;++i)
  30.     {
  31.         int a,b,c;
  32.         cin>>a>>b>>c;
  33.         --a;
  34.         qu[a].pb({i,-1});
  35.         qu[b].pb({i,1});
  36.         nod[i]=1;
  37.         req[i]=c;
  38.     }
  39.     int off=2,st=M/2;
  40.     while (off<=M)
  41.     {
  42.         memset(cn,0,sizeof(cn));
  43.         for (int i=0;i<q;++i) nod[i]*=2;
  44.         for (int i=0;i<=n;++i)
  45.         {
  46.             for (auto a: qu[i]) cn[a.x]+=a.y*seg[nod[a.x]];
  47.             if (i<n) ++seg[off+h[i]/st];
  48.         }
  49.         for (int i=0;i<q;++i) if (cn[i]<req[i]) ++nod[i],req[i]-=cn[i];
  50.         off*=2,st/=2;
  51.     }
  52.     for (int i=0;i<q;++i) cout<<v[nod[i]-M]<<en;
  53. }
Advertisement
Add Comment
Please, Sign In to add comment