#include<bits/stdc++.h>
#define forin(i,a,b) for(int i=a;i<=b;++i)
#define forde(i,a,b) for(int i=a;i>=b;--i)
#define ll long long
using namespace std;
const int N = 5e5 + 10;
int n, q;
ll d[N], tr[N << 2], lazy[N << 2], ans[N], sp[20][N];
struct query {
int f, l, pos;
};
query r[N];
void build() {
forin(i,1,n) sp[0][i] = d[i];
forin(j,1,log2(n)) {
forin(i,1,n - (1 << j - 1) + 1) {
sp[j][i] = max(sp[j - 1][i], sp[j - 1][i + (1 << j - 1)]);
}
}
}
ll mx(int f, int l) {
if(f == l) return d[f];
int j = log2(l - f + 1);
return max(sp[j][f], sp[j][l - (1 << j) + 1]);
}
void push(int id, int f, int l) {
ll t = lazy[id];
if(t) {
tr[id] = max(tr[id], t + mx(f, l));
if(f < l) {
lazy[id << 1] = max(lazy[id << 1], t);
lazy[id << 1 | 1] = max(lazy[id << 1 | 1], t);
}
lazy[id] = 0;
}
}
void up(int u, int v, ll val, int id = 1, int f = 1, int l = n) {
push(id, f, l);
if(v < f || l < u) return;
if(u <= f && l <= v) {
lazy[id] = max(lazy[id], val);
push(id, f, l);
return;
}
int mid = f + l >> 1;
up(u, v, val, id << 1, f, mid);
up(u, v, val, id << 1 | 1, mid + 1, l);
tr[id] = max(tr[id << 1], tr[id << 1 | 1]);
}
ll get(int u, int v, int id = 1, int f = 1, int l = n) {
push(id, f, l);
if(v < f || l < u) return 0;
if(u <= f && l <= v) return tr[id];
int mid = f + l >> 1;
return max(get(u, v, id << 1, f, mid), get(u, v, id << 1 | 1, mid + 1, l));
}
int main () {
cin.tie(0)->sync_with_stdio(0);
if(fopen("Task.inp","r")) {
freopen("Task.inp","r",stdin);
freopen("WA.out","w",stdout);
}
cin>>n;
forin(i,1,n) cin>>d[i];
build();
cin>>q;
forin(i,1,q) {
r[i].pos = i;
cin>>r[i].f>>r[i].l;
}
sort(r + 1, r + q + 1, [] (query a, query b) {
return a.f > b.f;
});
stack<int> st;
vector<pair<int, int>> vt;
vt.push_back({0, 0});
forde(i,n,1) {
while(!st.empty() && d[st.top()] <= d[i]) {
vt.push_back({i, st.top()});
st.pop();
}
if(!st.empty()) vt.push_back({i, st.top()});
st.push(i);
}
int j = 1;
forin(i,1,q) {
while(j <= vt.size() - 1 && vt[j].first >= r[i].f) {
int a, b; tie(a, b) = vt[j++];
if(2 * b - a > n) continue;
up(2 * b - a, n, d[a] + d[b]);
}
ans[r[i].pos] = get(r[i].f, r[i].l);
}
forin(i,1,q) cout<<ans[i]<<"\n";
}
Compilation message
jumps.cpp: In function 'void build()':
jumps.cpp:16:31: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
16 | forin(i,1,n - (1 << j - 1) + 1) {
| ~~^~~
jumps.cpp:2:37: note: in definition of macro 'forin'
2 | #define forin(i,a,b) for(int i=a;i<=b;++i)
| ^
jumps.cpp:17:64: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
17 | sp[j][i] = max(sp[j - 1][i], sp[j - 1][i + (1 << j - 1)]);
| ~~^~~
jumps.cpp: In function 'void up(int, int, long long int, int, int, int)':
jumps.cpp:45:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
45 | int mid = f + l >> 1;
| ~~^~~
jumps.cpp: In function 'long long int get(int, int, int, int, int)':
jumps.cpp:54:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
54 | int mid = f + l >> 1;
| ~~^~~
jumps.cpp: In function 'int main()':
jumps.cpp:87:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
87 | while(j <= vt.size() - 1 && vt[j].first >= r[i].f) {
| ~~^~~~~~~~~~~~~~~~
jumps.cpp:60:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
60 | freopen("Task.inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
jumps.cpp:61:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
61 | freopen("WA.out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
435 ms |
16320 KB |
Output is correct |
12 |
Correct |
376 ms |
16028 KB |
Output is correct |
13 |
Correct |
410 ms |
15964 KB |
Output is correct |
14 |
Correct |
391 ms |
16140 KB |
Output is correct |
15 |
Correct |
377 ms |
16136 KB |
Output is correct |
16 |
Correct |
390 ms |
15564 KB |
Output is correct |
17 |
Correct |
381 ms |
15596 KB |
Output is correct |
18 |
Correct |
374 ms |
15488 KB |
Output is correct |
19 |
Correct |
392 ms |
16084 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
237 ms |
40692 KB |
Output is correct |
2 |
Correct |
133 ms |
39760 KB |
Output is correct |
3 |
Correct |
131 ms |
39028 KB |
Output is correct |
4 |
Correct |
234 ms |
40644 KB |
Output is correct |
5 |
Correct |
236 ms |
40632 KB |
Output is correct |
6 |
Correct |
232 ms |
40584 KB |
Output is correct |
7 |
Correct |
240 ms |
40632 KB |
Output is correct |
8 |
Correct |
231 ms |
40668 KB |
Output is correct |
9 |
Correct |
230 ms |
40612 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
435 ms |
16320 KB |
Output is correct |
12 |
Correct |
376 ms |
16028 KB |
Output is correct |
13 |
Correct |
410 ms |
15964 KB |
Output is correct |
14 |
Correct |
391 ms |
16140 KB |
Output is correct |
15 |
Correct |
377 ms |
16136 KB |
Output is correct |
16 |
Correct |
390 ms |
15564 KB |
Output is correct |
17 |
Correct |
381 ms |
15596 KB |
Output is correct |
18 |
Correct |
374 ms |
15488 KB |
Output is correct |
19 |
Correct |
392 ms |
16084 KB |
Output is correct |
20 |
Correct |
237 ms |
40692 KB |
Output is correct |
21 |
Correct |
133 ms |
39760 KB |
Output is correct |
22 |
Correct |
131 ms |
39028 KB |
Output is correct |
23 |
Correct |
234 ms |
40644 KB |
Output is correct |
24 |
Correct |
236 ms |
40632 KB |
Output is correct |
25 |
Correct |
232 ms |
40584 KB |
Output is correct |
26 |
Correct |
240 ms |
40632 KB |
Output is correct |
27 |
Correct |
231 ms |
40668 KB |
Output is correct |
28 |
Correct |
230 ms |
40612 KB |
Output is correct |
29 |
Correct |
1838 ms |
115884 KB |
Output is correct |
30 |
Correct |
1592 ms |
113684 KB |
Output is correct |
31 |
Correct |
1649 ms |
111948 KB |
Output is correct |
32 |
Correct |
1903 ms |
115824 KB |
Output is correct |
33 |
Correct |
1832 ms |
115728 KB |
Output is correct |
34 |
Correct |
1857 ms |
115160 KB |
Output is correct |
35 |
Correct |
1862 ms |
115188 KB |
Output is correct |
36 |
Correct |
1864 ms |
115220 KB |
Output is correct |
37 |
Correct |
1853 ms |
115644 KB |
Output is correct |
38 |
Correct |
1213 ms |
115792 KB |
Output is correct |
39 |
Correct |
1205 ms |
115784 KB |
Output is correct |
40 |
Correct |
1222 ms |
114096 KB |
Output is correct |
41 |
Correct |
1170 ms |
113744 KB |
Output is correct |
42 |
Correct |
1166 ms |
113804 KB |
Output is correct |
43 |
Correct |
1210 ms |
114788 KB |
Output is correct |
44 |
Correct |
1309 ms |
115680 KB |
Output is correct |
45 |
Correct |
1314 ms |
115760 KB |
Output is correct |
46 |
Correct |
1314 ms |
114428 KB |
Output is correct |
47 |
Correct |
1328 ms |
114104 KB |
Output is correct |
48 |
Correct |
1365 ms |
114188 KB |
Output is correct |
49 |
Correct |
1283 ms |
115452 KB |
Output is correct |
50 |
Correct |
1518 ms |
115812 KB |
Output is correct |
51 |
Correct |
1559 ms |
115828 KB |
Output is correct |
52 |
Correct |
1596 ms |
115088 KB |
Output is correct |
53 |
Correct |
1451 ms |
114884 KB |
Output is correct |
54 |
Correct |
1468 ms |
115040 KB |
Output is correct |
55 |
Correct |
1471 ms |
115752 KB |
Output is correct |