#include <bits/stdc++.h>
using namespace std;
const int Nmax = 5e5 + 5;
const int inf = 1e9;
vector<int> start[Nmax], query[Nmax];
vector<pair<int,int>> v;
int a[Nmax], st[Nmax];
int n, L[Nmax], R[Nmax];
int ans[Nmax];
#define left_son ((node<<1))
#define right_son ((node<<1)|1)
#define mid ((st+dr)>>1)
class SegTree
{
int best[Nmax<<2];
int mx[Nmax<<2];
int lazy[Nmax<<2];
void propag(int node)
{
if(!lazy[node]) return;
best[left_son] = max(best[left_son], mx[left_son] + lazy[node]);
best[right_son] = max(best[right_son], mx[right_son] + lazy[node]);
lazy[left_son] = max(lazy[left_son], lazy[node]);
lazy[right_son] = max(lazy[right_son], lazy[node]);
lazy[node] = 0;
}
public:
void init(int node, int st, int dr)
{
lazy[node] = 0;
best[node] = -inf;
if(st == dr)
{
mx[node] = a[st];
return;
}
init(left_son, st, mid);
init(right_son, mid+1, dr);
mx[node] = max(mx[left_son], mx[right_son]);
}
void update(int node, int st, int dr, int L, int R, int coef)
{
if(L <= st && dr <= R)
{
lazy[node] = max(lazy[node], coef);
best[node] = max(best[node], mx[node] + coef);
return;
}
propag(node);
if(L <= mid) update(left_son, st, mid, L, R, coef);
if(mid < R) update(right_son, mid+1, dr, L, R, coef);
best[node] = max(best[left_son], best[right_son]);
}
int query(int node, int st, int dr, int L, int R)
{
if(L <= st && dr <= R)
return best[node];
propag(node);
int ans = -inf;
if(L <= mid) ans = max(ans, query(left_son, st, mid, L, R));
if(mid < R) ans = max(ans, query(right_son, mid+1, dr, L, R));
return ans;
}
} aint;
void fnd()
{
int i, nr = 0;
st[0] = 0;
a[0] = inf;
for(i=1; i<=n; ++i)
{
while(a[st[nr]] <= a[i])
{
v.push_back({st[nr], i});
--nr;
}
v.push_back({st[nr], i});
st[++nr] = i;
}
}
int main()
{
// freopen("triple.in", "r", stdin);
cin.tie(0); cin.sync_with_stdio(false);
cin >> n;
int i, q;
for(i=1; i<=n; ++i) cin >> a[i];
fnd();
cin >> q;
for(i=1; i<=q; ++i) cin >> L[i] >> R[i];
for(auto it : v)
start[it.first].push_back(it.second);
for(i=1; i<=q; ++i)
query[L[i]].push_back(i);
aint.init(1, 1, n);
for(i=n; i; --i)
{
for(auto it : start[i])
{
int R = 2 * it - i;
if(R <= n)
aint.update(1, 1, n, R, n, a[i] + a[it]);
}
for(auto it : query[i])
ans[it] = aint.query(1, 1, n, i, R[it]);
}
for(i=1; i<=q; ++i) cout << ans[i] << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
23928 KB |
Output is correct |
2 |
Correct |
24 ms |
23928 KB |
Output is correct |
3 |
Correct |
24 ms |
23900 KB |
Output is correct |
4 |
Correct |
23 ms |
23928 KB |
Output is correct |
5 |
Correct |
28 ms |
23928 KB |
Output is correct |
6 |
Correct |
23 ms |
23816 KB |
Output is correct |
7 |
Correct |
23 ms |
23928 KB |
Output is correct |
8 |
Correct |
23 ms |
23928 KB |
Output is correct |
9 |
Correct |
27 ms |
23928 KB |
Output is correct |
10 |
Correct |
23 ms |
23928 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
23928 KB |
Output is correct |
2 |
Correct |
24 ms |
23928 KB |
Output is correct |
3 |
Correct |
24 ms |
23900 KB |
Output is correct |
4 |
Correct |
23 ms |
23928 KB |
Output is correct |
5 |
Correct |
28 ms |
23928 KB |
Output is correct |
6 |
Correct |
23 ms |
23816 KB |
Output is correct |
7 |
Correct |
23 ms |
23928 KB |
Output is correct |
8 |
Correct |
23 ms |
23928 KB |
Output is correct |
9 |
Correct |
27 ms |
23928 KB |
Output is correct |
10 |
Correct |
23 ms |
23928 KB |
Output is correct |
11 |
Correct |
348 ms |
43216 KB |
Output is correct |
12 |
Correct |
352 ms |
43256 KB |
Output is correct |
13 |
Correct |
355 ms |
43028 KB |
Output is correct |
14 |
Correct |
364 ms |
43128 KB |
Output is correct |
15 |
Correct |
346 ms |
43256 KB |
Output is correct |
16 |
Correct |
381 ms |
42616 KB |
Output is correct |
17 |
Correct |
362 ms |
42588 KB |
Output is correct |
18 |
Correct |
338 ms |
42488 KB |
Output is correct |
19 |
Correct |
347 ms |
43128 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
182 ms |
42308 KB |
Output is correct |
2 |
Correct |
122 ms |
42800 KB |
Output is correct |
3 |
Correct |
115 ms |
41192 KB |
Output is correct |
4 |
Correct |
184 ms |
42308 KB |
Output is correct |
5 |
Correct |
204 ms |
42212 KB |
Output is correct |
6 |
Correct |
189 ms |
41568 KB |
Output is correct |
7 |
Correct |
187 ms |
41480 KB |
Output is correct |
8 |
Correct |
177 ms |
41568 KB |
Output is correct |
9 |
Correct |
179 ms |
41824 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
23928 KB |
Output is correct |
2 |
Correct |
24 ms |
23928 KB |
Output is correct |
3 |
Correct |
24 ms |
23900 KB |
Output is correct |
4 |
Correct |
23 ms |
23928 KB |
Output is correct |
5 |
Correct |
28 ms |
23928 KB |
Output is correct |
6 |
Correct |
23 ms |
23816 KB |
Output is correct |
7 |
Correct |
23 ms |
23928 KB |
Output is correct |
8 |
Correct |
23 ms |
23928 KB |
Output is correct |
9 |
Correct |
27 ms |
23928 KB |
Output is correct |
10 |
Correct |
23 ms |
23928 KB |
Output is correct |
11 |
Correct |
348 ms |
43216 KB |
Output is correct |
12 |
Correct |
352 ms |
43256 KB |
Output is correct |
13 |
Correct |
355 ms |
43028 KB |
Output is correct |
14 |
Correct |
364 ms |
43128 KB |
Output is correct |
15 |
Correct |
346 ms |
43256 KB |
Output is correct |
16 |
Correct |
381 ms |
42616 KB |
Output is correct |
17 |
Correct |
362 ms |
42588 KB |
Output is correct |
18 |
Correct |
338 ms |
42488 KB |
Output is correct |
19 |
Correct |
347 ms |
43128 KB |
Output is correct |
20 |
Correct |
182 ms |
42308 KB |
Output is correct |
21 |
Correct |
122 ms |
42800 KB |
Output is correct |
22 |
Correct |
115 ms |
41192 KB |
Output is correct |
23 |
Correct |
184 ms |
42308 KB |
Output is correct |
24 |
Correct |
204 ms |
42212 KB |
Output is correct |
25 |
Correct |
189 ms |
41568 KB |
Output is correct |
26 |
Correct |
187 ms |
41480 KB |
Output is correct |
27 |
Correct |
177 ms |
41568 KB |
Output is correct |
28 |
Correct |
179 ms |
41824 KB |
Output is correct |
29 |
Correct |
1219 ms |
87084 KB |
Output is correct |
30 |
Correct |
1060 ms |
88404 KB |
Output is correct |
31 |
Correct |
1088 ms |
84680 KB |
Output is correct |
32 |
Correct |
1165 ms |
87124 KB |
Output is correct |
33 |
Correct |
1172 ms |
86764 KB |
Output is correct |
34 |
Correct |
1172 ms |
86636 KB |
Output is correct |
35 |
Correct |
1138 ms |
86872 KB |
Output is correct |
36 |
Correct |
1138 ms |
86868 KB |
Output is correct |
37 |
Correct |
1165 ms |
87096 KB |
Output is correct |
38 |
Correct |
826 ms |
93268 KB |
Output is correct |
39 |
Correct |
804 ms |
93396 KB |
Output is correct |
40 |
Correct |
790 ms |
92000 KB |
Output is correct |
41 |
Correct |
783 ms |
91848 KB |
Output is correct |
42 |
Correct |
776 ms |
91612 KB |
Output is correct |
43 |
Correct |
824 ms |
92156 KB |
Output is correct |
44 |
Correct |
890 ms |
92516 KB |
Output is correct |
45 |
Correct |
884 ms |
92500 KB |
Output is correct |
46 |
Correct |
874 ms |
91092 KB |
Output is correct |
47 |
Correct |
852 ms |
90976 KB |
Output is correct |
48 |
Correct |
850 ms |
90876 KB |
Output is correct |
49 |
Correct |
851 ms |
91672 KB |
Output is correct |
50 |
Correct |
1010 ms |
89772 KB |
Output is correct |
51 |
Correct |
1065 ms |
89596 KB |
Output is correct |
52 |
Correct |
1034 ms |
88628 KB |
Output is correct |
53 |
Correct |
966 ms |
88332 KB |
Output is correct |
54 |
Correct |
1012 ms |
88532 KB |
Output is correct |
55 |
Correct |
990 ms |
89008 KB |
Output is correct |