Submission #1079464

#TimeUsernameProblemLanguageResultExecution timeMemory
107946412345678Index (COCI21_index)C++17
110 / 110
370 ms137236 KiB
#include <bits/stdc++.h> using namespace std; const int nx=2e5+5; int n, q, p[nx], l, r; struct persistsegtree { struct node { int sm; node *l, *r; node(int sm): sm(sm), l(0), r(0){} }; typedef node* pnode; pnode rt[nx]; void build(int l, int r, pnode &k) { k=new node(0); if (l==r) return; int md=(l+r)/2; build(l, md, k->l); build(md+1, r, k->r); } void update(int l, int r, pnode &k, pnode t, int idx) { k=new node(*t); if (l==r) return k->sm++, void(); int md=(l+r)/2; if (idx<=md) update(l, md, k->l, t->l, idx); else update(md+1, r, k->r, t->r, idx); k->sm=k->l->sm+k->r->sm; } int query(int l, int r, pnode k, pnode t, int sv) { if (l==r) return l; int md=(l+r)/2; if (k->r->sm-t->r->sm+sv>=md+1) return query(md+1, r, k->r, t->r, sv); else return query(l, md, k->l, t->l, sv+k->r->sm-t->r->sm); } } s; int main() { cin.tie(NULL)->sync_with_stdio(false); cin>>n>>q; s.build(1, n, s.rt[0]); for (int i=1; i<=n; i++) cin>>p[i], p[i]=min(p[i], n), s.update(1, n, s.rt[i], s.rt[i-1], p[i]); while (q--) cin>>l>>r, cout<<s.query(1, n, s.rt[r], s.rt[l-1], 0)<<'\n'; }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...