#include <bits/stdc++.h>
#define rep(i, a, b) for(int i=(a) ; i<=(b) ; ++i)
#define mp make_pair
#define pii pair<int, int>
#define PB push_back
#define F first
#define S second
#define Task ""
using namespace std;
template <typename T > inline void MIN(T &a, T b) { if(a>b) a=b; }
template <typename T > inline void MAX(T &a, T b) { if(a<b) a=b; }
const int N=500005;
int n, a[N], Q, ans[N];
vector <pii > query[N];
vector <int > v[N];
stack <int > s;
struct node {
int ab, c, sum;
node(int AB=0, int C=0, int SUM=0) {
ab=AB, c=C, sum=SUM;
}
};
node Merge(node L, node R) {
return node(max(L.ab, R.ab), max(L.c, R.c), max(max(L.sum, R.sum), L.ab+R.c));
}
node t[N*4];
void updateC(int l, int r, int id, int pos) {
if(l==r) {
t[id].c=a[pos];
t[id].sum=a[pos];
return;
}
int mid=(r+l)>>1;
if(pos<=mid) updateC(l, mid, id*2, pos);
else updateC(mid+1, r, id*2+1, pos);
t[id]=Merge(t[id*2], t[id*2+1]);
}
void updateAB(int l, int r, int id, int pos, int val) {
if(l==r) {
t[id].ab=max(t[id].ab, val);
t[id].sum=t[id].ab+t[id].c;
return;
}
int mid=(r+l)>>1;
if(pos<=mid) updateAB(l, mid, id*2, pos, val);
else updateAB(mid+1, r, id*2+1, pos, val);
t[id]=Merge(t[id*2], t[id*2+1]);
}
node get(int l, int r, int id, int u, int v) {
if(r<u||v<l) return node();
if(u<=l&&r<=v) return t[id];
int mid=(r+l)>>1;
node L=get(l, mid, id*2, u, v);
node R=get(mid+1, r, id*2+1, u, v);
return Merge(L, R);
}
void setup() {
cin >> n;
rep(i, 1, n) cin >> a[i];
cin >> Q;
rep(i, 1, Q) {
int l, r; cin >> l >> r;
query[l].PB(mp(r, i));
}
}
void pre() {
rep(i, 1, n) {
while(!s.empty()&&a[i]>a[s.top()]) {
v[s.top()].PB(i);
s.pop();
}
if(s.size()) v[s.top()].PB(i);
s.push(i);
}
}
void calc() {
for(int i=n ; i ; --i) {
updateC(1, n, 1, i);
for(auto val:v[i]) if(2*val-i<=n) updateAB(1, n, 1, 2*val-i, a[i]+a[val]);
for(auto P:query[i]) {
ans[P.S]=get(1, n, 1, i, P.F).sum;
}
}
rep(i, 1, Q) cout << ans[i] << '\n';
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
//freopen(Task".inp", "r", stdin);
//freopen(Task".out", "w", stdout);
setup();
pre();
calc();
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
44 ms |
47352 KB |
Output is correct |
2 |
Correct |
44 ms |
47352 KB |
Output is correct |
3 |
Correct |
43 ms |
47352 KB |
Output is correct |
4 |
Correct |
43 ms |
47356 KB |
Output is correct |
5 |
Correct |
43 ms |
47352 KB |
Output is correct |
6 |
Correct |
43 ms |
47352 KB |
Output is correct |
7 |
Correct |
43 ms |
47352 KB |
Output is correct |
8 |
Correct |
43 ms |
47352 KB |
Output is correct |
9 |
Correct |
43 ms |
47352 KB |
Output is correct |
10 |
Correct |
44 ms |
47352 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
44 ms |
47352 KB |
Output is correct |
2 |
Correct |
44 ms |
47352 KB |
Output is correct |
3 |
Correct |
43 ms |
47352 KB |
Output is correct |
4 |
Correct |
43 ms |
47356 KB |
Output is correct |
5 |
Correct |
43 ms |
47352 KB |
Output is correct |
6 |
Correct |
43 ms |
47352 KB |
Output is correct |
7 |
Correct |
43 ms |
47352 KB |
Output is correct |
8 |
Correct |
43 ms |
47352 KB |
Output is correct |
9 |
Correct |
43 ms |
47352 KB |
Output is correct |
10 |
Correct |
44 ms |
47352 KB |
Output is correct |
11 |
Correct |
458 ms |
65420 KB |
Output is correct |
12 |
Correct |
447 ms |
65400 KB |
Output is correct |
13 |
Correct |
447 ms |
65400 KB |
Output is correct |
14 |
Correct |
459 ms |
65332 KB |
Output is correct |
15 |
Correct |
450 ms |
65528 KB |
Output is correct |
16 |
Correct |
454 ms |
64760 KB |
Output is correct |
17 |
Correct |
450 ms |
64760 KB |
Output is correct |
18 |
Correct |
451 ms |
64760 KB |
Output is correct |
19 |
Correct |
444 ms |
65500 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
207 ms |
56312 KB |
Output is correct |
2 |
Correct |
155 ms |
56056 KB |
Output is correct |
3 |
Correct |
154 ms |
56952 KB |
Output is correct |
4 |
Correct |
208 ms |
56396 KB |
Output is correct |
5 |
Correct |
207 ms |
56312 KB |
Output is correct |
6 |
Correct |
201 ms |
55672 KB |
Output is correct |
7 |
Correct |
201 ms |
55672 KB |
Output is correct |
8 |
Correct |
199 ms |
55544 KB |
Output is correct |
9 |
Correct |
203 ms |
55872 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
44 ms |
47352 KB |
Output is correct |
2 |
Correct |
44 ms |
47352 KB |
Output is correct |
3 |
Correct |
43 ms |
47352 KB |
Output is correct |
4 |
Correct |
43 ms |
47356 KB |
Output is correct |
5 |
Correct |
43 ms |
47352 KB |
Output is correct |
6 |
Correct |
43 ms |
47352 KB |
Output is correct |
7 |
Correct |
43 ms |
47352 KB |
Output is correct |
8 |
Correct |
43 ms |
47352 KB |
Output is correct |
9 |
Correct |
43 ms |
47352 KB |
Output is correct |
10 |
Correct |
44 ms |
47352 KB |
Output is correct |
11 |
Correct |
458 ms |
65420 KB |
Output is correct |
12 |
Correct |
447 ms |
65400 KB |
Output is correct |
13 |
Correct |
447 ms |
65400 KB |
Output is correct |
14 |
Correct |
459 ms |
65332 KB |
Output is correct |
15 |
Correct |
450 ms |
65528 KB |
Output is correct |
16 |
Correct |
454 ms |
64760 KB |
Output is correct |
17 |
Correct |
450 ms |
64760 KB |
Output is correct |
18 |
Correct |
451 ms |
64760 KB |
Output is correct |
19 |
Correct |
444 ms |
65500 KB |
Output is correct |
20 |
Correct |
207 ms |
56312 KB |
Output is correct |
21 |
Correct |
155 ms |
56056 KB |
Output is correct |
22 |
Correct |
154 ms |
56952 KB |
Output is correct |
23 |
Correct |
208 ms |
56396 KB |
Output is correct |
24 |
Correct |
207 ms |
56312 KB |
Output is correct |
25 |
Correct |
201 ms |
55672 KB |
Output is correct |
26 |
Correct |
201 ms |
55672 KB |
Output is correct |
27 |
Correct |
199 ms |
55544 KB |
Output is correct |
28 |
Correct |
203 ms |
55872 KB |
Output is correct |
29 |
Correct |
1294 ms |
93468 KB |
Output is correct |
30 |
Correct |
1122 ms |
92792 KB |
Output is correct |
31 |
Correct |
1138 ms |
94828 KB |
Output is correct |
32 |
Correct |
1307 ms |
93304 KB |
Output is correct |
33 |
Correct |
1295 ms |
93360 KB |
Output is correct |
34 |
Correct |
1285 ms |
91068 KB |
Output is correct |
35 |
Correct |
1280 ms |
90796 KB |
Output is correct |
36 |
Correct |
1275 ms |
90700 KB |
Output is correct |
37 |
Correct |
1292 ms |
92156 KB |
Output is correct |
38 |
Correct |
966 ms |
99236 KB |
Output is correct |
39 |
Correct |
935 ms |
99104 KB |
Output is correct |
40 |
Correct |
911 ms |
95848 KB |
Output is correct |
41 |
Correct |
908 ms |
95244 KB |
Output is correct |
42 |
Correct |
924 ms |
95232 KB |
Output is correct |
43 |
Correct |
933 ms |
97028 KB |
Output is correct |
44 |
Correct |
1014 ms |
98424 KB |
Output is correct |
45 |
Correct |
1006 ms |
98524 KB |
Output is correct |
46 |
Correct |
1029 ms |
95240 KB |
Output is correct |
47 |
Correct |
990 ms |
94784 KB |
Output is correct |
48 |
Correct |
1008 ms |
94832 KB |
Output is correct |
49 |
Correct |
999 ms |
97096 KB |
Output is correct |
50 |
Correct |
1108 ms |
96420 KB |
Output is correct |
51 |
Correct |
1097 ms |
96564 KB |
Output is correct |
52 |
Correct |
1113 ms |
94056 KB |
Output is correct |
53 |
Correct |
1071 ms |
93712 KB |
Output is correct |
54 |
Correct |
1077 ms |
93692 KB |
Output is correct |
55 |
Correct |
1102 ms |
95272 KB |
Output is correct |