#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;
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];
//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;
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]);
//print();
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
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 |
1 |
Runtime error |
551 ms |
480456 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
2889 ms |
524288 KB |
Execution killed with signal 9 |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
209 ms |
243112 KB |
Output is correct |
2 |
Correct |
205 ms |
231508 KB |
Output is correct |
3 |
Correct |
176 ms |
218776 KB |
Output is correct |
4 |
Correct |
166 ms |
223384 KB |
Output is correct |
5 |
Correct |
199 ms |
240976 KB |
Output is correct |
6 |
Correct |
164 ms |
225104 KB |
Output is correct |
7 |
Correct |
191 ms |
234132 KB |
Output is correct |
8 |
Correct |
178 ms |
225876 KB |
Output is correct |
9 |
Correct |
187 ms |
233552 KB |
Output is correct |
10 |
Correct |
169 ms |
217840 KB |
Output is correct |
11 |
Correct |
156 ms |
218388 KB |
Output is correct |
12 |
Correct |
167 ms |
217940 KB |
Output is correct |
13 |
Correct |
163 ms |
218704 KB |
Output is correct |
14 |
Correct |
162 ms |
218704 KB |
Output is correct |
15 |
Correct |
149 ms |
217684 KB |
Output is correct |
16 |
Correct |
109 ms |
212016 KB |
Output is correct |
17 |
Correct |
159 ms |
219216 KB |
Output is correct |
18 |
Correct |
173 ms |
217428 KB |
Output is correct |
19 |
Correct |
94 ms |
207444 KB |
Output is correct |
20 |
Correct |
97 ms |
207448 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Runtime error |
551 ms |
480456 KB |
Execution killed with signal 11 |
2 |
Halted |
0 ms |
0 KB |
- |