#include <bits/stdc++.h>
//#include <ext/pb_ds/assoc_container.hpp>
//#include <ext/pb_ds/tree_policy.hpp>
//#include "bits_stdc++.h"
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define lb lower_bound
#define ub upper_bound
#define input(x) scanf("%lld", &x);
#define input2(x, y) scanf("%lld%lld", &x, &y);
#define input3(x, y, z) scanf("%lld%lld%lld", &x, &y, &z);
#define input4(x, y, z, a) scanf("%lld%lld%lld%lld", &x, &y, &z, &a);
#define print(x, y) printf("%lld%c", x, y);
#define show(x) cerr << #x << " is " << x << endl;
#define show2(x,y) cerr << #x << " is " << x << " " << #y << " is " << y << endl;
#define show3(x,y,z) cerr << #x << " is " << x << " " << #y << " is " << y << " " << #z << " is " << z << endl;
#define discretize(x) sort(x.begin(), x.end()); x.erase(unique(x.begin(), x.end()), x.end());
using namespace std;
//using namespace __gnu_pbds;
//#define ordered_set tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update>
//#define ordered_multiset tree<int, null_type, less_equal<int>, srb_tree_tag, tree_order_statistics_node_update>
typedef long long ll;
typedef long double ld;
typedef pair<ld, ll> pd;
typedef pair<string, ll> psl;
typedef pair<ll, ll> pi;
typedef pair<pi, ll> piii;
typedef pair<pi, pi> pii;
ll const INF = 1e9;
ll n, q, l, r;
ll arr[500005], ans[500005];
vector<ll> nxt[500005];
vector<pi> queries[500005];
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=0;
lazy=-1;
if (s!=e){
l = new node(s, m);
r = new node(m+1, e);
c=max(l->c, r->c);
}
else c=arr[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;
int main()
{
input(n);
deque<ll> dq;
for (ll i=0; i<n; ++i) input(arr[i]);
input(q);
for (ll i=0; i<q; ++i)
{
input2(l, r);
l--; r--;
queries[l].pb(mp(r, i));
}
// abuse monotonicity
for (ll i=n-1; i>=0; --i)
{
while (dq.size() && arr[dq.back()]<arr[i]) dq.pop_back();
if (dq.size()) nxt[i].pb(dq.back());
dq.pb(i);
}
dq.clear();
for (ll i=0; i<n; ++i)
{
while (dq.size() && arr[dq.back()]<arr[i]) dq.pop_back();
if (dq.size()) nxt[dq.back()].pb(i);
dq.pb(i);
}
// SGTREE
st= new node(0, n-1);
for (ll i=n-1; i>=0; --i)
{
//show(i);
for (ll u: nxt[i])
{
//show2(u, u*2-i);
ll dist = u-i;
if (u+dist<=n-1) st->up(u+dist, n-1, arr[i] + arr[u]);
}
for (pi q: queries[i]) {ans[q.s] = st ->query(i, q.f);}
}
for (ll i=0; i<q; ++i) print(ans[i], '\n');
return 0;
}
Compilation message
jumps.cpp: In function 'int main()':
jumps.cpp:11:23: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
11 | #define input(x) scanf("%lld", &x);
| ~~~~~^~~~~~~~~~~~
jumps.cpp:86:2: note: in expansion of macro 'input'
86 | input(n);
| ^~~~~
jumps.cpp:11:23: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
11 | #define input(x) scanf("%lld", &x);
| ~~~~~^~~~~~~~~~~~
jumps.cpp:88:25: note: in expansion of macro 'input'
88 | for (ll i=0; i<n; ++i) input(arr[i]);
| ^~~~~
jumps.cpp:11:23: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
11 | #define input(x) scanf("%lld", &x);
| ~~~~~^~~~~~~~~~~~
jumps.cpp:89:2: note: in expansion of macro 'input'
89 | input(q);
| ^~~~~
jumps.cpp:12:27: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
12 | #define input2(x, y) scanf("%lld%lld", &x, &y);
| ~~~~~^~~~~~~~~~~~~~~~~~~~
jumps.cpp:92:3: note: in expansion of macro 'input2'
92 | input2(l, r);
| ^~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
27276 KB |
Output is correct |
2 |
Incorrect |
6 ms |
27228 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
27276 KB |
Output is correct |
2 |
Incorrect |
6 ms |
27228 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
133 ms |
64084 KB |
Output is correct |
2 |
Incorrect |
108 ms |
61644 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
6 ms |
27276 KB |
Output is correct |
2 |
Incorrect |
6 ms |
27228 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |