#include <bits/stdc++.h>
using namespace std;
const int nu = 5e5+5;
int n, query, a[nu], x[nu], y[nu];
int lon[nu], ans[nu];
vector<int> q[nu], be[nu];
stack<int> st1, st2;
int lazy[nu*4], it[nu*4], ma[nu*4];
void build(int id, int l, int r)
{
if(l == r) ma[id] = a[l];
else
{
int mid = (l+r)/2;
build(id*2, l, mid);
build(id*2+1, mid+1, r);
ma[id] = max(ma[id*2], ma[id*2+1]);
}
}
void update(int id, int l, int r, int u, int v, int val)
{
if(v < l || u > r) return ;
else if(u <= l && r <= v)
{
it[id] = max(it[id], val+ma[id]);
lazy[id] = max(lazy[id], val);
}
else
{
int mid = (l+r)/2;
it[id*2] = max(it[id*2], lazy[id]+ma[id*2]); lazy[id*2] = max(lazy[id*2], lazy[id]);
it[id*2+1] = max(it[id*2+1], lazy[id]+ma[id*2+1]); lazy[id*2+1] = max(lazy[id*2+1], lazy[id]);
lazy[id] = -1e9;
update(id*2, l, mid, u, v, val);
update(id*2+1, mid+1, r, u, v, val);
it[id] = max(it[id*2], it[id*2+1]);
}
}
int getmax(int id, int l, int r, int u, int v)
{
if(v < l || u > r) return -1e9;
else if(u <= l && r <= v) return it[id];
else
{
int mid = (l+r)/2;
it[id*2] = max(it[id*2], lazy[id]+ma[id*2]); lazy[id*2] = max(lazy[id*2], lazy[id]);
it[id*2+1] = max(it[id*2+1], lazy[id]+ma[id*2+1]); lazy[id*2+1] = max(lazy[id*2+1], lazy[id]);
lazy[id] = -1e9;
return max(getmax(id*2, l, mid, u, v), getmax(id*2+1, mid+1, r, u, v));
}
}
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
cin >> n;
for(int i = 1; i <= n; ++i) cin >> a[i];
cin >> query;
for(int i = 1; i <= query; ++i)
{
cin >> x[i] >> y[i];
q[x[i]].push_back(i);
}
st1.push(n+1); st2.push(0);
for(int i = n; i > 0; --i)
{
while(int(st1.size()) > 1 && a[st1.top()] < a[i]) st1.pop();
//while(int(st2.size()) > 1 && a[st2.top()] > a[i]) st2.pop();
//be[i] = st2.top();
lon[i] = st1.top();
//st2.push(i);
st1.push(i);
}
for(int i = 1; i <= n; ++i)
{
while(int(st2.size()) > 1 && a[st2.top()] < a[i]) st2.pop();
be[st2.top()].push_back(i);
st2.push(i);
}
build(1, 1, n);
for(int i = 1; i <= 4*n; ++i) it[i] = lazy[i] = -1e9;
for(int i = n; i > 0; --i)
{
int aa = i; int b;
int c = 2*b-aa;
for(int j = 0; j < int(be[i].size()); ++j)
{
int qq = be[i][j];
if(qq <= n && 2*qq-i <= n) update(1, 1, n, 2*qq-i, n, a[i]+a[qq]);
}
// if(be[i] <= n && c <= n) update(1, 1, n, c, n, a[i]+a[be[i]]), cout << c << ' ' << n << ' ' << a[i]+a[be[i]] << ' ' << i << '\n';
b = lon[i]; c = 2*b-aa;
if(lon[i] <= n && c <= n) update(1, 1, n, c, n, a[i]+a[lon[i]]);
for(int j = 0; j < int(q[i].size()); ++j)
{
int p = q[i][j];
ans[p] = getmax(1, 1, n, x[p], y[p]);
}
}
for(int i = 1; i <= query; ++i) cout << ans[i] << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23828 KB |
Output is correct |
2 |
Correct |
13 ms |
23832 KB |
Output is correct |
3 |
Correct |
12 ms |
23800 KB |
Output is correct |
4 |
Correct |
16 ms |
23892 KB |
Output is correct |
5 |
Correct |
12 ms |
23780 KB |
Output is correct |
6 |
Correct |
12 ms |
23836 KB |
Output is correct |
7 |
Correct |
12 ms |
23764 KB |
Output is correct |
8 |
Correct |
17 ms |
23764 KB |
Output is correct |
9 |
Correct |
15 ms |
23892 KB |
Output is correct |
10 |
Correct |
14 ms |
23828 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23828 KB |
Output is correct |
2 |
Correct |
13 ms |
23832 KB |
Output is correct |
3 |
Correct |
12 ms |
23800 KB |
Output is correct |
4 |
Correct |
16 ms |
23892 KB |
Output is correct |
5 |
Correct |
12 ms |
23780 KB |
Output is correct |
6 |
Correct |
12 ms |
23836 KB |
Output is correct |
7 |
Correct |
12 ms |
23764 KB |
Output is correct |
8 |
Correct |
17 ms |
23764 KB |
Output is correct |
9 |
Correct |
15 ms |
23892 KB |
Output is correct |
10 |
Correct |
14 ms |
23828 KB |
Output is correct |
11 |
Correct |
282 ms |
43152 KB |
Output is correct |
12 |
Correct |
376 ms |
43188 KB |
Output is correct |
13 |
Correct |
293 ms |
43196 KB |
Output is correct |
14 |
Correct |
327 ms |
43232 KB |
Output is correct |
15 |
Correct |
293 ms |
43220 KB |
Output is correct |
16 |
Correct |
291 ms |
42596 KB |
Output is correct |
17 |
Correct |
426 ms |
42572 KB |
Output is correct |
18 |
Correct |
277 ms |
42496 KB |
Output is correct |
19 |
Correct |
275 ms |
43104 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
138 ms |
38716 KB |
Output is correct |
2 |
Correct |
89 ms |
37116 KB |
Output is correct |
3 |
Correct |
101 ms |
42540 KB |
Output is correct |
4 |
Correct |
148 ms |
38604 KB |
Output is correct |
5 |
Correct |
184 ms |
38724 KB |
Output is correct |
6 |
Correct |
137 ms |
37964 KB |
Output is correct |
7 |
Correct |
137 ms |
37964 KB |
Output is correct |
8 |
Correct |
133 ms |
37952 KB |
Output is correct |
9 |
Correct |
140 ms |
38292 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23828 KB |
Output is correct |
2 |
Correct |
13 ms |
23832 KB |
Output is correct |
3 |
Correct |
12 ms |
23800 KB |
Output is correct |
4 |
Correct |
16 ms |
23892 KB |
Output is correct |
5 |
Correct |
12 ms |
23780 KB |
Output is correct |
6 |
Correct |
12 ms |
23836 KB |
Output is correct |
7 |
Correct |
12 ms |
23764 KB |
Output is correct |
8 |
Correct |
17 ms |
23764 KB |
Output is correct |
9 |
Correct |
15 ms |
23892 KB |
Output is correct |
10 |
Correct |
14 ms |
23828 KB |
Output is correct |
11 |
Correct |
282 ms |
43152 KB |
Output is correct |
12 |
Correct |
376 ms |
43188 KB |
Output is correct |
13 |
Correct |
293 ms |
43196 KB |
Output is correct |
14 |
Correct |
327 ms |
43232 KB |
Output is correct |
15 |
Correct |
293 ms |
43220 KB |
Output is correct |
16 |
Correct |
291 ms |
42596 KB |
Output is correct |
17 |
Correct |
426 ms |
42572 KB |
Output is correct |
18 |
Correct |
277 ms |
42496 KB |
Output is correct |
19 |
Correct |
275 ms |
43104 KB |
Output is correct |
20 |
Correct |
138 ms |
38716 KB |
Output is correct |
21 |
Correct |
89 ms |
37116 KB |
Output is correct |
22 |
Correct |
101 ms |
42540 KB |
Output is correct |
23 |
Correct |
148 ms |
38604 KB |
Output is correct |
24 |
Correct |
184 ms |
38724 KB |
Output is correct |
25 |
Correct |
137 ms |
37964 KB |
Output is correct |
26 |
Correct |
137 ms |
37964 KB |
Output is correct |
27 |
Correct |
133 ms |
37952 KB |
Output is correct |
28 |
Correct |
140 ms |
38292 KB |
Output is correct |
29 |
Correct |
942 ms |
86332 KB |
Output is correct |
30 |
Correct |
766 ms |
82348 KB |
Output is correct |
31 |
Correct |
820 ms |
95896 KB |
Output is correct |
32 |
Correct |
901 ms |
86396 KB |
Output is correct |
33 |
Correct |
941 ms |
86444 KB |
Output is correct |
34 |
Correct |
895 ms |
84132 KB |
Output is correct |
35 |
Correct |
911 ms |
83848 KB |
Output is correct |
36 |
Correct |
900 ms |
83752 KB |
Output is correct |
37 |
Correct |
866 ms |
85216 KB |
Output is correct |
38 |
Correct |
597 ms |
93056 KB |
Output is correct |
39 |
Correct |
601 ms |
93124 KB |
Output is correct |
40 |
Correct |
581 ms |
89728 KB |
Output is correct |
41 |
Correct |
589 ms |
89184 KB |
Output is correct |
42 |
Correct |
584 ms |
89324 KB |
Output is correct |
43 |
Correct |
592 ms |
91012 KB |
Output is correct |
44 |
Correct |
662 ms |
92372 KB |
Output is correct |
45 |
Correct |
631 ms |
92448 KB |
Output is correct |
46 |
Correct |
624 ms |
89272 KB |
Output is correct |
47 |
Correct |
621 ms |
88840 KB |
Output is correct |
48 |
Correct |
622 ms |
88784 KB |
Output is correct |
49 |
Correct |
648 ms |
90848 KB |
Output is correct |
50 |
Correct |
710 ms |
90288 KB |
Output is correct |
51 |
Correct |
712 ms |
90160 KB |
Output is correct |
52 |
Correct |
693 ms |
87628 KB |
Output is correct |
53 |
Correct |
705 ms |
87368 KB |
Output is correct |
54 |
Correct |
741 ms |
87364 KB |
Output is correct |
55 |
Correct |
730 ms |
89008 KB |
Output is correct |