#include <bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC optimize("Ofast")
#pragma GCC optimize("unroll-loops")
#pragma GCC target("avx,avx2,fma")
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
#define MAX 505050
#define MAXS 20
#define INF 1e9
#define bb ' '
#define ln '\n'
#define Ln '\n'
#ifdef _MSC_VER
# include <intrin.h>
# define __builtin_popcount __popcnt
#endif
#define MOD 1000000007
struct segtree {
int s;
vector<int> tree, lazy, l, r;
void init(int x = 1) {
if (x >= s) {
l[x] = r[x] = x - s + 1;
return;
}
init(x * 2);
init(x * 2 + 1);
tree[x] = max(tree[x + 1], tree[x * 2 + 1]);
l[x] = l[x * 2];
r[x] = r[x * 2 + 1];
}
segtree() {}
segtree(int N) {
s = 1 << (int)ceil(log2(N));
tree = lazy = l = r = vector<int>(2 * s + 1);
}
inline void prop(int x) {
if (!x) return;
if (x >= s) return;
for (auto c : { 2 * x, 2 * x + 1 }) {
tree[c] += lazy[x];
lazy[c] += lazy[x];
}
lazy[x] = 0;
}
inline void update(int low, int high, int x, int loc = 1) {
prop(loc);
if (low == l[loc] && high == r[loc]) {
tree[loc] += x;
lazy[loc] += x;
}
else {
if (high <= r[loc * 2]) update(low, high, x, loc * 2);
else if (low >= l[loc * 2 + 1]) update(low, high, x, loc * 2 + 1);
else update(low, r[loc * 2], x, loc * 2), update(l[loc * 2 + 1], high, x, loc * 2 + 1);
tree[loc] = max(tree[loc * 2], tree[loc * 2 + 1]);
}
}
inline int query(int low, int high, int loc = 1) {
prop(loc);
if (low == l[loc] && high == r[loc]) return tree[loc];
if (high <= r[loc * 2]) return query(low, high, loc * 2);
if (low >= l[loc * 2 + 1]) return query(low, high, loc * 2 + 1);
return max(query(low, r[loc * 2], loc * 2), query(l[loc * 2 + 1], high, loc * 2 + 1));
}
};
int A[MAX];
pii query[MAX];
vector<int> st[MAX];
int ans[MAX];
signed main() {
ios::sync_with_stdio(false), cin.tie(0);
int N;
cin >> N;
int i;
stack<int> s;
segtree seg(N);
for (i = 1; i <= N; i++) cin >> A[i], seg.tree[i + seg.s - 1] = A[i];
seg.init();
for (i = 1; i <= N; i++) {
while (s.size()) {
st[s.top()].push_back(i);
if (A[s.top()] > A[i]) break;
else s.pop();
}
s.push(i);
}
int Q;
cin >> Q;
int a, b;
vector<int> qv;
for (i = 1; i <= Q; i++) {
qv.push_back(i);
cin >> a >> b;
query[i] = pii(a, b);
}
sort(qv.begin(), qv.end(), [&](int i, int j) { return query[i].first < query[j].first; });
set<pii> vpi;
vpi.emplace(0, 0);
vpi.emplace(N + 1, INF);
for (i = N; i >= 1; i--) {
for (auto v : st[i]) {
int e = 2 * v - i;
if (e > N) continue;
int X = A[i] + A[v];
auto it = vpi.upper_bound(pii(e, INF));
it--;
if (it->second > X) continue;
if (it->first == e) {
auto pv = prev(it);
int xx = it->second;
int r = next(it)->first;
seg.update(e, r - 1, pv->second - xx);
vpi.erase(it);
it = pv;
}
int p = it->second;
auto it2 = next(it);
vector<pii> er;
for (; it2 != vpi.end(); it2++) {
if (it2->second > X) break;
auto it3 = next(it2);
seg.update(it2->first, it3->first - 1, p - it2->second);
er.push_back(*it2);
}
for (auto v : er) vpi.erase(v);
it2 = next(it);
seg.update(e, it2->first - 1, X - it->second);
vpi.emplace(e, X);
}
while (qv.size() && query[qv.back()].first == i) ans[qv.back()] = seg.query(query[qv.back()].first, query[qv.back()].second), qv.pop_back();
}
for (i = 1; i <= Q; i++) cout << ans[i] << ln;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
12116 KB |
Output is correct |
2 |
Correct |
8 ms |
12140 KB |
Output is correct |
3 |
Correct |
6 ms |
12196 KB |
Output is correct |
4 |
Correct |
7 ms |
12196 KB |
Output is correct |
5 |
Correct |
7 ms |
12116 KB |
Output is correct |
6 |
Correct |
8 ms |
12124 KB |
Output is correct |
7 |
Correct |
6 ms |
12100 KB |
Output is correct |
8 |
Correct |
7 ms |
12192 KB |
Output is correct |
9 |
Correct |
7 ms |
12316 KB |
Output is correct |
10 |
Correct |
7 ms |
12116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
12116 KB |
Output is correct |
2 |
Correct |
8 ms |
12140 KB |
Output is correct |
3 |
Correct |
6 ms |
12196 KB |
Output is correct |
4 |
Correct |
7 ms |
12196 KB |
Output is correct |
5 |
Correct |
7 ms |
12116 KB |
Output is correct |
6 |
Correct |
8 ms |
12124 KB |
Output is correct |
7 |
Correct |
6 ms |
12100 KB |
Output is correct |
8 |
Correct |
7 ms |
12192 KB |
Output is correct |
9 |
Correct |
7 ms |
12316 KB |
Output is correct |
10 |
Correct |
7 ms |
12116 KB |
Output is correct |
11 |
Correct |
616 ms |
30216 KB |
Output is correct |
12 |
Correct |
635 ms |
30376 KB |
Output is correct |
13 |
Correct |
776 ms |
30180 KB |
Output is correct |
14 |
Correct |
744 ms |
30292 KB |
Output is correct |
15 |
Correct |
705 ms |
30296 KB |
Output is correct |
16 |
Correct |
628 ms |
29640 KB |
Output is correct |
17 |
Correct |
717 ms |
29500 KB |
Output is correct |
18 |
Correct |
649 ms |
29604 KB |
Output is correct |
19 |
Correct |
669 ms |
30156 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
543 ms |
29428 KB |
Output is correct |
2 |
Correct |
237 ms |
38508 KB |
Output is correct |
3 |
Correct |
502 ms |
30024 KB |
Output is correct |
4 |
Correct |
501 ms |
29344 KB |
Output is correct |
5 |
Correct |
495 ms |
29424 KB |
Output is correct |
6 |
Correct |
497 ms |
28808 KB |
Output is correct |
7 |
Correct |
475 ms |
28664 KB |
Output is correct |
8 |
Correct |
483 ms |
28628 KB |
Output is correct |
9 |
Correct |
474 ms |
29000 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
12116 KB |
Output is correct |
2 |
Correct |
8 ms |
12140 KB |
Output is correct |
3 |
Correct |
6 ms |
12196 KB |
Output is correct |
4 |
Correct |
7 ms |
12196 KB |
Output is correct |
5 |
Correct |
7 ms |
12116 KB |
Output is correct |
6 |
Correct |
8 ms |
12124 KB |
Output is correct |
7 |
Correct |
6 ms |
12100 KB |
Output is correct |
8 |
Correct |
7 ms |
12192 KB |
Output is correct |
9 |
Correct |
7 ms |
12316 KB |
Output is correct |
10 |
Correct |
7 ms |
12116 KB |
Output is correct |
11 |
Correct |
616 ms |
30216 KB |
Output is correct |
12 |
Correct |
635 ms |
30376 KB |
Output is correct |
13 |
Correct |
776 ms |
30180 KB |
Output is correct |
14 |
Correct |
744 ms |
30292 KB |
Output is correct |
15 |
Correct |
705 ms |
30296 KB |
Output is correct |
16 |
Correct |
628 ms |
29640 KB |
Output is correct |
17 |
Correct |
717 ms |
29500 KB |
Output is correct |
18 |
Correct |
649 ms |
29604 KB |
Output is correct |
19 |
Correct |
669 ms |
30156 KB |
Output is correct |
20 |
Correct |
543 ms |
29428 KB |
Output is correct |
21 |
Correct |
237 ms |
38508 KB |
Output is correct |
22 |
Correct |
502 ms |
30024 KB |
Output is correct |
23 |
Correct |
501 ms |
29344 KB |
Output is correct |
24 |
Correct |
495 ms |
29424 KB |
Output is correct |
25 |
Correct |
497 ms |
28808 KB |
Output is correct |
26 |
Correct |
475 ms |
28664 KB |
Output is correct |
27 |
Correct |
483 ms |
28628 KB |
Output is correct |
28 |
Correct |
474 ms |
29000 KB |
Output is correct |
29 |
Correct |
2329 ms |
70720 KB |
Output is correct |
30 |
Correct |
1637 ms |
93448 KB |
Output is correct |
31 |
Correct |
2714 ms |
72064 KB |
Output is correct |
32 |
Correct |
2551 ms |
70692 KB |
Output is correct |
33 |
Correct |
2485 ms |
70688 KB |
Output is correct |
34 |
Correct |
2604 ms |
68424 KB |
Output is correct |
35 |
Correct |
2646 ms |
68040 KB |
Output is correct |
36 |
Correct |
2379 ms |
68036 KB |
Output is correct |
37 |
Correct |
2295 ms |
69508 KB |
Output is correct |
38 |
Correct |
2041 ms |
70604 KB |
Output is correct |
39 |
Correct |
2066 ms |
70704 KB |
Output is correct |
40 |
Correct |
1953 ms |
67396 KB |
Output is correct |
41 |
Correct |
2025 ms |
66908 KB |
Output is correct |
42 |
Correct |
1957 ms |
66820 KB |
Output is correct |
43 |
Correct |
2045 ms |
68604 KB |
Output is correct |
44 |
Correct |
2132 ms |
70684 KB |
Output is correct |
45 |
Correct |
2161 ms |
70732 KB |
Output is correct |
46 |
Correct |
2154 ms |
67500 KB |
Output is correct |
47 |
Correct |
2107 ms |
67116 KB |
Output is correct |
48 |
Correct |
2031 ms |
67212 KB |
Output is correct |
49 |
Correct |
2133 ms |
69224 KB |
Output is correct |
50 |
Correct |
2181 ms |
70712 KB |
Output is correct |
51 |
Correct |
2168 ms |
70776 KB |
Output is correct |
52 |
Correct |
2188 ms |
68284 KB |
Output is correct |
53 |
Correct |
2204 ms |
67980 KB |
Output is correct |
54 |
Correct |
2167 ms |
67984 KB |
Output is correct |
55 |
Correct |
2113 ms |
69740 KB |
Output is correct |