#include "bits/stdc++.h"
using namespace std;
const int maxn = 5e5 + 5;
const int inf = 1e9;
int n, q, a[maxn];
vector<pair<int, int>> cand;
int ans[maxn];
struct node {
int res, ft, se;
node() {
res = ft = se = 0;
}
node(int x, int y, int z) : res(x), ft(y), se(z) {}
node operator+(const node &o) const {
node tmp = *this;
tmp.res = max({tmp.res, o.res, this->ft + o.se});
tmp.se = max(tmp.se, o.se);
tmp.ft = max(tmp.ft, o.ft);
return tmp;
}
} tree[maxn * 4];
struct segment_tree {
int n;
segment_tree() {}
segment_tree(int n) : n(n) {}
void build(int ind, int l, int r) {
if(l == r) {
tree[ind].res = tree[ind].se = a[l];
return;
}
int mid = (l + r) / 2;
build(ind * 2, l, mid);
build(ind * 2 + 1, mid + 1, r);
tree[ind] = tree[ind * 2] + tree[ind * 2 + 1];
}
void update(int ind, int l, int r, int pos, int val) {
if(l == r) {
tree[ind].ft = max(tree[ind].ft, val);
tree[ind].res = tree[ind].ft + tree[ind].se;
return;
}
int mid = (l + r) / 2;
if(pos <= mid) update(ind * 2, l, mid, pos, val);
else update(ind * 2 + 1, mid + 1, r, pos, val);
tree[ind] = tree[ind * 2] + tree[ind * 2 + 1];
}
node get(int ind, int l, int r, int x, int y) {
if(l > y || r < x) return node();
if(x <= l and r <= y) return tree[ind];
int mid = (l + r) / 2;
return get(ind * 2, l, mid, x, y) + get(ind * 2 + 1, mid + 1, r, x, y);
}
void update(int pos, int val) {
update(1, 1, n, pos, val);
}
node get(int x, int y) {
return get(1, 1, n, x, y);
}
} st;
struct query {
int l, r, id;
bool operator<(const query &o) {
return l > o.l;
}
} que[maxn];
void solve() {
cin >> n;
for(int i = 1; i <= n; ++i) {
cin >> a[i];
}
stack<int> s;
for(int i = 1; i <= n; ++i) {
while(!s.empty() and a[i] >= a[s.top()]) {
cand.push_back({s.top(), i * 2 - s.top()});
s.pop();
}
if(!s.empty()) {
cand.push_back({s.top(), i * 2 - s.top()});
}
s.push(i);
}
st = segment_tree(n);
st.build(1, 1, n);
cin >> q;
for(int i = 1; i <= q; ++i) {
cin >> que[i].l >> que[i].r;
que[i].id = i;
}
sort(que + 1, que + q + 1);
sort(cand.begin(), cand.end(), [](const pair<int, int> &x, const pair<int, int> &y) -> bool {
return x.first > y.first;
});
int j = 0;
for(int i = 1; i <= q; ++i) {
while(j < (int)cand.size() and cand[j].first >= que[i].l) {
int x = cand[j].first, y = cand[j].second;
int z = (y + x) / 2;
if(y <= n) st.update(y, a[x] + a[z]);
++j;
// cerr << x << " " << z << " " << y << '\n';
}
ans[que[i].id] = st.get(que[i].l, que[i].r).res;
}
for(int i = 1; i <= q; ++i) {
cout << ans[i] << '\n';
}
}
signed main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
solve();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
27480 KB |
Output is correct |
2 |
Correct |
5 ms |
27604 KB |
Output is correct |
3 |
Correct |
5 ms |
27484 KB |
Output is correct |
4 |
Correct |
5 ms |
27484 KB |
Output is correct |
5 |
Correct |
4 ms |
27484 KB |
Output is correct |
6 |
Correct |
5 ms |
27736 KB |
Output is correct |
7 |
Correct |
5 ms |
27480 KB |
Output is correct |
8 |
Correct |
5 ms |
27480 KB |
Output is correct |
9 |
Correct |
4 ms |
27612 KB |
Output is correct |
10 |
Correct |
4 ms |
27484 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
27480 KB |
Output is correct |
2 |
Correct |
5 ms |
27604 KB |
Output is correct |
3 |
Correct |
5 ms |
27484 KB |
Output is correct |
4 |
Correct |
5 ms |
27484 KB |
Output is correct |
5 |
Correct |
4 ms |
27484 KB |
Output is correct |
6 |
Correct |
5 ms |
27736 KB |
Output is correct |
7 |
Correct |
5 ms |
27480 KB |
Output is correct |
8 |
Correct |
5 ms |
27480 KB |
Output is correct |
9 |
Correct |
4 ms |
27612 KB |
Output is correct |
10 |
Correct |
4 ms |
27484 KB |
Output is correct |
11 |
Correct |
187 ms |
43280 KB |
Output is correct |
12 |
Correct |
173 ms |
43092 KB |
Output is correct |
13 |
Correct |
178 ms |
43092 KB |
Output is correct |
14 |
Correct |
174 ms |
43316 KB |
Output is correct |
15 |
Correct |
177 ms |
43348 KB |
Output is correct |
16 |
Correct |
179 ms |
42580 KB |
Output is correct |
17 |
Correct |
193 ms |
42660 KB |
Output is correct |
18 |
Correct |
208 ms |
42576 KB |
Output is correct |
19 |
Correct |
211 ms |
43088 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
82 ms |
33488 KB |
Output is correct |
2 |
Correct |
44 ms |
31440 KB |
Output is correct |
3 |
Correct |
52 ms |
31932 KB |
Output is correct |
4 |
Correct |
83 ms |
33488 KB |
Output is correct |
5 |
Correct |
77 ms |
33488 KB |
Output is correct |
6 |
Correct |
78 ms |
32824 KB |
Output is correct |
7 |
Correct |
86 ms |
32720 KB |
Output is correct |
8 |
Correct |
90 ms |
32720 KB |
Output is correct |
9 |
Correct |
83 ms |
32976 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
27480 KB |
Output is correct |
2 |
Correct |
5 ms |
27604 KB |
Output is correct |
3 |
Correct |
5 ms |
27484 KB |
Output is correct |
4 |
Correct |
5 ms |
27484 KB |
Output is correct |
5 |
Correct |
4 ms |
27484 KB |
Output is correct |
6 |
Correct |
5 ms |
27736 KB |
Output is correct |
7 |
Correct |
5 ms |
27480 KB |
Output is correct |
8 |
Correct |
5 ms |
27480 KB |
Output is correct |
9 |
Correct |
4 ms |
27612 KB |
Output is correct |
10 |
Correct |
4 ms |
27484 KB |
Output is correct |
11 |
Correct |
187 ms |
43280 KB |
Output is correct |
12 |
Correct |
173 ms |
43092 KB |
Output is correct |
13 |
Correct |
178 ms |
43092 KB |
Output is correct |
14 |
Correct |
174 ms |
43316 KB |
Output is correct |
15 |
Correct |
177 ms |
43348 KB |
Output is correct |
16 |
Correct |
179 ms |
42580 KB |
Output is correct |
17 |
Correct |
193 ms |
42660 KB |
Output is correct |
18 |
Correct |
208 ms |
42576 KB |
Output is correct |
19 |
Correct |
211 ms |
43088 KB |
Output is correct |
20 |
Correct |
82 ms |
33488 KB |
Output is correct |
21 |
Correct |
44 ms |
31440 KB |
Output is correct |
22 |
Correct |
52 ms |
31932 KB |
Output is correct |
23 |
Correct |
83 ms |
33488 KB |
Output is correct |
24 |
Correct |
77 ms |
33488 KB |
Output is correct |
25 |
Correct |
78 ms |
32824 KB |
Output is correct |
26 |
Correct |
86 ms |
32720 KB |
Output is correct |
27 |
Correct |
90 ms |
32720 KB |
Output is correct |
28 |
Correct |
83 ms |
32976 KB |
Output is correct |
29 |
Correct |
509 ms |
57528 KB |
Output is correct |
30 |
Correct |
408 ms |
53668 KB |
Output is correct |
31 |
Correct |
360 ms |
55552 KB |
Output is correct |
32 |
Correct |
490 ms |
57532 KB |
Output is correct |
33 |
Correct |
486 ms |
57760 KB |
Output is correct |
34 |
Correct |
487 ms |
55204 KB |
Output is correct |
35 |
Correct |
465 ms |
54960 KB |
Output is correct |
36 |
Correct |
468 ms |
55076 KB |
Output is correct |
37 |
Correct |
485 ms |
56496 KB |
Output is correct |
38 |
Correct |
416 ms |
57512 KB |
Output is correct |
39 |
Correct |
417 ms |
57616 KB |
Output is correct |
40 |
Correct |
371 ms |
54196 KB |
Output is correct |
41 |
Correct |
388 ms |
53680 KB |
Output is correct |
42 |
Correct |
384 ms |
53656 KB |
Output is correct |
43 |
Correct |
375 ms |
55400 KB |
Output is correct |
44 |
Correct |
427 ms |
57520 KB |
Output is correct |
45 |
Correct |
402 ms |
57480 KB |
Output is correct |
46 |
Correct |
393 ms |
54452 KB |
Output is correct |
47 |
Correct |
385 ms |
53936 KB |
Output is correct |
48 |
Correct |
392 ms |
53992 KB |
Output is correct |
49 |
Correct |
407 ms |
56092 KB |
Output is correct |
50 |
Correct |
427 ms |
57520 KB |
Output is correct |
51 |
Correct |
424 ms |
57772 KB |
Output is correct |
52 |
Correct |
451 ms |
55220 KB |
Output is correct |
53 |
Correct |
413 ms |
54936 KB |
Output is correct |
54 |
Correct |
445 ms |
54944 KB |
Output is correct |
55 |
Correct |
444 ms |
56500 KB |
Output is correct |