#include <bits/stdc++.h>
#define MN 500071
using namespace std;
int n, q, A[MN];
vector<pair<int, int> > OG;
struct nod {
int delta, origm, lazy_d;
int val, ma;
};
nod V[4 * MN];
void init(int u = 1, int s = 1, int d = n) {
if(s != d) {
init(u << 1, s, (s + d) >> 1);
init(u << 1 | 1, ((s + d) >> 1) + 1, d);
V[u].origm = V[u].ma = V[u].val = max(V[u << 1].origm, V[u << 1 | 1].origm);
} else {
V[u].origm = V[u].val = V[u].ma = A[s];
}
}
void prop(int u, int s, int d) {
if(V[u].delta < V[u].lazy_d) {
V[u].delta = V[u].lazy_d;
V[u].val = V[u].delta + V[u].origm;
V[u].ma = max({V[u].ma, V[u].val});
}
if(s != d) {
V[u << 1].lazy_d = max(V[u << 1].lazy_d, V[u].lazy_d);
V[u << 1 | 1].lazy_d = max(V[u << 1 | 1].lazy_d, V[u].lazy_d);
}
V[u].lazy_d = 0;
}
void update(int val, int l, int r, int u = 1, int s = 1, int d = n) {
prop(u, s, d);
if(d < l || r < s) return;
if(l <= s && d <= r) {
V[u].lazy_d = val;
prop(u, s, d);
return;
}
update(val, l, r, u << 1, s, (s + d) >> 1);
update(val, l, r, u << 1 | 1, ((s + d) >> 1) + 1, d);
V[u].ma = max({V[u].val, V[u].ma, V[u << 1].ma, V[u << 1 | 1].ma});
}
int query(int l, int r, int u = 1, int s = 1, int d = n) {
if(d < l || r < s) return 0;
prop(u, s, d);
if(l <= s && d <= r) return V[u].ma;
return max(query(l, r, u << 1, s, (s + d) >> 1), query(l, r, u << 1 | 1, ((s + d) >> 1) + 1, d));
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n;
for(int i = 1; i <= n; ++i) cin >> A[i];
stack<int> S;
init();
for(int i = 1; i <= n; ++i) {
while(!S.empty() && A[i] > A[S.top()]) {
OG.push_back({S.top(), i});
S.pop();
}
if(!S.empty()) OG.push_back({S.top(), i});
S.push(i);
}
cin >> q;
vector<tuple<int, int, int> > Q;
for(int i = 1, st, dr; i <= q; ++i)
cin >> st >> dr, Q.push_back({st, dr, i});
vector<int> R(q + 1);
sort(Q.rbegin(), Q.rend());
sort(OG.rbegin(), OG.rend());
auto it = OG.begin();
for(auto [l, r, id] : Q) {
while(it != OG.end() && it->first >= l) {
update(A[it->first] + A[it->second], it->second * 2 - it->first, n);
//printf("Update de valoare %d de la %d la n\n", A[it->first] + A[it->second], it->second * 2 - it->first);
++it;
}
R[id] = query(l, r);
}
for(int i = 1; i <= q; ++i) cout << R[i] << "\n";
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
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 |
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 |
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 |
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 |
330 ms |
18376 KB |
Output is correct |
12 |
Correct |
328 ms |
18340 KB |
Output is correct |
13 |
Correct |
356 ms |
18208 KB |
Output is correct |
14 |
Correct |
381 ms |
18412 KB |
Output is correct |
15 |
Correct |
338 ms |
18408 KB |
Output is correct |
16 |
Correct |
357 ms |
17832 KB |
Output is correct |
17 |
Correct |
312 ms |
17644 KB |
Output is correct |
18 |
Correct |
362 ms |
17716 KB |
Output is correct |
19 |
Correct |
391 ms |
18292 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
194 ms |
17348 KB |
Output is correct |
2 |
Correct |
111 ms |
15224 KB |
Output is correct |
3 |
Correct |
107 ms |
15608 KB |
Output is correct |
4 |
Correct |
186 ms |
17360 KB |
Output is correct |
5 |
Correct |
206 ms |
17364 KB |
Output is correct |
6 |
Correct |
219 ms |
16704 KB |
Output is correct |
7 |
Correct |
187 ms |
16508 KB |
Output is correct |
8 |
Correct |
183 ms |
16580 KB |
Output is correct |
9 |
Correct |
202 ms |
16836 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
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 |
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 |
330 ms |
18376 KB |
Output is correct |
12 |
Correct |
328 ms |
18340 KB |
Output is correct |
13 |
Correct |
356 ms |
18208 KB |
Output is correct |
14 |
Correct |
381 ms |
18412 KB |
Output is correct |
15 |
Correct |
338 ms |
18408 KB |
Output is correct |
16 |
Correct |
357 ms |
17832 KB |
Output is correct |
17 |
Correct |
312 ms |
17644 KB |
Output is correct |
18 |
Correct |
362 ms |
17716 KB |
Output is correct |
19 |
Correct |
391 ms |
18292 KB |
Output is correct |
20 |
Correct |
194 ms |
17348 KB |
Output is correct |
21 |
Correct |
111 ms |
15224 KB |
Output is correct |
22 |
Correct |
107 ms |
15608 KB |
Output is correct |
23 |
Correct |
186 ms |
17360 KB |
Output is correct |
24 |
Correct |
206 ms |
17364 KB |
Output is correct |
25 |
Correct |
219 ms |
16704 KB |
Output is correct |
26 |
Correct |
187 ms |
16508 KB |
Output is correct |
27 |
Correct |
183 ms |
16580 KB |
Output is correct |
28 |
Correct |
202 ms |
16836 KB |
Output is correct |
29 |
Correct |
1084 ms |
58648 KB |
Output is correct |
30 |
Correct |
802 ms |
51520 KB |
Output is correct |
31 |
Correct |
800 ms |
53580 KB |
Output is correct |
32 |
Correct |
1045 ms |
58644 KB |
Output is correct |
33 |
Correct |
1002 ms |
58644 KB |
Output is correct |
34 |
Correct |
1038 ms |
56372 KB |
Output is correct |
35 |
Correct |
1033 ms |
56044 KB |
Output is correct |
36 |
Correct |
1016 ms |
55988 KB |
Output is correct |
37 |
Correct |
1049 ms |
57520 KB |
Output is correct |
38 |
Correct |
868 ms |
58652 KB |
Output is correct |
39 |
Correct |
827 ms |
58696 KB |
Output is correct |
40 |
Correct |
826 ms |
55268 KB |
Output is correct |
41 |
Correct |
855 ms |
54908 KB |
Output is correct |
42 |
Correct |
839 ms |
54924 KB |
Output is correct |
43 |
Correct |
831 ms |
56560 KB |
Output is correct |
44 |
Correct |
890 ms |
58660 KB |
Output is correct |
45 |
Correct |
898 ms |
58784 KB |
Output is correct |
46 |
Correct |
856 ms |
55504 KB |
Output is correct |
47 |
Correct |
860 ms |
55108 KB |
Output is correct |
48 |
Correct |
925 ms |
55148 KB |
Output is correct |
49 |
Correct |
860 ms |
57088 KB |
Output is correct |
50 |
Correct |
957 ms |
58728 KB |
Output is correct |
51 |
Correct |
903 ms |
58712 KB |
Output is correct |
52 |
Correct |
914 ms |
56180 KB |
Output is correct |
53 |
Correct |
910 ms |
55920 KB |
Output is correct |
54 |
Correct |
931 ms |
56044 KB |
Output is correct |
55 |
Correct |
936 ms |
57596 KB |
Output is correct |