Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #include <bits/stdc++.h>
- #define x first
- #define y second
- #define pii pair<int,int>
- #define pb push_back
- #define all(a) begin(a),end(a)
- using namespace std;
- const char en='\n';
- const int N=100010,M=1<<17;
- int q,n,h[N],nod[N],cn[N],seg[M*2],req[N];
- vector<pii> qu[N];
- int main()
- {
- ios_base::sync_with_stdio(0);
- cin.tie(0);
- cin>>n>>q;
- vector<int> v;
- for (int i=0;i<n;++i)
- {
- cin>>h[i];
- v.pb(h[i]);
- }
- sort(all(v));
- v.erase(unique(all(v)),v.end());
- for (int i=0;i<n;++i) h[i]=lower_bound(all(v),h[i])-v.begin();
- for (int i=0;i<q;++i)
- {
- int a,b,c;
- cin>>a>>b>>c;
- --a;
- qu[a].pb({i,-1});
- qu[b].pb({i,1});
- nod[i]=1;
- req[i]=c;
- }
- int off=2,st=M/2;
- while (off<=M)
- {
- memset(cn,0,sizeof(cn));
- for (int i=0;i<q;++i) nod[i]*=2;
- for (int i=0;i<=n;++i)
- {
- for (auto a: qu[i]) cn[a.x]+=a.y*seg[nod[a.x]];
- if (i<n) ++seg[off+h[i]/st];
- }
- for (int i=0;i<q;++i) if (cn[i]<req[i]) ++nod[i],req[i]-=cn[i];
- off*=2,st/=2;
- }
- for (int i=0;i<q;++i) cout<<v[nod[i]-M]<<en;
- }
Advertisement
Add Comment
Please, Sign In to add comment