#include <bits/stdc++.h>
#pragma GCC target("sse,sse2")
#pragma GCC optimize("unroll-loops,O3")
#define rep(i,l,r) for (int i = l; i < r; i++)
#define repr(i,r,l) for (int i = r; i >= l; i--)
#define X first
#define Y second
#define all(x) (x).begin() , (x).end()
#define pb push_back
#define endl '\n'
#define debug(x) cerr << #x << " : " << x << endl;
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pll;
constexpr int N = 1e6+20,mod = 1e9+7,inf = 1e9+10;
inline int mkay(int a,int b){
if (a+b >= mod) return a+b-mod;
if (a+b < 0) return a+b+mod;
return a+b;
}
inline int poww(int a,int k){
if (k < 0) return 0;
int z = 1;
while (k){
if (k&1) z = 1ll*z*a%mod;
a = 1ll*a*a%mod;
k /= 2;
}
return z;
}
int n;
int a[N],l[N],r[N],mx[N][20],lg[N];
int rmq(int l,int r){
int j = lg[r-l];
return max(mx[r-1][j],mx[l+(1 << j)-1][j]);
}
int main(){
ios_base :: sync_with_stdio(0); cin.tie(0);
cin >> n;
lg[1] = 0;
rep(i,2,n+1) lg[i] = 1+lg[i/2];
stack<int> st;
rep(i,0,n){
cin >> a[i];
mx[i][0] = a[i];
while (!st.empty() && a[st.top()] < a[i]) st.pop();
if (st.empty()) l[i] = -1;
else l[i] = st.top();
st.push(i);
}
rep(j,1,20){
rep(i,(1 << j)-1,n){
mx[i][j] = max(mx[i][j-1],mx[i-(1 << (j-1))][j-1]);
}
}
while(!st.empty()) st.pop();
repr(i,n-1,0){
while (!st.empty() && a[st.top()] < a[i]) st.pop();
if (st.empty()) r[i] = n;
else r[i] = st.top();
st.push(i);
}
int q;
cin >> q;
while(q--){
int L,R;
cin >> L >> R;
L--;
int ans = 0,m[5] = {-1,-1,-1,-1,-1};
rep(i,L,R){
rep(j,0,5){
if (a[i] <= m[j]) continue;
repr(k,4,j+1) m[k] = m[k-1];
m[j] = a[i];
}
}
rep(i,L,R){
if (l[i] >= L && 2*i-l[i] < R){
if (a[i] >= m[4] || a[l[i]] >= m[4] || rmq(2*i-l[i],R) >= m[4]) ans = max(ans,a[i]+a[l[i]]+rmq(2*i-l[i],R));
}
if (r[i] < R && 2*r[i]-i < R){
if (a[i] >= m[4] || a[r[i]] >= m[4] || rmq(2*r[i]-i,R) >= m[4]) ans = max(ans,a[i]+a[r[i]]+rmq(2*r[i]-i,R));
}
}
cout << ans << endl;
}
return 0;
}
#include <bits/stdc++.h>
#pragma GCC target("sse,sse2")
#pragma GCC optimize("unroll-loops,O3")
#define rep(i,l,r) for (int i = l; i < r; i++)
#define repr(i,r,l) for (int i = r; i >= l; i--)
#define X first
#define Y second
#define all(x) (x).begin() , (x).end()
#define pb push_back
#define endl '\n'
#define debug(x) cerr << #x << " : " << x << endl;
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pll;
constexpr int N = 1e6+20,mod = 1e9+7,inf = 1e9+10;
inline int mkay(int a,int b){
if (a+b >= mod) return a+b-mod;
if (a+b < 0) return a+b+mod;
return a+b;
}
inline int poww(int a,int k){
if (k < 0) return 0;
int z = 1;
while (k){
if (k&1) z = 1ll*z*a%mod;
a = 1ll*a*a%mod;
k /= 2;
}
return z;
}
int n;
int a[N],l[N],r[N],mx[N][20],lg[N];
int rmq(int l,int r){
int j = lg[r-l];
return max(mx[r-1][j],mx[l+(1 << j)-1][j]);
}
int main(){
ios_base :: sync_with_stdio(0); cin.tie(0);
cin >> n;
lg[1] = 0;
rep(i,2,n+1) lg[i] = 1+lg[i/2];
stack<int> st;
rep(i,0,n){
cin >> a[i];
mx[i][0] = a[i];
while (!st.empty() && a[st.top()] < a[i]) st.pop();
if (st.empty()) l[i] = -1;
else l[i] = st.top();
st.push(i);
}
rep(j,1,20){
rep(i,(1 << j)-1,n){
mx[i][j] = max(mx[i][j-1],mx[i-(1 << (j-1))][j-1]);
}
}
while(!st.empty()) st.pop();
repr(i,n-1,0){
while (!st.empty() && a[st.top()] < a[i]) st.pop();
if (st.empty()) r[i] = n;
else r[i] = st.top();
st.push(i);
}
int q;
cin >> q;
while(q--){
int L,R;
cin >> L >> R;
L--;
int ans = 0,m[5] = {-1,-1,-1,-1,-1};
rep(i,L,R){
rep(j,0,5){
if (a[i] <= m[j]) continue;
repr(k,4,j+1) m[k] = m[k-1];
m[j] = a[i];
}
}
rep(i,L,R){
if (l[i] >= L && 2*i-l[i] < R){
if (a[i] >= m[4] || a[l[i]] >= m[4] || rmq(2*i-l[i],R) >= m[4]) ans = max(ans,a[i]+a[l[i]]+rmq(2*i-l[i],R));
}
if (r[i] < R && 2*r[i]-i < R){
if (a[i] >= m[4] || a[r[i]] >= m[4] || rmq(2*r[i]-i,R) >= m[4]) ans = max(ans,a[i]+a[r[i]]+rmq(2*r[i]-i,R));
}
}
cout << ans << endl;
}
return 0;
}
Compilation message
jumps.cpp:108:15: error: redefinition of 'constexpr const int N'
108 | constexpr int N = 1e6+20,mod = 1e9+7,inf = 1e9+10;
| ^
jumps.cpp:16:15: note: 'constexpr const int N' previously defined here
16 | constexpr int N = 1e6+20,mod = 1e9+7,inf = 1e9+10;
| ^
jumps.cpp:108:26: error: redefinition of 'constexpr const int mod'
108 | constexpr int N = 1e6+20,mod = 1e9+7,inf = 1e9+10;
| ^~~
jumps.cpp:16:26: note: 'constexpr const int mod' previously defined here
16 | constexpr int N = 1e6+20,mod = 1e9+7,inf = 1e9+10;
| ^~~
jumps.cpp:108:38: error: redefinition of 'constexpr const int inf'
108 | constexpr int N = 1e6+20,mod = 1e9+7,inf = 1e9+10;
| ^~~
jumps.cpp:16:38: note: 'constexpr const int inf' previously defined here
16 | constexpr int N = 1e6+20,mod = 1e9+7,inf = 1e9+10;
| ^~~
jumps.cpp:109:12: error: redefinition of 'int mkay(int, int)'
109 | inline int mkay(int a,int b){
| ^~~~
jumps.cpp:17:12: note: 'int mkay(int, int)' previously defined here
17 | inline int mkay(int a,int b){
| ^~~~
jumps.cpp:115:12: error: redefinition of 'int poww(int, int)'
115 | inline int poww(int a,int k){
| ^~~~
jumps.cpp:23:12: note: 'int poww(int, int)' previously defined here
23 | inline int poww(int a,int k){
| ^~~~
jumps.cpp:125:5: error: redefinition of 'int n'
125 | int n;
| ^
jumps.cpp:33:5: note: 'int n' previously declared here
33 | int n;
| ^
jumps.cpp:126:5: error: redefinition of 'int a [1000020]'
126 | int a[N],l[N],r[N],mx[N][20],lg[N];
| ^
jumps.cpp:34:5: note: 'int a [1000020]' previously declared here
34 | int a[N],l[N],r[N],mx[N][20],lg[N];
| ^
jumps.cpp:126:10: error: redefinition of 'int l [1000020]'
126 | int a[N],l[N],r[N],mx[N][20],lg[N];
| ^
jumps.cpp:34:10: note: 'int l [1000020]' previously declared here
34 | int a[N],l[N],r[N],mx[N][20],lg[N];
| ^
jumps.cpp:126:15: error: redefinition of 'int r [1000020]'
126 | int a[N],l[N],r[N],mx[N][20],lg[N];
| ^
jumps.cpp:34:15: note: 'int r [1000020]' previously declared here
34 | int a[N],l[N],r[N],mx[N][20],lg[N];
| ^
jumps.cpp:126:20: error: redefinition of 'int mx [1000020][20]'
126 | int a[N],l[N],r[N],mx[N][20],lg[N];
| ^~
jumps.cpp:34:20: note: 'int mx [1000020][20]' previously declared here
34 | int a[N],l[N],r[N],mx[N][20],lg[N];
| ^~
jumps.cpp:126:30: error: redefinition of 'int lg [1000020]'
126 | int a[N],l[N],r[N],mx[N][20],lg[N];
| ^~
jumps.cpp:34:30: note: 'int lg [1000020]' previously declared here
34 | int a[N],l[N],r[N],mx[N][20],lg[N];
| ^~
jumps.cpp:128:5: error: redefinition of 'int rmq(int, int)'
128 | int rmq(int l,int r){
| ^~~
jumps.cpp:36:5: note: 'int rmq(int, int)' previously defined here
36 | int rmq(int l,int r){
| ^~~
jumps.cpp:133:5: error: redefinition of 'int main()'
133 | int main(){
| ^~~~
jumps.cpp:41:5: note: 'int main()' previously defined here
41 | int main(){
| ^~~~