This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |