/*
Just consider the pair(a, b) such that with every a < k < b, a[a] > a[k] and a[b] > a[k]
The number of these pairs is just O(n): the proof can be expressed by the code finding these pairs
The rest is quite easy
*/
#include<bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
#define sz(x) ( (int)(x).size() )
using LL = long long;
const int inf = 1e9;
mt19937 rng( (uint32_t)chrono::steady_clock::now().time_since_epoch().count() );
template<class T>
inline bool asMn(T &a, const T &b) { return a > b ? a = b, true : false; }
template<class T>
inline bool asMx(T &a, const T &b) { return a < b ? a = b, true : false; }
int n;
vector<int> a;
struct Node {
int mxA, mxOth, mx;
Node(int _mxA = 0, int _mxOth = 0, int _mx = 0) : mxA(_mxA), mxOth(_mxOth), mx(_mx) {}
};
struct It {
vector<Node> node;
It(int nNode) { node.assign(nNode, 0); }
void build(int i = 1, int Left = 0, int Right = n - 1) {
if (Left == Right) {
node[i] = Node(a[Left], -inf, -inf);
return ;
}
int Mid = (Left + Right) >> 1;
build(i << 1, Left, Mid);
build(i << 1 | 1, Mid + 1, Right);
node[i].mxA = max(node[i << 1].mxA, node[i << 1 | 1].mxA);
}
void upd(int l, int r, int val, int i = 1, int Left = 0, int Right = n - 1) {
if (i != 1) {
asMx(node[i].mxOth, node[i >> 1].mxOth);
asMx(node[i].mx, node[i].mxOth + node[i].mxA);
}
if (Right < l || r < Left) return ;
if (l <= Left && Right <= r) {
asMx(node[i].mxOth, val); asMx(node[i].mx, node[i].mxOth + node[i].mxA);
return ;
}
int Mid = (Left + Right) >> 1;
upd(l, r, val, i << 1, Left, Mid);
upd(l, r, val, i << 1 | 1, Mid + 1, Right);
node[i].mx = max(node[i << 1].mx, node[i << 1 | 1].mx);
}
int getMx(int l, int r, int i = 1, int Left = 0, int Right = n - 1) {
if (i != 1) {
asMx(node[i].mxOth, node[i >> 1].mxOth);
asMx(node[i].mx, node[i].mxOth + node[i].mxA);
}
if (Right < l || r < Left) return -inf;
if (l <= Left && Right <= r) return node[i].mx;
int Mid = (Left + Right) >> 1;
return max(getMx(l, r, i << 1, Left, Mid), getMx(l, r, i << 1 | 1, Mid + 1, Right) );
}
};
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
#ifdef FourLeafClover
freopen("input", "r", stdin);
#endif // FourLeafCLover
cin >> n;
a.assign(n, 0);
for (int i = 0; i < n; ++i) cin >> a[i];
vector<vector<int> > paired(n);
stack<int> st;
for (int i = 0; i < n; ++i) {
while (sz(st) && a[st.top()] <= a[i]) {
paired[st.top()].emplace_back(i);
st.pop();
}
if (sz(st) ) paired[st.top()].emplace_back(i);
st.emplace(i);
}
int q; cin >> q;
vector<vector<pair<int, int> > > que(n);
for (int i = 0; i < q; ++i) {
int l, r; cin >> l >> r; --l; --r;
que[l].emplace_back(r, i);
}
vector<int> ans(q);
It it( (n + 5) << 2);
it.build();
for (int i = n - 1; i >= 0; --i) {
for (int j : paired[i]) if ( (j << 1) - i <= n - 1) it.upd( (j << 1) - i, n - 1, a[i] + a[j]);
for (auto query : que[i]) ans[query.second] = it.getMx(i, query.first);
}
for (int i = 0; i < q; ++i) cout << ans[i] << '\n';
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
6 ms |
380 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
6 ms |
376 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
6 ms |
380 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
6 ms |
376 KB |
Output is correct |
11 |
Correct |
312 ms |
14328 KB |
Output is correct |
12 |
Correct |
316 ms |
14328 KB |
Output is correct |
13 |
Correct |
321 ms |
14328 KB |
Output is correct |
14 |
Correct |
313 ms |
14396 KB |
Output is correct |
15 |
Correct |
314 ms |
14328 KB |
Output is correct |
16 |
Correct |
316 ms |
13688 KB |
Output is correct |
17 |
Correct |
306 ms |
13816 KB |
Output is correct |
18 |
Correct |
322 ms |
13688 KB |
Output is correct |
19 |
Correct |
320 ms |
14304 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
176 ms |
28064 KB |
Output is correct |
2 |
Correct |
105 ms |
27896 KB |
Output is correct |
3 |
Correct |
103 ms |
28792 KB |
Output is correct |
4 |
Correct |
181 ms |
28180 KB |
Output is correct |
5 |
Correct |
183 ms |
28152 KB |
Output is correct |
6 |
Correct |
175 ms |
27512 KB |
Output is correct |
7 |
Correct |
170 ms |
27384 KB |
Output is correct |
8 |
Correct |
178 ms |
27384 KB |
Output is correct |
9 |
Correct |
196 ms |
28020 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
5 ms |
376 KB |
Output is correct |
2 |
Correct |
6 ms |
380 KB |
Output is correct |
3 |
Correct |
5 ms |
376 KB |
Output is correct |
4 |
Correct |
5 ms |
376 KB |
Output is correct |
5 |
Correct |
5 ms |
376 KB |
Output is correct |
6 |
Correct |
5 ms |
376 KB |
Output is correct |
7 |
Correct |
5 ms |
376 KB |
Output is correct |
8 |
Correct |
5 ms |
376 KB |
Output is correct |
9 |
Correct |
5 ms |
376 KB |
Output is correct |
10 |
Correct |
6 ms |
376 KB |
Output is correct |
11 |
Correct |
312 ms |
14328 KB |
Output is correct |
12 |
Correct |
316 ms |
14328 KB |
Output is correct |
13 |
Correct |
321 ms |
14328 KB |
Output is correct |
14 |
Correct |
313 ms |
14396 KB |
Output is correct |
15 |
Correct |
314 ms |
14328 KB |
Output is correct |
16 |
Correct |
316 ms |
13688 KB |
Output is correct |
17 |
Correct |
306 ms |
13816 KB |
Output is correct |
18 |
Correct |
322 ms |
13688 KB |
Output is correct |
19 |
Correct |
320 ms |
14304 KB |
Output is correct |
20 |
Correct |
176 ms |
28064 KB |
Output is correct |
21 |
Correct |
105 ms |
27896 KB |
Output is correct |
22 |
Correct |
103 ms |
28792 KB |
Output is correct |
23 |
Correct |
181 ms |
28180 KB |
Output is correct |
24 |
Correct |
183 ms |
28152 KB |
Output is correct |
25 |
Correct |
175 ms |
27512 KB |
Output is correct |
26 |
Correct |
170 ms |
27384 KB |
Output is correct |
27 |
Correct |
178 ms |
27384 KB |
Output is correct |
28 |
Correct |
196 ms |
28020 KB |
Output is correct |
29 |
Correct |
1188 ms |
93432 KB |
Output is correct |
30 |
Correct |
977 ms |
92792 KB |
Output is correct |
31 |
Correct |
1056 ms |
94840 KB |
Output is correct |
32 |
Correct |
1196 ms |
93432 KB |
Output is correct |
33 |
Correct |
1221 ms |
93432 KB |
Output is correct |
34 |
Correct |
1149 ms |
91128 KB |
Output is correct |
35 |
Correct |
1180 ms |
90888 KB |
Output is correct |
36 |
Correct |
1094 ms |
90872 KB |
Output is correct |
37 |
Correct |
1148 ms |
92280 KB |
Output is correct |
38 |
Correct |
863 ms |
99196 KB |
Output is correct |
39 |
Correct |
823 ms |
99068 KB |
Output is correct |
40 |
Correct |
813 ms |
95736 KB |
Output is correct |
41 |
Correct |
812 ms |
95352 KB |
Output is correct |
42 |
Correct |
826 ms |
95352 KB |
Output is correct |
43 |
Correct |
824 ms |
97056 KB |
Output is correct |
44 |
Correct |
840 ms |
98552 KB |
Output is correct |
45 |
Correct |
877 ms |
98424 KB |
Output is correct |
46 |
Correct |
843 ms |
95352 KB |
Output is correct |
47 |
Correct |
851 ms |
95124 KB |
Output is correct |
48 |
Correct |
857 ms |
94904 KB |
Output is correct |
49 |
Correct |
821 ms |
97016 KB |
Output is correct |
50 |
Correct |
938 ms |
96700 KB |
Output is correct |
51 |
Correct |
966 ms |
96680 KB |
Output is correct |
52 |
Correct |
983 ms |
94200 KB |
Output is correct |
53 |
Correct |
962 ms |
93692 KB |
Output is correct |
54 |
Correct |
950 ms |
93816 KB |
Output is correct |
55 |
Correct |
1002 ms |
95456 KB |
Output is correct |