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<iostream>
#include<stack>
#include<map>
#include<vector>
#include<string>
#include<unordered_map>
#include <queue>
#include<cstring>
#include<limits.h>
#include <cassert>
#include <cstdint>
#include<cmath>
#include<set>
#include<algorithm>
#include <iomanip>
#include<numeric> //gcd(a,b)
#include<bitset>
using namespace std;
#define ll long long
#define f first
#define endl "\n"
#define s second
#define pii pair<int,int>
#define ppii pair<int,pii>
#define vi vector<int>
#define pb push_back
#define all(x) x.begin(),x.end()
#define rall(x) x.rbegin(),x.rend()
#define F(n) for(int i=0;i<n;i++)
#define lb lower_bound
#define ub upper_bound
#define fastio ios::sync_with_stdio(false);cin.tie(NULL);
#pragma GCC optimize ("03,unroll-loops")
using namespace std;
#define int long long
#define double long double
const int mod=1e9+7,mxn=3e5+5,lg=30,inf=1e16,minf=-1e9,Mxn=1e6;
int n,q;
struct fen{
int fwk[mxn+10];
void update(int pos,int val){for(int i=pos;i<=n;i+=(i&-i))fwk[i]+=val;}
int qry(int pos){
if(pos==0)return 0;
int sum=0;
for(int i=pos;i>0;i-=(i&-i))sum+=fwk[i];
return sum;
}
}t;
pii nxt[mxn+10];
int pos[mxn+10],opos[mxn+10],rep[mxn+10];
//nx number that is more than cur,position of number in block
deque<int> com[mxn+10];
int split(int id,int x){
int nxtid=com[id][x];
t.update(id,-com[id].size()+x);
t.update(nxtid,com[id].size()-x);
if(2*x>com[id].size()){
while(com[id].size()!=x){
com[nxtid].push_front(com[id].back());
com[id].pop_back();
}
}
else{
swap(com[id],com[nxtid]);
for(int i=0;i<x;i++){
com[id].push_back(com[nxtid].front());
com[nxtid].pop_front();
}
}
return nxtid;
}
void calblock(int x){
int st=x;
while(st!=inf&&(nxt[st].s-opos[st])<com[st].size()){
split(st,nxt[st].s-opos[st]);
st=nxt[st].f;
}
}
/*
void print(){
for(int i=1;i<=n;i++){
for(auto j:com[i])cout<<j<<" ";
//cout<<'\n';
}
cout<<'\n';
for(int i=1;i<=n;i++)cout<<t.qry(i)-t.qry(i-1)<<" ";
cout<<'\n';
}*/
int ans[Mxn+10];
pii get(int x){
int l=1,r=n,pos=inf;
while(l<=r){
int mid=l+(r-l)/2;
if(t.qry(mid)>=x)r=mid-1,pos=min(pos,mid);
else l=mid+1;
}
int y=(x)-(t.qry(pos)-com[pos].size());
return {pos,y};
}
int32_t main(){
fastio
cin>>n>>q;
vector<int>v(n);
for(int i=0;i<n;i++)cin>>v[i],opos[v[i]]=i;
for(int i=1;i<=n;i++)rep[i]=i;
stack<pii>st;
for(int i=n-1;i>=0;i--){
while(!st.empty()&&v[i]>st.top().f)st.pop();
nxt[v[i]]=((st.empty())?make_pair(inf,inf):st.top());
st.push({v[i],i});
}
vector<pair<int,pii>>qry(q);
for(int i=0;i<q;i++){
cin>>qry[i].f>>qry[i].s.f,qry[i].s.s=i;
}
sort(all(qry));
for(int i=0;i<n;i++)com[v[0]].pb(v[i]);
t.update(v[0],n);
calblock(v[0]);
int cur=0;
for(int i=0;i<n;i++){
pii a;
//print();
while(cur<q&&qry[cur].f<=i){
a=get(qry[cur].s.f);
ans[qry[cur].s.s]=com[a.f][a.s-1];
cur++;
}
a=get(n/2);
if(com[a.f].size()<a.s)break;
if(a.s==com[a.f].size())break;
calblock(split(a.f,a.s));
}
while(cur<q){
pii a=get(qry[cur].s.f);
ans[qry[cur].s.s]=com[a.f][a.s-1];
cur++;
}
for(int i=0;i<q;i++)cout<<ans[i]<<"\n";
}
/*
*/
/*
10 0
7 5 2 9 10 8 4 3 6 1
6 3
1 5 6 2 3 4
1 2
0 4
1 5
*/
Compilation message (stderr)
Main.cpp: In function 'long long int split(long long int, long long int)':
Main.cpp:57:8: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::deque<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
57 | if(2*x>com[id].size()){
| ~~~^~~~~~~~~~~~~~~
Main.cpp:58:23: warning: comparison of integer expressions of different signedness: 'std::deque<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
58 | while(com[id].size()!=x){
| ~~~~~~~~~~~~~~^~~
Main.cpp: In function 'void calblock(long long int)':
Main.cpp:75:37: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::deque<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
75 | while(st!=inf&&(nxt[st].s-opos[st])<com[st].size()){
| ~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~~
Main.cpp: In function 'int32_t main()':
Main.cpp:131:21: warning: comparison of integer expressions of different signedness: 'std::deque<long long int>::size_type' {aka 'long unsigned int'} and 'long long int' [-Wsign-compare]
131 | if(com[a.f].size()<a.s)break;
| ^
Main.cpp:132:9: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::deque<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
132 | if(a.s==com[a.f].size())break;
| ~~~^~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |