#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
vector<int> vect;
struct node{
int s, e, m, val, lazy, c, sum;
node *l, *r;
void propagate(){
if (lazy==-1)return;
val=max(val, lazy);
sum=max(sum, val+c);
if (s!=e){
l->lazy=max(l->lazy, lazy);
r->lazy=max(r->lazy, lazy);
}
lazy=-1;
}
node(int S, int E){
s = S, e = E, m = (s+e)/2;
val=sum=-1e9;
lazy=-1;
if (s!=e){
l = new node(s, m);
r = new node(m+1, e);
c=max(l->c, r->c);
}
else c=vect[s];
}
void up(int left, int right, int v){
if (left>right)return;
propagate();
if (s==left && e==right)lazy=max(lazy, v);
else{
if (right<=m)l->up(left, right, v);
else if (left>m)r->up(left, right, v);
else l->up(left, m, v), r->up(m+1, right, v);
r->propagate(), l->propagate();
sum=max(l->val+l->c, r->val+r->c);
}
}
int query(int left, int right){
propagate();
if (s==left && e==right)return sum;
if (right<=m)return l->query(left, right);
else if (left>m)return r->query(left, right);
else return max(l->query(left, m), r->query(m+1, right));
}
}*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->up(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";
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
177 ms |
52184 KB |
Output is correct |
2 |
Incorrect |
137 ms |
51360 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Incorrect |
1 ms |
344 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |