#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define ff first
#define ss second
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int N = 2000005;
const ll MOD = 1000000007;
const ll INF = 0x3f3f3f3f3f3f3f3f;
const int SQ = 300;
int a[N];
int lz[N], con[N], ans[N];
void build(int id, int l, int r){
if(l == r){
con[id] = ans[id] = a[l];
return;
}
int m = (l + r) >> 1;
build(id << 1, l, m);
build(id << 1 | 1, m + 1, r);
con[id] = ans[id] = max(con[id << 1], con[id << 1 | 1]);
}
void reset(int id, int l, int r){
ans[id] = max(ans[id], con[id] + lz[id]);
}
void update(int id, int l, int r, int L, int R, int x){
lz[id] = max(lz[id], lz[id >> 1]);
ans[id] = max(ans[id], con[id] + lz[id]);
if(r < L || R < l) return;
if(L <= l && r <= R){
lz[id] = max(lz[id], x);
ans[id] = max(ans[id], con[id] + lz[id]);
return;
}
int m = (l + r) >> 1;
update(id << 1, l, m, L, R, x);
update(id << 1 | 1, m + 1, r, L, R, x);
ans[id] = max({ans[id], ans[id << 1], ans[id << 1 | 1]});
}
int query(int id, int l, int r, int L, int R){
lz[id] = max(lz[id], lz[id >> 1]);
ans[id] = max(ans[id], con[id] + lz[id]);
if(r < L || R < l)
return 0;
if(L <= l && r <= R)
return ans[id];
int m = (l + r) >> 1;
return max(query(id << 1, l, m, L, R), query(id << 1 | 1, m + 1, r, L, R));
}
vector<pair<int, int> > upd[N], que[N];
int res[N];
vector<int> tmp;
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n;
cin>>n;
for(int i = 1; i <= n; ++i)
cin>>a[i];
build(1, 1, n);
for(int i = 1; i <= n; ++i){
while(!tmp.empty() && a[tmp.back()] < a[i]){
upd[tmp.back()].push_back({i * 2 - tmp.back(), a[i] + a[tmp.back()]});
tmp.pop_back();
}
if(!tmp.empty())
upd[tmp.back()].push_back({i * 2 - tmp.back(), a[i] + a[tmp.back()]});
tmp.push_back(i);
}
int q;
cin>>q;
for(int l, r, i = 1; i <= q; ++i){
cin>>l>>r;
que[l].push_back({r, i});
}
for(int i = n; i >= 1; --i){
for(auto& [pos, val] : upd[i])
update(1, 1, n, pos, n, val);
for(auto& [r, v] : que[i])
res[v] = query(1, 1, n, i + 2, r);
}
for(int i = 1; i <= q; ++i)
cout<<res[i]<<'\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
53 ms |
94276 KB |
Output is correct |
2 |
Correct |
54 ms |
94164 KB |
Output is correct |
3 |
Correct |
54 ms |
94284 KB |
Output is correct |
4 |
Correct |
55 ms |
94248 KB |
Output is correct |
5 |
Correct |
56 ms |
94208 KB |
Output is correct |
6 |
Correct |
57 ms |
94252 KB |
Output is correct |
7 |
Correct |
54 ms |
94208 KB |
Output is correct |
8 |
Correct |
54 ms |
94244 KB |
Output is correct |
9 |
Correct |
55 ms |
94276 KB |
Output is correct |
10 |
Correct |
55 ms |
94240 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
53 ms |
94276 KB |
Output is correct |
2 |
Correct |
54 ms |
94164 KB |
Output is correct |
3 |
Correct |
54 ms |
94284 KB |
Output is correct |
4 |
Correct |
55 ms |
94248 KB |
Output is correct |
5 |
Correct |
56 ms |
94208 KB |
Output is correct |
6 |
Correct |
57 ms |
94252 KB |
Output is correct |
7 |
Correct |
54 ms |
94208 KB |
Output is correct |
8 |
Correct |
54 ms |
94244 KB |
Output is correct |
9 |
Correct |
55 ms |
94276 KB |
Output is correct |
10 |
Correct |
55 ms |
94240 KB |
Output is correct |
11 |
Correct |
402 ms |
113308 KB |
Output is correct |
12 |
Correct |
393 ms |
113212 KB |
Output is correct |
13 |
Correct |
391 ms |
113192 KB |
Output is correct |
14 |
Correct |
385 ms |
113256 KB |
Output is correct |
15 |
Correct |
390 ms |
113252 KB |
Output is correct |
16 |
Correct |
384 ms |
112660 KB |
Output is correct |
17 |
Correct |
385 ms |
112528 KB |
Output is correct |
18 |
Correct |
387 ms |
112568 KB |
Output is correct |
19 |
Correct |
383 ms |
113148 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
250 ms |
110432 KB |
Output is correct |
2 |
Correct |
159 ms |
109124 KB |
Output is correct |
3 |
Correct |
160 ms |
109900 KB |
Output is correct |
4 |
Correct |
242 ms |
110392 KB |
Output is correct |
5 |
Correct |
243 ms |
110408 KB |
Output is correct |
6 |
Correct |
239 ms |
109784 KB |
Output is correct |
7 |
Correct |
239 ms |
109636 KB |
Output is correct |
8 |
Correct |
241 ms |
109700 KB |
Output is correct |
9 |
Correct |
247 ms |
109892 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
53 ms |
94276 KB |
Output is correct |
2 |
Correct |
54 ms |
94164 KB |
Output is correct |
3 |
Correct |
54 ms |
94284 KB |
Output is correct |
4 |
Correct |
55 ms |
94248 KB |
Output is correct |
5 |
Correct |
56 ms |
94208 KB |
Output is correct |
6 |
Correct |
57 ms |
94252 KB |
Output is correct |
7 |
Correct |
54 ms |
94208 KB |
Output is correct |
8 |
Correct |
54 ms |
94244 KB |
Output is correct |
9 |
Correct |
55 ms |
94276 KB |
Output is correct |
10 |
Correct |
55 ms |
94240 KB |
Output is correct |
11 |
Correct |
402 ms |
113308 KB |
Output is correct |
12 |
Correct |
393 ms |
113212 KB |
Output is correct |
13 |
Correct |
391 ms |
113192 KB |
Output is correct |
14 |
Correct |
385 ms |
113256 KB |
Output is correct |
15 |
Correct |
390 ms |
113252 KB |
Output is correct |
16 |
Correct |
384 ms |
112660 KB |
Output is correct |
17 |
Correct |
385 ms |
112528 KB |
Output is correct |
18 |
Correct |
387 ms |
112568 KB |
Output is correct |
19 |
Correct |
383 ms |
113148 KB |
Output is correct |
20 |
Correct |
250 ms |
110432 KB |
Output is correct |
21 |
Correct |
159 ms |
109124 KB |
Output is correct |
22 |
Correct |
160 ms |
109900 KB |
Output is correct |
23 |
Correct |
242 ms |
110392 KB |
Output is correct |
24 |
Correct |
243 ms |
110408 KB |
Output is correct |
25 |
Correct |
239 ms |
109784 KB |
Output is correct |
26 |
Correct |
239 ms |
109636 KB |
Output is correct |
27 |
Correct |
241 ms |
109700 KB |
Output is correct |
28 |
Correct |
247 ms |
109892 KB |
Output is correct |
29 |
Correct |
1351 ms |
155100 KB |
Output is correct |
30 |
Correct |
1120 ms |
151944 KB |
Output is correct |
31 |
Correct |
1100 ms |
153984 KB |
Output is correct |
32 |
Correct |
1336 ms |
155104 KB |
Output is correct |
33 |
Correct |
1345 ms |
155064 KB |
Output is correct |
34 |
Correct |
1303 ms |
152900 KB |
Output is correct |
35 |
Correct |
1358 ms |
152584 KB |
Output is correct |
36 |
Correct |
1299 ms |
152688 KB |
Output is correct |
37 |
Correct |
1342 ms |
154068 KB |
Output is correct |
38 |
Correct |
981 ms |
160800 KB |
Output is correct |
39 |
Correct |
978 ms |
160748 KB |
Output is correct |
40 |
Correct |
964 ms |
157548 KB |
Output is correct |
41 |
Correct |
953 ms |
156996 KB |
Output is correct |
42 |
Correct |
965 ms |
156980 KB |
Output is correct |
43 |
Correct |
955 ms |
158836 KB |
Output is correct |
44 |
Correct |
1028 ms |
160196 KB |
Output is correct |
45 |
Correct |
1044 ms |
160196 KB |
Output is correct |
46 |
Correct |
1010 ms |
157104 KB |
Output is correct |
47 |
Correct |
1010 ms |
156668 KB |
Output is correct |
48 |
Correct |
1014 ms |
156664 KB |
Output is correct |
49 |
Correct |
1024 ms |
158728 KB |
Output is correct |
50 |
Correct |
1140 ms |
158336 KB |
Output is correct |
51 |
Correct |
1126 ms |
158320 KB |
Output is correct |
52 |
Correct |
1121 ms |
155912 KB |
Output is correct |
53 |
Correct |
1127 ms |
155532 KB |
Output is correct |
54 |
Correct |
1107 ms |
155564 KB |
Output is correct |
55 |
Correct |
1115 ms |
157380 KB |
Output is correct |