#include<bits/stdc++.h>
using namespace std;
#define task "test"
#define REP(i,a,b) for(int i = (a); i<=(b); ++i)
#define REPD(i,a,b) for(int i = (a); i>=(b); --i)
#define ALL(x) (x).begin(),(x).end()
#define SZ(x) (int)(x).size()
#define BIT(x,i) ((x>>i)&1LL)
#define MASK(i) (1LL<<i)
#define fi first
#define se second
#define vi vector<int>
#define pii pair<int,int>
//-----------------------------------------------------------------------//
const int oo = 1e9,LOG = 19,MAXN = 5e5+7,N = 1e2+3;
const int MOD = 1e9+7,MOD1 = 1e9+207,MOD2 = 1e9+407,MOD3 = 998244353;
//-----------------------------------------------------------------------//
template<typename T> bool mini(T &a, T b){if(a>b){a=b;return true;} return false;}
template<typename T> bool maxi(T &a, T b){if(a<b){a=b;return true;} return false;}
int a[MAXN],f[MAXN][LOG];
int n,q;
int get(int l, int r){
int k = __lg(r-l+1);
return max(f[l][k],f[r-MASK(k)+1][k]);
}
void sub1(){
while(q--){
int res = 0,l,r; cin >> l >> r;
REP(i,l,r){
REP(j,i+1,r){
int k = j+j-i;
if(k>r) break;
maxi(res,a[i]+a[j]+get(k,r));
}
}
cout << res << "\n";
}
}
void sub2(){
cout << 0;
}
void sub3(){
while(q--){
int res = 0,l,r; cin >> l >> r;
stack<int> st; vi dp(n*2+8,0);
REPD(i,r,l){
while(SZ(st)!=0 && a[st.top()]<a[i]){
maxi(dp[2*st.top()-i],a[i]+a[st.top()]);
st.pop();
}
if(SZ(st)!=0) maxi(dp[2*st.top()-i],a[i]+a[st.top()]);
st.push(i);
}
REP(i,l,r){
maxi(dp[i],dp[i-1]);
maxi(res,dp[i]+a[i]);
}
cout << res << "\n";
}
}
void solve(){
cin >> n;
REP(i,1,n){
cin >> a[i];
f[i][0] = a[i];
}
REP(j,1,LOG-1){
REP(i,1,n){
f[i][j] = max(f[i][j-1],f[i+MASK(j-1)][j-1]);
}
}
cin >> q;
sub3(); return ;
if(n<=100 && q<=100){
sub1(); return ;
}
sub3();
}
signed main(){
ios::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
if(fopen(task".inp","r")){
freopen(task".inp","r",stdin);
freopen(task".out","w",stdout);
}
solve();
return 0;
}
Compilation message
jumps.cpp: In function 'void solve()':
jumps.cpp:77:47: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
77 | f[i][j] = max(f[i][j-1],f[i+MASK(j-1)][j-1]);
| ~^~
jumps.cpp:11:23: note: in definition of macro 'MASK'
11 | #define MASK(i) (1LL<<i)
| ^
jumps.cpp: In function 'int main()':
jumps.cpp:92:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
92 | freopen(task".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
jumps.cpp:93:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
93 | freopen(task".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Execution timed out |
4056 ms |
5228 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
27 ms |
19388 KB |
Output is correct |
2 |
Correct |
24 ms |
20056 KB |
Output is correct |
3 |
Correct |
26 ms |
19372 KB |
Output is correct |
4 |
Correct |
27 ms |
19356 KB |
Output is correct |
5 |
Correct |
27 ms |
19284 KB |
Output is correct |
6 |
Correct |
26 ms |
19292 KB |
Output is correct |
7 |
Correct |
24 ms |
19284 KB |
Output is correct |
8 |
Correct |
24 ms |
19364 KB |
Output is correct |
9 |
Correct |
24 ms |
19292 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Execution timed out |
4056 ms |
5228 KB |
Time limit exceeded |
12 |
Halted |
0 ms |
0 KB |
- |