This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#include <bits/stdc++.h>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
typedef pair<ll,ll> pll;
typedef pair<ld,ld> pdd;
#define ff(i,a,b) for(int i = a; i <= b; i++)
#define fb(i,b,a) for(int i = b; i >= a; i--)
#define trav(a,x) for (auto& a : x)
#define sz(a) (int)(a).size()
#define pb push_back
#define fi first
#define se second
#define lb lower_bound
#define ub upper_bound
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
template<typename T>
using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
// os.order_of_key(k) the number of elements in the os less than k
// *os.find_by_order(k) print the k-th smallest number in os(0-based)
const int mod = 1000000007;
const int inf = 1e9 + 5;
const int mxN = 500005;
int n, q;
int niz[mxN];
vector<pii> upit[mxN];
ll res[mxN];
ll bor[4 * mxN][2]; // 1 - max update, 0 - max vrednost
ll lenj[4 * mxN];
void build(int v, int tl, int tr){
if(tl == tr){
bor[v][0] = bor[v][1] = niz[tl];
return;
}
int mid = (tl + tr) / 2;
build(v * 2, tl, mid); build(v * 2 + 1, mid + 1, tr);
bor[v][0] = max(bor[v * 2][0], bor[v * 2 + 1][0]);
bor[v][1] = max(bor[v * 2][1], bor[v * 2 + 1][1]);
}
void propagate(int v, int tl, int tr){
if(lenj[v] != 0){
bor[v][1] = max(bor[v][1], bor[v][0] + lenj[v]);
if(tl != tr){
lenj[v * 2] = max(lenj[v * 2], lenj[v]);
lenj[v * 2 + 1] = max(lenj[v * 2 + 1], lenj[v]);
}
lenj[v] = 0;
}
}
void lazyupd(int v, int tl, int tr, int l, int r, ll val){
propagate(v, tl, tr);
if(tl > tr || l > tr || tl > r)return;
if(tl >= l && tr <= r){
lenj[v] = max(lenj[v], val);
propagate(v, tl, tr);
return;
}
int mid = (tl + tr) / 2;
lazyupd(v * 2, tl, mid, l, r, val); lazyupd(v * 2 + 1, mid + 1, tr, l, r, val);
bor[v][1] = max(bor[v * 2][1], bor[v * 2 + 1][1]);
}
ll kveri(int v, int tl, int tr, int l, int r){
propagate(v, tl, tr);
if(tl > r || l > tr)return 0;
if(tl >= l && tr <= r)return bor[v][1];
int mid = (tl + tr) / 2;
return max(kveri(v * 2, tl, mid, l, r), kveri(v * 2 + 1, mid + 1, tr, l, r));
}
int main() {
cin.tie(0)->sync_with_stdio(0);
cin >> n;
ff(i,1,n)cin >> niz[i];
cin >> q;
ff(i,1,q){
int l, r;
cin >> l >> r;
upit[l].pb({r,i});
}
stack<int> stek; build(1,1,n);
fb(i,n,1){
while(sz(stek) && niz[i] > niz[stek.top()]){
int j = stek.top(); stek.pop();
lazyupd(1,1,n,j + j - i,n,niz[i] + niz[j]);
}
if(sz(stek) != 0){
int j = stek.top();
lazyupd(1,1,n,j + j - i,n,niz[i] + niz[j]);
}
stek.push(i);
for(auto c : upit[i]){
int r = c.fi;
int id = c.se;
res[id] = kveri(1,1,n,i,r);
}
}
ff(i,1,q)cout << res[i] << '\n';
return 0;
}
/*
3
1000000000 1000000000 1000000000
1
1 3
15
12 96 100 61 54 66 37 34 58 21 21 1 13 50 81
12
1 15
3 12
11 14
1 13
5 9
4 6
6 14
2 5
4 15
1 7
1 10
8 13
5
5 4 4 5 4
1
1 5
5
5 2 1 5 3
3
1 4
2 5
1 5
// probati bojenje sahovski
*/
# | 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... |