#include <bits/stdc++.h>
using namespace std;
constexpr int inf = 1e9 + 7;
vector<int> t, tag;
int sz = 1;
void pull(int x) {
t[x] = max(t[x << 1], t[x << 1 | 1]) + tag[x];
}
void init(int n, vector<int> a) {
sz = 1 << (__lg(n) + bool(n & (n - 1)));
t.assign(sz << 1, {}), tag.assign(sz << 1, {});
for (int i = 0; i < n; ++i) {
t[i + sz] = a[i];
}
for (int i = sz - 1; i > 0; --i) {
pull(i);
}
}
void push(int x) {
for (int k : {x << 1, x << 1 | 1}) {
tag[k] += tag[x];
t[k] += tag[x];
}
tag[x] = 0;
}
void rangeAdd(int l, int r, int val, int x = 1, int lx = 0, int rx = sz) {
if (l >= rx || lx >= r) return;
if (l <= lx && rx <= r) {
tag[x] += val;
t[x] += val;
return;
}
int mid = (lx + rx) >> 1;
rangeAdd(l, r, val, x << 1, lx, mid), rangeAdd(l, r, val, x << 1 | 1, mid, rx);
pull(x);
}
int rangeQuery(int l, int r, int x = 1, int lx = 0, int rx = sz) {
if (l >= rx || lx >= r) return -inf;
if (l <= lx && rx <= r) return t[x];
int mid = (lx + rx) >> 1;
return max(rangeQuery(l, r, x << 1, lx, mid), rangeQuery(l, r, x << 1 | 1, mid, rx)) + tag[x];
}
int main() {
ios::sync_with_stdio(false);
cin.tie(nullptr);
int n;
cin >> n;
vector<int> a(n);
for (int i = 0; i < n; ++i) {
cin >> a[i];
}
vector<array<int, 3>> events;
auto relax = [&](int i, int j) {
int r = j + (j - i);
if (r < n) {
events.push_back({i, j, -1});
}
};
vector<int> stk;
for (int i = 0; i < n; ++i) {
while (!stk.empty() && a[stk.back()] <= a[i]) {
relax(stk.back(), i);
stk.pop_back();
}
if (!stk.empty()) {
relax(stk.back(), i);
}
stk.push_back(i);
}
int q;
cin >> q;
for (int i = 0; i < q; ++i) {
int l, r;
cin >> l >> r;
events.push_back({l - 1, r, i});
}
vector<int> ans(q);
sort(events.begin(), events.end(), [&](array<int, 3> x, array<int, 3> y) {
return x[0] != y[0] ? x[0] > y[0] : x[2] < y[2];
});
init(n, a);
set<array<int, 3>> st;
for (auto [i, j, tp]: events) {
if (tp > -1) {
ans[tp] = rangeQuery(i, j);
} else {
int k = j + (j - i);
int sum = a[i] + a[j];
auto it = st.lower_bound({k + 1, -1, -1});
if (it != st.begin() && (*prev(it))[2] >= sum) {
continue;
}
int r = n;
while (true) {
it = st.lower_bound({k, -1, -1});
if (it == st.end()) {
r = n;
break;
}
auto [x, y, s] = *it;
if (s > sum) {
r = (*it)[0];
break;
}
rangeAdd(x, y, -s);
st.erase(it);
}
if (it != st.begin()) {
auto [x, y, s] = *prev(it);
assert(x < k);
assert(y >= k);
assert(s < sum);
st.erase(prev(it));
rangeAdd(k, y, -s);
st.insert({x, k, s});
}
rangeAdd(k, r, sum);
st.insert({k, r, sum});
// for (auto [l, r, k]: st) {
// cout << l << " " << r << " " << k << endl;
// }
// for (int i = 0; i < n; ++i) {
// cout << rangeQuery(i, i + 1) - a[i] << " ";
// }
// cout << endl;
// cout << endl;
}
}
for (int i = 0; i < q; ++i) {
cout << ans[i] << "\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
316 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
316 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
275 ms |
14408 KB |
Output is correct |
12 |
Correct |
279 ms |
13940 KB |
Output is correct |
13 |
Correct |
259 ms |
13684 KB |
Output is correct |
14 |
Correct |
274 ms |
13736 KB |
Output is correct |
15 |
Correct |
270 ms |
13876 KB |
Output is correct |
16 |
Correct |
263 ms |
13008 KB |
Output is correct |
17 |
Correct |
294 ms |
14588 KB |
Output is correct |
18 |
Correct |
293 ms |
14612 KB |
Output is correct |
19 |
Correct |
299 ms |
15208 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
249 ms |
10960 KB |
Output is correct |
2 |
Correct |
184 ms |
20316 KB |
Output is correct |
3 |
Correct |
160 ms |
9628 KB |
Output is correct |
4 |
Correct |
268 ms |
10948 KB |
Output is correct |
5 |
Correct |
258 ms |
10932 KB |
Output is correct |
6 |
Correct |
245 ms |
10928 KB |
Output is correct |
7 |
Correct |
246 ms |
11180 KB |
Output is correct |
8 |
Correct |
244 ms |
11192 KB |
Output is correct |
9 |
Correct |
271 ms |
11184 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
316 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
275 ms |
14408 KB |
Output is correct |
12 |
Correct |
279 ms |
13940 KB |
Output is correct |
13 |
Correct |
259 ms |
13684 KB |
Output is correct |
14 |
Correct |
274 ms |
13736 KB |
Output is correct |
15 |
Correct |
270 ms |
13876 KB |
Output is correct |
16 |
Correct |
263 ms |
13008 KB |
Output is correct |
17 |
Correct |
294 ms |
14588 KB |
Output is correct |
18 |
Correct |
293 ms |
14612 KB |
Output is correct |
19 |
Correct |
299 ms |
15208 KB |
Output is correct |
20 |
Correct |
249 ms |
10960 KB |
Output is correct |
21 |
Correct |
184 ms |
20316 KB |
Output is correct |
22 |
Correct |
160 ms |
9628 KB |
Output is correct |
23 |
Correct |
268 ms |
10948 KB |
Output is correct |
24 |
Correct |
258 ms |
10932 KB |
Output is correct |
25 |
Correct |
245 ms |
10928 KB |
Output is correct |
26 |
Correct |
246 ms |
11180 KB |
Output is correct |
27 |
Correct |
244 ms |
11192 KB |
Output is correct |
28 |
Correct |
271 ms |
11184 KB |
Output is correct |
29 |
Correct |
1091 ms |
38936 KB |
Output is correct |
30 |
Correct |
875 ms |
62432 KB |
Output is correct |
31 |
Correct |
802 ms |
34992 KB |
Output is correct |
32 |
Correct |
1087 ms |
39036 KB |
Output is correct |
33 |
Correct |
1123 ms |
38988 KB |
Output is correct |
34 |
Correct |
1044 ms |
38288 KB |
Output is correct |
35 |
Correct |
1068 ms |
37524 KB |
Output is correct |
36 |
Correct |
1054 ms |
37600 KB |
Output is correct |
37 |
Correct |
1052 ms |
38176 KB |
Output is correct |
38 |
Correct |
934 ms |
38380 KB |
Output is correct |
39 |
Correct |
911 ms |
38352 KB |
Output is correct |
40 |
Correct |
961 ms |
36640 KB |
Output is correct |
41 |
Correct |
923 ms |
36372 KB |
Output is correct |
42 |
Correct |
893 ms |
36372 KB |
Output is correct |
43 |
Correct |
939 ms |
37272 KB |
Output is correct |
44 |
Incorrect |
968 ms |
38296 KB |
Output isn't correct |
45 |
Halted |
0 ms |
0 KB |
- |