#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define vi vector<int>
#define vl vector<long long>
#define ff first
#define ss second
#define pii pair<int,int>
#define pll pair<long long, long long>
#define pb push_back
#define rep(i, b) for(int i = 0; i < (b); ++i)
#define rep2(i,a,b) for(int i = a; i <= (b); ++i)
#define rep3(i,a,b,c) for(int i = a; i <= (b); i+=c)
#define count_bits(x) __builtin_popcountll((x))
#define all(x) (x).begin(),(x).end()
#define siz(x) (int)(x).size()
#define forall(it,x) for(auto& it:(x))
using namespace std;
//mt19937 mt;void random(){mt.seed(chrono::time_point_cast<chrono::milliseconds>(chrono::high_resolution_clock::now()).time_since_epoch().count());}
//ll rand(ll a, ll b) {return a + (mt() % (b-a+1));}
const int INF = 1e9+50;
const ll INF_L = 1e18+40;
const ll MOD = 1e9+7;
struct node_str
{
ll ans = 0,max_act = -1e18,oper = 0;
node_str operator+(const node_str& other)
{
node_str res;
res.ans = max(ans,other.ans);
res.max_act = max(max_act,other.max_act);
return res;
}
void change(ll x)
{
ans = max(ans,max_act+x);
oper = max(x,oper);
}
};
const int tree_siz = 1024*2048-1;
node_str drzewo[tree_siz+1];
void spych(int v)
{
drzewo[v*2].change(drzewo[v].oper);
drzewo[v*2+1].change(drzewo[v].oper);
drzewo[v].oper = 0;
}
void max_all(ll x)
{
drzewo[1].oper = max(drzewo[1].oper,x);
drzewo[1].ans = max(drzewo[1].ans,drzewo[1].max_act+drzewo[1].oper);
}
ll get_ans(int akt, int p1, int p2, int s1, int s2)
{
if(p2 < s1 || p1 > s2) return -1e18;
if(p1 >= s1 && p2 <= s2) return drzewo[akt].ans;
spych(akt);
return max(get_ans(akt*2,p1,(p1+p2)/2,s1,s2),get_ans(akt*2+1,(p1+p2)/2+1,p2,s1,s2));
}
void set_on(int akt, int l, int r, int poz, ll x)
{
if(l == r)
{
drzewo[akt].ans = x;
drzewo[akt].max_act = x;
return;
}
spych(akt);
if((l+r)/2 >= poz) set_on(akt*2,l,(l+r)/2,poz,x);
else set_on(akt*2+1,(l+r)/2+1,r,poz,x);
drzewo[akt] = drzewo[akt*2]+drzewo[akt*2+1];
}
ll arr[500003];
vector<pii> queries[500003];
ll ans[500003];
vi active_pairs[500003];
int main()
{
ios_base::sync_with_stdio(0);cin.tie(0);cout.tie(0);
//random();
int n;
cin >> n;
rep2(i,1,n) cin >> arr[i];
vector<pii> pairs;
stack<int> st;
rep2(i,1,n)
{
while(!st.empty() && arr[st.top()] < arr[i])
{
st.pop();
}
if(siz(st) != 0)
{
pairs.pb({st.top(),i});
}
st.push(i);
}
while(!st.empty()) st.pop();
for(int i = n; i >= 1; i--)
{
while(!st.empty() && arr[st.top()] < arr[i])
{
st.pop();
}
if(siz(st) != 0)
{
pairs.pb({i,st.top()});
}
st.push(i);
}
sort(all(pairs));
int q;
cin >> q;
rep(qq,q)
{
int l,r;
cin >> l >> r;
int p = 0;
int k = siz(pairs)-1;
int ans = siz(pairs);
while(p <= k)
{
int mid = (p+k)/2;
if(pairs[mid].ff >= l)
{
ans = mid;
k = mid-1;
}
else
{
p = mid+1;
}
}
queries[r].pb({ans,qq});
}
rep(i,siz(pairs))
{
active_pairs[2*pairs[i].ss - pairs[i].ff].pb(i);
}
rep2(i,1,n)
{
forall(it,active_pairs[i])
{
set_on(1,0,tree_siz/2,it,arr[pairs[it].ff]+arr[pairs[it].ss]);
}
max_all(arr[i]);
forall(it,queries[i])
{
ans[it.ss] = get_ans(1,0,tree_siz/2,it.ff,tree_siz/2);
}
}
rep(i,q) cout << ans[i] << "\n";
}
# | 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... |