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 persist
{
struct node
{
int vl;
node *l, *r;
node(): vl(0), l(0), r(0){}
node(int vl): vl(vl), 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->vl++, 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->vl=k->l->vl+k->r->vl;
}
void show(int l, int r, pnode k)
{
cout<<l<<' '<<r<<' '<<k->vl<<'\n';
if (l==r) return;
int md=(l+r)/2;
show(l, md, k->l);
show(md+1, r ,k->r);
}
int query(int l, int r, pnode t, pnode k, int sm)
{
if (l==r) return l;
//cout<<"query "<<l<<' '<<r<<' '<<sm<<' '<<(k->r->vl)-(t->r->vl)<<'\n';
int md=(l+r)/2;
if ((k->r->vl)-(t->r->vl)+sm>=md+1) return query(md+1, r, t->r, k->r, sm);
else return query(l, md, t->l, k->l, (k->r->vl)-(t->r->vl)+sm);
}
} s;
int main()
{
cin.tie(NULL)->sync_with_stdio(false);
cin>>n>>q;
for (int i=1; i<=n; i++) cin>>p[i];
s.build(1, n, s.rt[0]);
for (int i=1; i<=n; i++) s.update(1, n, s.rt[i], s.rt[i-1], p[i]);
//s.show(1, n, s.rt[n]);
while (q--)
{
cin>>l>>r;
cout<<s.query(1, n, s.rt[l-1], s.rt[r], 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... |