#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
#define fi first
#define se second
#define mp make_pair
#define fastIO ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
const int N = (int)5e5 + 9;
const int M = (int)1e6 + 10;
vector<int> X[N];
vector<pii> Q[N];
ll A[N];
ll ans[N];
struct Node{
ll sol;
ll f1;
ll f2;
};
Node T[M * 4 + 512];
Node unite(Node A, Node B){
Node res;
res.sol = max(A.sol, B.sol);
res.sol = max(res.sol, A.f1 + B.f2);
res.f1 = max(A.f1, B.f1);
res.f2 = max(A.f2, B.f2);
return res;
}
void upd(int node, int cl, int cr, int pos, ll i1, ll i2){
if(cl == cr){
T[node].f1=max(T[node].f1, i1);
T[node].f2=max(T[node].f2, i2);
T[node].sol=T[node].f1+T[node].f2;
return ;
}
int mid = (cl + cr) / 2;
if(mid >= pos)
upd(node * 2, cl, mid, pos, i1, i2);
else
upd(node * 2 + 1, mid + 1, cr, pos, i1, i2);
T[node] = unite(T[node * 2], T[node * 2 + 1]);
}
Node qq(int node, int cl, int cr, int tl, int tr){
if(cr < tl)
return {0, 0, 0};
if(cl > tr)
return {0, 0, 0};
if(cl >= tl && cr <= tr){
return T[node];
}
int mid = (cl + cr) / 2;
Node f1 = qq(node * 2, cl, mid, tl, tr);
Node f2 = qq(node * 2 + 1, mid + 1, cr, tl, tr);
return unite(f1, f2);
}
int main(){
fastIO;
int n, q;
cin >> n;
vector<int> ids;
for(int i = 1; i <= n; i ++ ){
cin >> A[i];
while(!ids.empty() && A[i] > A[ids.back()]){
X[ids.back()].push_back(i);
ids.pop_back();
}
if(!ids.empty()){
X[ids.back()].push_back(i);
}
ids.push_back(i);
}
cin >> q;
int l, r;
for(int i = 1; i <= q; i ++ ){
cin >> l >>r;
Q[l].push_back(mp(r, i));
}
int id;
for(int r = n; r >= 1; r -- ){
upd(1, 1, n, r, 0ll, A[r]);
for(auto x : X[r]){
id = (2 * x - r);
if(id <= n)
upd(1, 1, n, id, A[r] + A[x], 0ll);
}
for(auto v : Q[r]){
Node qji = qq(1, 1, n, r, v.fi);
ans[v.se] = qji.sol;
}
}
for(int i = 1 ; i <= q; i ++ ){
cout << ans[i] << "\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
23936 KB |
Output is correct |
2 |
Correct |
17 ms |
23832 KB |
Output is correct |
3 |
Correct |
14 ms |
23808 KB |
Output is correct |
4 |
Correct |
16 ms |
23808 KB |
Output is correct |
5 |
Correct |
18 ms |
23808 KB |
Output is correct |
6 |
Correct |
16 ms |
23808 KB |
Output is correct |
7 |
Correct |
16 ms |
23808 KB |
Output is correct |
8 |
Correct |
16 ms |
23808 KB |
Output is correct |
9 |
Correct |
14 ms |
23808 KB |
Output is correct |
10 |
Correct |
16 ms |
23808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
23936 KB |
Output is correct |
2 |
Correct |
17 ms |
23832 KB |
Output is correct |
3 |
Correct |
14 ms |
23808 KB |
Output is correct |
4 |
Correct |
16 ms |
23808 KB |
Output is correct |
5 |
Correct |
18 ms |
23808 KB |
Output is correct |
6 |
Correct |
16 ms |
23808 KB |
Output is correct |
7 |
Correct |
16 ms |
23808 KB |
Output is correct |
8 |
Correct |
16 ms |
23808 KB |
Output is correct |
9 |
Correct |
14 ms |
23808 KB |
Output is correct |
10 |
Correct |
16 ms |
23808 KB |
Output is correct |
11 |
Correct |
345 ms |
44408 KB |
Output is correct |
12 |
Correct |
312 ms |
44408 KB |
Output is correct |
13 |
Correct |
321 ms |
44484 KB |
Output is correct |
14 |
Correct |
312 ms |
44544 KB |
Output is correct |
15 |
Correct |
322 ms |
44408 KB |
Output is correct |
16 |
Correct |
324 ms |
43896 KB |
Output is correct |
17 |
Correct |
320 ms |
43768 KB |
Output is correct |
18 |
Correct |
320 ms |
43768 KB |
Output is correct |
19 |
Correct |
325 ms |
44280 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
176 ms |
45944 KB |
Output is correct |
2 |
Correct |
127 ms |
45688 KB |
Output is correct |
3 |
Correct |
124 ms |
46572 KB |
Output is correct |
4 |
Correct |
177 ms |
45944 KB |
Output is correct |
5 |
Correct |
172 ms |
45944 KB |
Output is correct |
6 |
Correct |
170 ms |
45304 KB |
Output is correct |
7 |
Correct |
159 ms |
45176 KB |
Output is correct |
8 |
Correct |
159 ms |
45176 KB |
Output is correct |
9 |
Correct |
161 ms |
45504 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
20 ms |
23936 KB |
Output is correct |
2 |
Correct |
17 ms |
23832 KB |
Output is correct |
3 |
Correct |
14 ms |
23808 KB |
Output is correct |
4 |
Correct |
16 ms |
23808 KB |
Output is correct |
5 |
Correct |
18 ms |
23808 KB |
Output is correct |
6 |
Correct |
16 ms |
23808 KB |
Output is correct |
7 |
Correct |
16 ms |
23808 KB |
Output is correct |
8 |
Correct |
16 ms |
23808 KB |
Output is correct |
9 |
Correct |
14 ms |
23808 KB |
Output is correct |
10 |
Correct |
16 ms |
23808 KB |
Output is correct |
11 |
Correct |
345 ms |
44408 KB |
Output is correct |
12 |
Correct |
312 ms |
44408 KB |
Output is correct |
13 |
Correct |
321 ms |
44484 KB |
Output is correct |
14 |
Correct |
312 ms |
44544 KB |
Output is correct |
15 |
Correct |
322 ms |
44408 KB |
Output is correct |
16 |
Correct |
324 ms |
43896 KB |
Output is correct |
17 |
Correct |
320 ms |
43768 KB |
Output is correct |
18 |
Correct |
320 ms |
43768 KB |
Output is correct |
19 |
Correct |
325 ms |
44280 KB |
Output is correct |
20 |
Correct |
176 ms |
45944 KB |
Output is correct |
21 |
Correct |
127 ms |
45688 KB |
Output is correct |
22 |
Correct |
124 ms |
46572 KB |
Output is correct |
23 |
Correct |
177 ms |
45944 KB |
Output is correct |
24 |
Correct |
172 ms |
45944 KB |
Output is correct |
25 |
Correct |
170 ms |
45304 KB |
Output is correct |
26 |
Correct |
159 ms |
45176 KB |
Output is correct |
27 |
Correct |
159 ms |
45176 KB |
Output is correct |
28 |
Correct |
161 ms |
45504 KB |
Output is correct |
29 |
Correct |
1056 ms |
98548 KB |
Output is correct |
30 |
Correct |
1016 ms |
98172 KB |
Output is correct |
31 |
Correct |
940 ms |
99756 KB |
Output is correct |
32 |
Correct |
1061 ms |
98424 KB |
Output is correct |
33 |
Correct |
1135 ms |
98552 KB |
Output is correct |
34 |
Correct |
1113 ms |
96120 KB |
Output is correct |
35 |
Correct |
1113 ms |
95992 KB |
Output is correct |
36 |
Correct |
1086 ms |
95864 KB |
Output is correct |
37 |
Correct |
1063 ms |
97272 KB |
Output is correct |
38 |
Correct |
811 ms |
104184 KB |
Output is correct |
39 |
Correct |
808 ms |
104136 KB |
Output is correct |
40 |
Correct |
810 ms |
100856 KB |
Output is correct |
41 |
Correct |
811 ms |
100344 KB |
Output is correct |
42 |
Correct |
782 ms |
100240 KB |
Output is correct |
43 |
Correct |
788 ms |
102132 KB |
Output is correct |
44 |
Correct |
838 ms |
103544 KB |
Output is correct |
45 |
Correct |
866 ms |
103728 KB |
Output is correct |
46 |
Correct |
810 ms |
100308 KB |
Output is correct |
47 |
Correct |
860 ms |
100000 KB |
Output is correct |
48 |
Correct |
840 ms |
99944 KB |
Output is correct |
49 |
Correct |
886 ms |
102132 KB |
Output is correct |
50 |
Correct |
923 ms |
101624 KB |
Output is correct |
51 |
Correct |
931 ms |
101752 KB |
Output is correct |
52 |
Correct |
906 ms |
99192 KB |
Output is correct |
53 |
Correct |
900 ms |
98808 KB |
Output is correct |
54 |
Correct |
892 ms |
98808 KB |
Output is correct |
55 |
Correct |
944 ms |
100472 KB |
Output is correct |