Submission #928999

#TimeUsernameProblemLanguageResultExecution timeMemory
928999WarinchaiIndex (COCI21_index)C++14
110 / 110
411 ms136628 KiB
#include<bits/stdc++.h> using namespace std; int inf=1e9; struct segtree{ struct node{ int sum; node *l,*r; node(int x=0){ sum=x; l=r=NULL; } }; typedef node* pnode; pnode rt[200005]; int getsum(pnode x){ return x?x->sum:0; } void upd(int st,int en,pnode x,pnode &y,int pos,int val){ y=new node(*x); if(st==en){ y->sum+=val; return void(); } int m=(st+en)/2; if(pos<=m)upd(st,m,x->l,y->l,pos,val); else upd(m+1,en,x->r,y->r,pos,val); y->sum=getsum(y->l)+getsum(y->r); } int fans(int st,int en,pnode x,pnode y,int s=0){ if(st==en)return st; int m=(st+en)/2; if(getsum(y->r)-getsum(x->r)+s>=m+1)return fans(m+1,en,x->r,y->r,s); else return fans(st,m,x->l,y->l,s+getsum(y->r)-getsum(x->r)); } void build(int st,int en,pnode &x){ x=new node(); if(st==en)return; int m=(st+en)/2; build(st,m,x->l); build(m+1,en,x->r); } }tr; int main(){ ios_base::sync_with_stdio(false); cin.tie(0); int n,q;cin>>n>>q; tr.build(1,2e5,tr.rt[0]); for(int i=1;i<=n;i++){ int a;cin>>a; tr.upd(1,2e5,tr.rt[i-1],tr.rt[i],a,1); } for(int i=1;i<=q;i++){ int l,r;cin>>l>>r; cout<<tr.fans(1,2e5,tr.rt[l-1],tr.rt[r])<<"\n"; } }
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...