This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
/*#pragma GCC target ("avx2")
#pragma GCC optimize ("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize ("unroll-loops")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")/**/
#include<bits/stdc++.h>
/**/
//typedef int ll;
typedef long long ll;
typedef unsigned long long ull;
typedef std::string str;
//typedef vector<std::vector<ll>> vv;
/*typedef __int128 vll;
typedef unsigned __int128 uvll;*/
#define llll std::pair<ll , ll>
#define pb push_back
#define pf push_front
#define halo cout << "hello\n"
#define fi first
#define sec second
#define vv(x) std::vector<std::vector<x>>
#define all(a) a.begin() , a.end()
const ll limit = 1e15+7;
const ll ous = 2e5 + 7;
const ll dx[4] = {-1 , 0 , 1 , 0} , dy[4] = {0,1,0,-1};
ll n , m;std::vector<ll> ar , nex , ants , valt;std::vector<std::pair<ll , llll>> que , v;
std::set<std::pair<ll , llll>> st;
ll siz = 0 , kd = 0;
ll fnd(ll f , ll s , ll t){
ll ret = std::lower_bound(all(v) , std::make_pair(f , std::make_pair(s , t))) - v.begin();
return ret;
}
void upd(ll l , ll r , ll a , ll nt , ll oz){
if(l == r){
if(oz){
valt[a] = v[l].sec.sec - v[l].sec.fi + 1;
}
else{
valt[a] = 0;
}
return;
}
ll md = (l + r)/2;
if(nt > md){
upd(md+1 , r , a*2+1 , nt , oz);
}
else{
upd(l , md , a*2 , nt , oz);
}
valt[a] = valt[a*2] + valt[a*2+1];
}
ll binseg(ll l , ll r , ll a ,ll cnt){
if(l == r){
return ar[v[l].sec.fi + cnt - 1];
}
ll md = (l + r) / 2;
if(valt[a*2] >= cnt){
return binseg(l , md , a*2 ,cnt);
}
else{
cnt -= valt[a*2];
return binseg(md+1 , r , a*2+1 , cnt);
}
}
void addset(ll l , ll r){
siz += r - l + 1;
st.insert({ar[l] , {l , r}});
if(kd == 0){
v.pb({ar[l] , {l , r}});
}
else{
upd(0 , v.size()-1 , 1 , fnd(ar[l] , l , r) , 1);
}
}
void addi(ll l , ll r){
while(r >= l){
ll nx = nex[l] - 1;;
nx = std::min(nx , r);
addset(l , nx);
//std::cout << l << " " << nx << "\n";
l = nex[l];
}
}
void rm(){
while(1){
std::pair<ll , llll> a = *st.rbegin();
ll p = a.sec.sec - a.sec.fi + 1;
if(siz - p < n/2){
return;
}
siz-=p;
//std::cout << a.fi << " " << a.sec.fi << " " << a.sec.sec << " " << siz << "\n";
auto it = st.end();it--;
st.erase(it);
}
}
void midup(){
std::pair<ll , llll> a = *st.rbegin();
ll p = a.sec.sec - a.sec.fi + 1;
auto it = st.end();it--;st.erase(it);
siz -= p;
if(kd){
upd(0 , v.size()-1 , 1 , fnd(a.fi , a.sec.fi , a.sec.sec) , 0);
}
//std::cout << a.fi << " " << a.sec.fi << " " << a.sec.sec << " " << siz << "\n";
ll of = n/2 -siz;
addset(a.sec.fi , a.sec.fi + of - 1);
addi(a.sec.fi+of , a.sec.sec);
}
void solve(){
std::cin >> n >> m;
ar.resize(n);que.resize(m);nex.resize(n);ants.resize(m);
for(ll i = 0;n>i;i++){
std::cin >> ar[i];
}
for(ll i = 0;m>i;i++){
std::cin >> que[i].fi >> que[i].sec.fi;
que[i].fi = std::min(que[i].fi , n);
que[i].sec.sec = i;
}
std::sort(all(que));
std::vector<llll> sta;
sta.pb({limit , n});
for(ll i = n-1;0<=i;i--){
while(sta.back().fi < ar[i]){
sta.pop_back();
}
nex[i] = sta.back().sec;
sta.pb({ar[i] , i});
}
addi(0 , n-1);
while(1){
rm();
if(siz == n/2){
break;
}
midup();
}
std::sort(all(v));
valt.resize(4 * v.size());
kd = 1;
st.clear();
ll tt = 0;
siz = 0;
addi(0 , n-1);
for(ll i = 0;m>i;i++){
for(;que[i].fi > tt;tt++){
rm();
midup();
}
ants[que[i].sec.sec] = binseg(0 , v.size() - 1 , 1 , que[i].sec.fi);
}
for(ll i = 0;m>i;i++){
std::cout << ants[i] << "\n";
}
return;/**/
}
signed main(){
std::ios_base::sync_with_stdio(false);std::cin.tie(NULL);
ll t=1;
//std::cin >> t;
ll o = 1;
while(t--){
solve();
}
return 0;
}/**/
Compilation message (stderr)
Main.cpp:5:78: warning: "/*" within comment [-Wcomment]
5 | #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")/**/
|
Main.cpp: In function 'int main()':
Main.cpp:162:8: warning: unused variable 'o' [-Wunused-variable]
162 | ll o = 1;
| ^
# | 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... |