This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#define LOCAL
#include "bits/stdc++.h"
using namespace std;
#define FAST ios_base::sync_with_stdio(false); cin.tie(0);
#define LLINF ((long long) 1e18)//1234567890987654321
#define INF 1234567890ll
#define pb push_back
#define eb emplace_back
#define ins insert
#define f first
#define s second
#define db 0
#define EPS (1e-7) //0.0000001 the value
#define PI (acos((ld)-1.0))
#define MAXN (500006)
#define ll long long int
#define ld long double
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count()); //can be used by calling rng() or shuffle(A, A+n, rng)
#define FOR(ii, ss, ee) for(ll ii = (ss); ii < (ll)(ee); ++ii)
#define space " "
#define cbr cerr << "hi\n"
#define mmst(x, v) memset((x), v, sizeof ((x)))
#define siz(x) ((ll)x.size())
#define ph push
#define btinpct(x) __builtin_popcountll((x))
#define MSB(bm) (63-__builtin_clzll((bm)))
#define all(x) (x).begin(), (x).end()
#define lbd(x, y) lower_bound(all(x), y)
#define ubd(x, y) upper_bound(all(x), y)
typedef pair <ll, ll> pi;
typedef pair <ll, pi> spi;
typedef pair <pi, pi> dpi;
inline ll rand(ll x, ll y) { ++y; return (rng() % (y-x)) + x; } //inclusivesss
string to_string(char c) {string s(1,c);return s;}string to_string(bool b){return (b ? "true" : "false");}template <typename A, typename B>string to_string(pair<A, B> p) {return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";}template <typename A>string to_string(A v) {bool first = true;string res = "{";for (const auto &x : v) {if (!first) {res += ", ";}first = false;res += to_string(x);}res += "}";return res;}void degug_out() { cerr << endl; }template <typename Head, typename... Tail>void degug_out(Head H, Tail... T) {cerr << " " << to_string(H);degug_out(T...);}inline ll gcd(ll a,ll b){if(a>b)swap(a,b);if(a==0)return b;return gcd(b%a,a);}
#ifdef LOCAL
// #define degug(...) cerr << "[" << #__VA_ARGS__ << "]:", degug_out(__VA_ARGS__)
#else
// #define degug(...) 42
#define cerr if(0)cout
#endif
ll n,q,A[MAXN];
spi Q[MAXN];
ll ans[MAXN]; ll pmx[MAXN];
vector<pi>v;
struct node {
int s,e,m;
ll v,o;ll add;
node *l,*r;
node(ll _S,ll _E){
s=_S,e=_E,m=(s+e)>>1ll;
v=o=0;add=0;
if(s!=e){
l=new node(s,m);
r=new node(m+1,e);
o=max(l->o,r->o);
v=max(l->v,r->v);
} else v=o=A[s],l=r=0;
}
void update(ll x,ll y,ll nval){
value();
if(s==x&&e==y) { add=max(add,nval); return; }
if(x>m)r->update(x,y,nval);
else if(y<=m)l->update(x,y,nval);
else l->update(x,m,nval),r->update(m+1,y,nval);
v=max(l->value(),r->value());
}
ll value() {
if(add) v=max(v,o+add);
if(s^e){
l->add=max(l->add,add);
r->add=max(r->add,add);
}
// add=0;
return v;
}
ll rmq(ll x,ll y){
value();
if(s==x&&e==y)return v;
if(x>m)return r->rmq(x,y);
else if(y<=m)return l->rmq(x,y);
else return max(l->rmq(x,m),r->rmq(m+1,y));
}
} *seg;
int main() // array size ah!!
{
FAST
cin>>n;
FOR(i,1,n+1)cin>>A[i]; for(ll i=n;i>=1;--i)pmx[i]=max(pmx[i+1],A[i]);
cin>>q;
FOR(i,1,q+1)cin>>Q[i].s.f>>Q[i].s.s,Q[i].f=i;
sort(Q+1,Q+q+1,[](spi x,spi y){return x.s.f>y.s.f;});
stack<pi>stk;
FOR(i,1,n+1){
while(stk.size()) {
v.eb(stk.top().s,i);
if(stk.top().f<=A[i]) stk.pop(); else break;
}
stk.emplace(A[i],i);
}
sort(all(v)); seg=new node(1,n);
FOR(i,1,q+1) {
ll ind=Q[i].f,x=Q[i].s.f,y=Q[i].s.s;
while(v.size()&&v.back().f>=x) {
ll a,b;tie(a,b)=v.back();v.pop_back();
if(b+b-a<=n) seg->update(b+b-a,n,A[a]+A[b]);
// cerr<<b+b-a<<' '<<n<<' '<<A[a]+A[b]<<'\n';
} assert(x+2<=y);
ans[ind]=seg->rmq(x+2,y);
}
FOR(i,1,q+1) cout<<ans[i]<<'\n';
}
/*
5
5 2 1 5 3
3
1 4
2 5
1 5
*
*
5
5 4 4 5 4
1
1 5
*
*
*/
Compilation message (stderr)
jumps.cpp: In function 'int main()':
jumps.cpp:19:25: warning: this 'for' clause does not guard... [-Wmisleading-indentation]
#define FOR(ii, ss, ee) for(ll ii = (ss); ii < (ll)(ee); ++ii)
^
jumps.cpp:88:2: note: in expansion of macro 'FOR'
FOR(i,1,n+1)cin>>A[i]; for(ll i=n;i>=1;--i)pmx[i]=max(pmx[i+1],A[i]);
^~~
jumps.cpp:88:25: note: ...this statement, but the latter is misleadingly indented as if it were guarded by the 'for'
FOR(i,1,n+1)cin>>A[i]; for(ll i=n;i>=1;--i)pmx[i]=max(pmx[i+1],A[i]);
^~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |