/* Author : Mychecksdead */
#include<bits/stdc++.h>
using namespace std;
#define ll long long int
#define MOD (1000000000+7)
#define MOD1 (998244353)
#define pb push_back
#define all(x) x.begin(), x.end()
#define en cout << '\n'
#define ff first
#define ss second
#define pii pair<int,int>
#define vi vector<int>
const int N = 2e6+100, M = 1e5+10, K = 22, MX = 30;
struct Fenwick{
int n;
vector<int> t;
Fenwick(int _n){
n = _n;
t.resize(n+1, 0);
}
void add(int v){
while(v <= n){
t[v]++;
v += (v&-v);
}
}
int get(int v){
int res = 0;
while(v > 0){
res += t[v];
v -= (v&-v);
}
return res;
}
};
int n, a[N], q, L[N], R[N], rmq[N][K], go[N], T[N], C[N], ans[N], COMB[N];
vector<pii> Q[N], to[N];
int comb(int x, int y){
return max(x,y);
}
int get(int l, int r){
int k = int(log2(r-l+1));
return max(rmq[l][k], rmq[r-(1<<k)+1][k]);
}
void build(int l, int r, int k){
C[k] = 0;
if(l == r){
T[k] = a[l];
COMB[k] = a[l];
return;
}
int m = l+r>>1;
build(l, m, k<<1);
build(m+1, r, k<<1|1);
COMB[k] = comb(COMB[k<<1], COMB[k<<1|1]);
T[k] = comb(T[k<<1], T[k<<1|1]);
}
int get_max(int l, int r, int ql, int qr, int k){
if(ql > r || l > qr) return 0;
if(ql <= l && r <= qr) return max(COMB[k], T[k]+C[k]);
C[k<<1] = max(C[k<<1], C[k]);
C[k<<1|1] = max(C[k<<1|1], C[k]);
int m = l+r>>1;
auto v = comb(get_max(l, m, ql, qr, k<<1), get_max(m+1, r, ql, qr, k<<1|1));
return v;
}
void upd(int l, int r, int ql, int qr, int k, int val){
if(ql > r || l > qr) return;
if(ql <= l && r <= qr){
C[k] = max(val, C[k]);
COMB[k] = max(COMB[k], C[k] + T[k]);
// cout << COMB[k] << '\n';
return;
}
C[k<<1] = max(C[k<<1], C[k]);
C[k<<1|1] = max(C[k<<1|1], C[k]);
int m = l+r>>1;
upd(l, m, ql, qr, k<<1, val);
upd(m+1, r, ql, qr, k<<1|1, val);
COMB[k] = max({COMB[k<<1], COMB[k<<1|1], C[k] + T[k]});
// cout << COMB[k] << ' '<< l << ' ' << r << '\n';
}
void solve(){
cin >> n;
for(int i = 1; i <= n; ++i){
cin >> a[i];
rmq[i][0] = a[i];
}
deque<int> A;
for(int i = 1; i <= n; ++i){
while(A.size() && a[A.back()] < a[i]) A.pop_back();
L[i] = A.size() == 0 ? - 1 : A.back();
A.pb(i);
if(L[i] > -1){
to[L[i]].pb({a[i] + a[L[i]], i + i - L[i]});
}
}
A.clear();
for(int i = n; i >= 1; --i){
while(A.size() && a[A.back()] < a[i]) A.pop_back();
R[i] = A.size() == 0 ? - 1 : A.back();
A.pb(i);
if(R[i] > -1)
to[i].pb({a[i] + a[R[i]], R[i] + R[i] - i});
}
for(int j = 1; j < K; ++j){
for(int i = 1; i + (1<<j) <= n + 1; ++i){
rmq[i][j] = max(rmq[i][j-1], rmq[i+(1<<(j-1))][j-1]);
}
}
build(1, n, 1);
cin >> q;
for(int i = 1; i <= q; ++i){
int l, r; cin >> l >> r;
Q[l].pb({r, i});
}
for(int i = n; i >= 1; --i){
for(auto p: to[i]){
if(p.ss <= n){
// cout << p.ff << ' ' << p.ss << ' ' << i << '\n';
upd(1, n, p.ss, n, 1, p.ff);
}
}
for(auto p: Q[i]){
ans[p.ss] = get_max(1, n, i, p.ff, 1);
}
}
for(int i = 1; i <= q; ++i){
cout << ans[i] << '\n';
}
}
int main(){
cin.tie(0); ios::sync_with_stdio(0);
int tt = 1, aa;
// freopen("in.txt", "r", stdin);
// freopen("out.txt", "w", stdout);
while(tt--){
solve();
en;
}
cerr<<"time taken : "<<(float)clock()/CLOCKS_PER_SEC<<" seconds\n";
return 0;
}
Compilation message
jumps.cpp: In function 'void build(int, int, int)':
jumps.cpp:56:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
56 | int m = l+r>>1;
| ~^~
jumps.cpp: In function 'int get_max(int, int, int, int, int)':
jumps.cpp:67:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
67 | int m = l+r>>1;
| ~^~
jumps.cpp: In function 'void upd(int, int, int, int, int, int)':
jumps.cpp:81:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
81 | int m = l+r>>1;
| ~^~
jumps.cpp: In function 'int main()':
jumps.cpp:141:15: warning: unused variable 'aa' [-Wunused-variable]
141 | int tt = 1, aa;
| ^~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
109392 KB |
Output is correct |
2 |
Correct |
20 ms |
109392 KB |
Output is correct |
3 |
Correct |
20 ms |
109392 KB |
Output is correct |
4 |
Correct |
20 ms |
109392 KB |
Output is correct |
5 |
Correct |
19 ms |
109560 KB |
Output is correct |
6 |
Correct |
18 ms |
109392 KB |
Output is correct |
7 |
Correct |
19 ms |
109392 KB |
Output is correct |
8 |
Correct |
19 ms |
109376 KB |
Output is correct |
9 |
Correct |
19 ms |
109392 KB |
Output is correct |
10 |
Correct |
20 ms |
109392 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
109392 KB |
Output is correct |
2 |
Correct |
20 ms |
109392 KB |
Output is correct |
3 |
Correct |
20 ms |
109392 KB |
Output is correct |
4 |
Correct |
20 ms |
109392 KB |
Output is correct |
5 |
Correct |
19 ms |
109560 KB |
Output is correct |
6 |
Correct |
18 ms |
109392 KB |
Output is correct |
7 |
Correct |
19 ms |
109392 KB |
Output is correct |
8 |
Correct |
19 ms |
109376 KB |
Output is correct |
9 |
Correct |
19 ms |
109392 KB |
Output is correct |
10 |
Correct |
20 ms |
109392 KB |
Output is correct |
11 |
Correct |
211 ms |
128216 KB |
Output is correct |
12 |
Correct |
216 ms |
128228 KB |
Output is correct |
13 |
Correct |
208 ms |
128072 KB |
Output is correct |
14 |
Correct |
206 ms |
128076 KB |
Output is correct |
15 |
Correct |
220 ms |
128160 KB |
Output is correct |
16 |
Correct |
205 ms |
127560 KB |
Output is correct |
17 |
Correct |
213 ms |
127560 KB |
Output is correct |
18 |
Correct |
211 ms |
127564 KB |
Output is correct |
19 |
Correct |
221 ms |
128108 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
137 ms |
145228 KB |
Output is correct |
2 |
Correct |
87 ms |
144712 KB |
Output is correct |
3 |
Correct |
84 ms |
144712 KB |
Output is correct |
4 |
Correct |
125 ms |
145272 KB |
Output is correct |
5 |
Correct |
128 ms |
145224 KB |
Output is correct |
6 |
Correct |
128 ms |
144460 KB |
Output is correct |
7 |
Correct |
131 ms |
144456 KB |
Output is correct |
8 |
Correct |
121 ms |
144368 KB |
Output is correct |
9 |
Correct |
124 ms |
144712 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
19 ms |
109392 KB |
Output is correct |
2 |
Correct |
20 ms |
109392 KB |
Output is correct |
3 |
Correct |
20 ms |
109392 KB |
Output is correct |
4 |
Correct |
20 ms |
109392 KB |
Output is correct |
5 |
Correct |
19 ms |
109560 KB |
Output is correct |
6 |
Correct |
18 ms |
109392 KB |
Output is correct |
7 |
Correct |
19 ms |
109392 KB |
Output is correct |
8 |
Correct |
19 ms |
109376 KB |
Output is correct |
9 |
Correct |
19 ms |
109392 KB |
Output is correct |
10 |
Correct |
20 ms |
109392 KB |
Output is correct |
11 |
Correct |
211 ms |
128216 KB |
Output is correct |
12 |
Correct |
216 ms |
128228 KB |
Output is correct |
13 |
Correct |
208 ms |
128072 KB |
Output is correct |
14 |
Correct |
206 ms |
128076 KB |
Output is correct |
15 |
Correct |
220 ms |
128160 KB |
Output is correct |
16 |
Correct |
205 ms |
127560 KB |
Output is correct |
17 |
Correct |
213 ms |
127560 KB |
Output is correct |
18 |
Correct |
211 ms |
127564 KB |
Output is correct |
19 |
Correct |
221 ms |
128108 KB |
Output is correct |
20 |
Correct |
137 ms |
145228 KB |
Output is correct |
21 |
Correct |
87 ms |
144712 KB |
Output is correct |
22 |
Correct |
84 ms |
144712 KB |
Output is correct |
23 |
Correct |
125 ms |
145272 KB |
Output is correct |
24 |
Correct |
128 ms |
145224 KB |
Output is correct |
25 |
Correct |
128 ms |
144460 KB |
Output is correct |
26 |
Correct |
131 ms |
144456 KB |
Output is correct |
27 |
Correct |
121 ms |
144368 KB |
Output is correct |
28 |
Correct |
124 ms |
144712 KB |
Output is correct |
29 |
Correct |
712 ms |
217416 KB |
Output is correct |
30 |
Correct |
527 ms |
216392 KB |
Output is correct |
31 |
Correct |
575 ms |
214600 KB |
Output is correct |
32 |
Correct |
681 ms |
217416 KB |
Output is correct |
33 |
Correct |
729 ms |
217416 KB |
Output is correct |
34 |
Correct |
664 ms |
215104 KB |
Output is correct |
35 |
Correct |
669 ms |
214856 KB |
Output is correct |
36 |
Correct |
710 ms |
215044 KB |
Output is correct |
37 |
Correct |
785 ms |
216420 KB |
Output is correct |
38 |
Correct |
549 ms |
223156 KB |
Output is correct |
39 |
Correct |
572 ms |
223160 KB |
Output is correct |
40 |
Correct |
527 ms |
219976 KB |
Output is correct |
41 |
Correct |
545 ms |
218360 KB |
Output is correct |
42 |
Correct |
535 ms |
218448 KB |
Output is correct |
43 |
Correct |
550 ms |
221000 KB |
Output is correct |
44 |
Correct |
589 ms |
222536 KB |
Output is correct |
45 |
Correct |
556 ms |
222600 KB |
Output is correct |
46 |
Correct |
524 ms |
219408 KB |
Output is correct |
47 |
Correct |
524 ms |
219112 KB |
Output is correct |
48 |
Correct |
535 ms |
218956 KB |
Output is correct |
49 |
Correct |
595 ms |
221032 KB |
Output is correct |
50 |
Correct |
696 ms |
220592 KB |
Output is correct |
51 |
Correct |
645 ms |
220660 KB |
Output is correct |
52 |
Correct |
605 ms |
218184 KB |
Output is correct |
53 |
Correct |
637 ms |
218016 KB |
Output is correct |
54 |
Correct |
642 ms |
217976 KB |
Output is correct |
55 |
Correct |
693 ms |
219464 KB |
Output is correct |