#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
#define int 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));
}
int32_t 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:17:31: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
17 | 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:18:64: warning: suggest parentheses around '-' inside '<<' [-Wparentheses]
18 | sp[j][i] = max(sp[j - 1][i], sp[j - 1][i + (1 << j - 1)]);
| ~~^~~
jumps.cpp: In function 'void up(long long int, long long int, long long int, long long int, long long int, long long int)':
jumps.cpp:46:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
46 | int mid = f + l >> 1;
| ~~^~~
jumps.cpp: In function 'long long int get(long long int, long long int, long long int, long long int, long long int)':
jumps.cpp:55:17: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
55 | int mid = f + l >> 1;
| ~~^~~
jumps.cpp: In function 'int32_t main()':
jumps.cpp:88:17: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
88 | while(j <= vt.size() - 1 && vt[j].first >= r[i].f) {
| ~~^~~~~~~~~~~~~~~~
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("Task.inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
jumps.cpp:62:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
62 | freopen("WA.out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 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 |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 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 |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 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 |
385 ms |
22088 KB |
Output is correct |
12 |
Correct |
366 ms |
22016 KB |
Output is correct |
13 |
Correct |
368 ms |
22100 KB |
Output is correct |
14 |
Correct |
374 ms |
22064 KB |
Output is correct |
15 |
Correct |
387 ms |
22132 KB |
Output is correct |
16 |
Correct |
380 ms |
21512 KB |
Output is correct |
17 |
Correct |
370 ms |
21460 KB |
Output is correct |
18 |
Correct |
368 ms |
21448 KB |
Output is correct |
19 |
Correct |
373 ms |
21956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
227 ms |
43712 KB |
Output is correct |
2 |
Correct |
126 ms |
42160 KB |
Output is correct |
3 |
Correct |
148 ms |
40644 KB |
Output is correct |
4 |
Correct |
235 ms |
43800 KB |
Output is correct |
5 |
Correct |
240 ms |
43820 KB |
Output is correct |
6 |
Correct |
240 ms |
43720 KB |
Output is correct |
7 |
Correct |
224 ms |
43740 KB |
Output is correct |
8 |
Correct |
232 ms |
43768 KB |
Output is correct |
9 |
Correct |
230 ms |
43740 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
340 KB |
Output is correct |
2 |
Correct |
0 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 |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 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 |
385 ms |
22088 KB |
Output is correct |
12 |
Correct |
366 ms |
22016 KB |
Output is correct |
13 |
Correct |
368 ms |
22100 KB |
Output is correct |
14 |
Correct |
374 ms |
22064 KB |
Output is correct |
15 |
Correct |
387 ms |
22132 KB |
Output is correct |
16 |
Correct |
380 ms |
21512 KB |
Output is correct |
17 |
Correct |
370 ms |
21460 KB |
Output is correct |
18 |
Correct |
368 ms |
21448 KB |
Output is correct |
19 |
Correct |
373 ms |
21956 KB |
Output is correct |
20 |
Correct |
227 ms |
43712 KB |
Output is correct |
21 |
Correct |
126 ms |
42160 KB |
Output is correct |
22 |
Correct |
148 ms |
40644 KB |
Output is correct |
23 |
Correct |
235 ms |
43800 KB |
Output is correct |
24 |
Correct |
240 ms |
43820 KB |
Output is correct |
25 |
Correct |
240 ms |
43720 KB |
Output is correct |
26 |
Correct |
224 ms |
43740 KB |
Output is correct |
27 |
Correct |
232 ms |
43768 KB |
Output is correct |
28 |
Correct |
230 ms |
43740 KB |
Output is correct |
29 |
Correct |
1776 ms |
129448 KB |
Output is correct |
30 |
Correct |
1432 ms |
125904 KB |
Output is correct |
31 |
Correct |
1451 ms |
121760 KB |
Output is correct |
32 |
Correct |
1806 ms |
129504 KB |
Output is correct |
33 |
Correct |
1735 ms |
129480 KB |
Output is correct |
34 |
Correct |
1841 ms |
128752 KB |
Output is correct |
35 |
Correct |
1751 ms |
128808 KB |
Output is correct |
36 |
Correct |
1783 ms |
128760 KB |
Output is correct |
37 |
Correct |
1695 ms |
129316 KB |
Output is correct |
38 |
Correct |
1210 ms |
129544 KB |
Output is correct |
39 |
Correct |
1147 ms |
129432 KB |
Output is correct |
40 |
Correct |
1124 ms |
127904 KB |
Output is correct |
41 |
Correct |
1122 ms |
127512 KB |
Output is correct |
42 |
Correct |
1113 ms |
127600 KB |
Output is correct |
43 |
Correct |
1154 ms |
128448 KB |
Output is correct |
44 |
Correct |
1226 ms |
129516 KB |
Output is correct |
45 |
Correct |
1218 ms |
129556 KB |
Output is correct |
46 |
Correct |
1254 ms |
128008 KB |
Output is correct |
47 |
Correct |
1226 ms |
127828 KB |
Output is correct |
48 |
Correct |
1222 ms |
127820 KB |
Output is correct |
49 |
Correct |
1240 ms |
129164 KB |
Output is correct |
50 |
Correct |
1376 ms |
129428 KB |
Output is correct |
51 |
Correct |
1415 ms |
129576 KB |
Output is correct |
52 |
Correct |
1409 ms |
128664 KB |
Output is correct |
53 |
Correct |
1346 ms |
128568 KB |
Output is correct |
54 |
Correct |
1339 ms |
128560 KB |
Output is correct |
55 |
Correct |
1404 ms |
129312 KB |
Output is correct |