#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> pll;
const int MAXN = 5e5;
const ll INF = 1e18;
int N, Q;
ll A[MAXN+10], B[MAXN+10], ans[MAXN+10];
vector<pii> V;
struct Query { int l, r, p; };
Query query[MAXN+10];
struct Node
{
ll A, B, BA;
Node() : A(-INF), B(-INF), BA(-INF) {}
};
Node add(Node lc, Node rc)
{
Node ret;
ret.BA=max({lc.BA, rc.BA, lc.B+rc.A});
ret.A=max(lc.A, rc.A);
ret.B=max(lc.B, rc.B);
return ret;
}
Node tree[MAXN*4+10];
void init(int node, int tl, int tr)
{
if(tl==tr)
{
tree[node].A=A[tl];
tree[node].B=B[tl];
tree[node].BA=A[tl]+B[tl];
return;
}
int mid=tl+tr>>1;
init(node*2, tl, mid);
init(node*2+1, mid+1, tr);
tree[node]=add(tree[node*2], tree[node*2+1]);
}
void update(int node, int tl, int tr, int pos)
{
if(tl==tr)
{
tree[node].A=A[tl];
tree[node].B=B[tl];
tree[node].BA=A[tl]+B[tl];
return;
}
int mid=tl+tr>>1;
if(pos<=mid) update(node*2, tl, mid, pos);
else update(node*2+1, mid+1, tr, pos);
tree[node]=add(tree[node*2], tree[node*2+1]);
}
Node qquery(int node, int tl, int tr, int l, int r)
{
if(l<=tl && tr<=r) return tree[node];
if(r<tl || tr<l) return Node();
int mid=tl+tr>>1;
return add(qquery(node*2, tl, mid, l, r), qquery(node*2+1, mid+1, tr, l, r));
}
int main()
{
int i, j, k;
scanf("%d", &N);
for(i=1; i<=N; i++) scanf("%lld", &A[i]), B[i]=-INF;
scanf("%d", &Q);
for(i=1; i<=Q; i++) scanf("%d%d", &query[i].l, &query[i].r), query[i].p=i;
vector<int> S;
for(i=1; i<=N; i++)
{
while(!S.empty() && A[S.back()]<=A[i]) V.push_back({S.back(), i}), S.pop_back();
if(!S.empty()) V.push_back({S.back(), i});
S.push_back(i);
}
sort(V.begin(), V.end(), greater<pii>());
sort(query+1, query+Q+1, [&](const Query &p, const Query &q) { return p.l>q.l; });
init(1, 1, N);
//for(auto it : V) printf("%d %d\n", it.first, it.second);
for(i=N, j=0, k=1; i>=1; i--)
{
for(; j<V.size() && V[j].first==i; j++)
{
int t=V[j].second*2-V[j].first;
if(t>N) continue;
B[t]=A[V[j].first]+A[V[j].second];
update(1, 1, N, t);
}
for(; k<=Q && query[k].l==i; k++)
{
ans[query[k].p]=qquery(1, 1, N, query[k].l, query[k].r).BA;
}
}
for(i=1; i<=Q; i++) printf("%lld\n", ans[i]);
}
Compilation message
jumps.cpp: In function 'void init(int, int, int)':
jumps.cpp:44:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid=tl+tr>>1;
~~^~~
jumps.cpp: In function 'void update(int, int, int, int)':
jumps.cpp:59:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid=tl+tr>>1;
~~^~~
jumps.cpp: In function 'Node qquery(int, int, int, int, int)':
jumps.cpp:69:12: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
int mid=tl+tr>>1;
~~^~~
jumps.cpp: In function 'int main()':
jumps.cpp:98:10: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
for(; j<V.size() && V[j].first==i; j++)
~^~~~~~~~~
jumps.cpp:76:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &N);
~~~~~^~~~~~~~~~
jumps.cpp:77:42: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(i=1; i<=N; i++) scanf("%lld", &A[i]), B[i]=-INF;
~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~
jumps.cpp:78:7: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d", &Q);
~~~~~^~~~~~~~~~
jumps.cpp:79:61: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
for(i=1; i<=Q; i++) scanf("%d%d", &query[i].l, &query[i].r), query[i].p=i;
~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
47352 KB |
Output is correct |
2 |
Incorrect |
29 ms |
47352 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
47352 KB |
Output is correct |
2 |
Incorrect |
29 ms |
47352 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
169 ms |
54756 KB |
Output is correct |
2 |
Correct |
102 ms |
52716 KB |
Output is correct |
3 |
Correct |
100 ms |
53860 KB |
Output is correct |
4 |
Correct |
187 ms |
54628 KB |
Output is correct |
5 |
Correct |
167 ms |
54756 KB |
Output is correct |
6 |
Correct |
153 ms |
54756 KB |
Output is correct |
7 |
Correct |
161 ms |
54628 KB |
Output is correct |
8 |
Correct |
153 ms |
54628 KB |
Output is correct |
9 |
Correct |
170 ms |
54632 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
47352 KB |
Output is correct |
2 |
Incorrect |
29 ms |
47352 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |