Submission #857349

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