#include <iostream>
#include <map>
#include <vector>
#include <climits>
#include <stack>
#include <string>
#include <queue>
#include <algorithm>
#include <set>
#include <unordered_set>
#include <unordered_map>
#include <cmath>
#include <cctype>
#include <bitset>
#include <iomanip>
#include <cstring>
#include <numeric>
using namespace std;
#define int long long
#define pb push_back
#define mp make_pair
#define pii pair<int, int>
#define fi first
#define se second
#define ll long long
ll const INF = 1e9;
vector<int> vect;
struct node{
int s, e, m;
ll aval, summ, pv, lazy;
node *l, *r;
node (int S, int E)
{
s = S, e = E, m = (s+e)/2;
summ = pv = -INF;
if (s==e) {aval = vect[s]; return;}
l = new node(s, m);
r = new node(m+1, e);
aval = max(l->aval, r->aval);
lazy = -INF;
}
void propogate()
{
if (lazy==-INF) return;
if (s!=e)
{
l->lazy = max(l->lazy, lazy);
r->lazy = max(r->lazy, lazy);
l->pv= max(l->pv, lazy);
l->summ = max(l->summ, l->aval + lazy);
r->pv= max(r->pv, lazy);
r->summ = max(r->summ, r->aval + lazy);
}
lazy = -INF;
}
void update(int S, int E, ll V)
{
if (S>E)return;
propogate();
if(s==S && e==E)
{
lazy = max(lazy, V);
pv= max(pv, V);
summ = max(summ, aval + pv);
}
else{
propogate();
if(E <= m) l->update(S, E, V);
else if (m < S) r->update(S, E, V);
else l->update(S, m, V),r->update(m+1, E, V);
l->propogate(),r->propogate();
summ = max(l->summ, r->summ);
}
}
ll query(int S, int E)
{
propogate();
if(s == S && e == E) return summ;
propogate();
if(E <= m) return l->query(S, E);
else if(S >= m+1) return r->query(S, E);
else return max(l->query(S, m), r->query(m+1, E));
}
} *st;
int32_t main(){
int n, q, a, b;
cin>>n;
vect.resize(n+1);
vector<vector<pii> > queries(n+1);
for (int i=1; i<=n; ++i)cin>>vect[i];
cin>>q;
vector<int> ans(q);
vector<vector<int> > next(n+1);
st = new node(1, n);
for (int i=0; i<q; ++i){
cin>>a>>b;
queries[a].pb(mp(b, i));
}
stack<int> s;
for (int i=1; i<=n; ++i){
while (!s.empty()&&vect[s.top()]<=vect[i])next[s.top()].pb(i), s.pop();
s.push(i);
}
while (!s.empty())s.pop();
for (int i=n; i>=1; --i){
while (!s.empty()&&vect[s.top()]<=vect[i])next[i].pb(s.top()), s.pop();
s.push(i);
}
for (int i=n; i>=1; --i){
for (auto c:next[i])st->update(2*c-i, n, vect[i]+vect[c]);
for (auto c:queries[i])ans[c.se]=st->query(i, c.fi);
}
for (auto a:ans)cout<<a<<"\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
296 ms |
23652 KB |
Output is correct |
12 |
Correct |
289 ms |
23632 KB |
Output is correct |
13 |
Correct |
306 ms |
23956 KB |
Output is correct |
14 |
Correct |
296 ms |
23728 KB |
Output is correct |
15 |
Correct |
324 ms |
23700 KB |
Output is correct |
16 |
Correct |
305 ms |
23168 KB |
Output is correct |
17 |
Correct |
338 ms |
23124 KB |
Output is correct |
18 |
Correct |
308 ms |
22908 KB |
Output is correct |
19 |
Correct |
306 ms |
23736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
179 ms |
51248 KB |
Output is correct |
2 |
Correct |
129 ms |
50596 KB |
Output is correct |
3 |
Correct |
127 ms |
48992 KB |
Output is correct |
4 |
Correct |
182 ms |
51420 KB |
Output is correct |
5 |
Correct |
174 ms |
51224 KB |
Output is correct |
6 |
Correct |
175 ms |
51428 KB |
Output is correct |
7 |
Correct |
164 ms |
51408 KB |
Output is correct |
8 |
Correct |
158 ms |
51284 KB |
Output is correct |
9 |
Correct |
185 ms |
51540 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
296 ms |
23652 KB |
Output is correct |
12 |
Correct |
289 ms |
23632 KB |
Output is correct |
13 |
Correct |
306 ms |
23956 KB |
Output is correct |
14 |
Correct |
296 ms |
23728 KB |
Output is correct |
15 |
Correct |
324 ms |
23700 KB |
Output is correct |
16 |
Correct |
305 ms |
23168 KB |
Output is correct |
17 |
Correct |
338 ms |
23124 KB |
Output is correct |
18 |
Correct |
308 ms |
22908 KB |
Output is correct |
19 |
Correct |
306 ms |
23736 KB |
Output is correct |
20 |
Correct |
179 ms |
51248 KB |
Output is correct |
21 |
Correct |
129 ms |
50596 KB |
Output is correct |
22 |
Correct |
127 ms |
48992 KB |
Output is correct |
23 |
Correct |
182 ms |
51420 KB |
Output is correct |
24 |
Correct |
174 ms |
51224 KB |
Output is correct |
25 |
Correct |
175 ms |
51428 KB |
Output is correct |
26 |
Correct |
164 ms |
51408 KB |
Output is correct |
27 |
Correct |
158 ms |
51284 KB |
Output is correct |
28 |
Correct |
185 ms |
51540 KB |
Output is correct |
29 |
Correct |
1282 ms |
149844 KB |
Output is correct |
30 |
Correct |
1116 ms |
147644 KB |
Output is correct |
31 |
Correct |
1169 ms |
143680 KB |
Output is correct |
32 |
Correct |
1231 ms |
149732 KB |
Output is correct |
33 |
Correct |
1280 ms |
149736 KB |
Output is correct |
34 |
Correct |
1280 ms |
149296 KB |
Output is correct |
35 |
Correct |
1222 ms |
148980 KB |
Output is correct |
36 |
Correct |
1269 ms |
149076 KB |
Output is correct |
37 |
Correct |
1245 ms |
149732 KB |
Output is correct |
38 |
Correct |
849 ms |
152304 KB |
Output is correct |
39 |
Correct |
804 ms |
152304 KB |
Output is correct |
40 |
Correct |
794 ms |
150612 KB |
Output is correct |
41 |
Correct |
792 ms |
150308 KB |
Output is correct |
42 |
Correct |
763 ms |
150352 KB |
Output is correct |
43 |
Correct |
785 ms |
151408 KB |
Output is correct |
44 |
Correct |
876 ms |
152028 KB |
Output is correct |
45 |
Correct |
863 ms |
152148 KB |
Output is correct |
46 |
Correct |
835 ms |
150512 KB |
Output is correct |
47 |
Correct |
891 ms |
150488 KB |
Output is correct |
48 |
Correct |
821 ms |
150696 KB |
Output is correct |
49 |
Correct |
852 ms |
151524 KB |
Output is correct |
50 |
Correct |
969 ms |
151380 KB |
Output is correct |
51 |
Correct |
1032 ms |
151376 KB |
Output is correct |
52 |
Correct |
990 ms |
150608 KB |
Output is correct |
53 |
Correct |
935 ms |
150608 KB |
Output is correct |
54 |
Correct |
929 ms |
150504 KB |
Output is correct |
55 |
Correct |
943 ms |
151440 KB |
Output is correct |