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>
#define fi first
#define se second
#define pb push_back
#define sz(a) (int)a.size()
#define all(a) a.begin(), a.end()
#define rall(a) a.rbegin(), a.rend()
#define ff(i,a,b) for(int i=a;i<=b;i++)
#define fb(i,b,a) for(int i=b;i>=a;i--)
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef pair<int,int> pii;
const int maxn = 500005;
const int inf = 1e9 + 5;
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)
int n, q;
ll niz[maxn];
int L[maxn];
int R[maxn];
vector<pii> upit[maxn];
vector<pii> ja[maxn];
int res[maxn];
ll bor[4 * maxn][2]; // 0 - vrednost, 1 - vrednost + koliko_sam_dodao
ll lenj[4 * maxn];
void build(int v, int tl, int tr){
bor[v][1] = -inf;
if(tl == tr){
bor[v][0] = 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]);
}
void propagate(int v, int tl, int tr){
if(lenj[v]){
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){
bor[v][1] = max(bor[v][1], bor[v][0] + val);
if(tl != tr){
lenj[v * 2] = max(lenj[v * 2], val);
lenj[v * 2 + 1] = max(lenj[v * 2 + 1], val);
}
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(l > tr || tl > r)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));
}
void add(int X, int Y){
if(X != 0 && Y != 0 && 2 * Y - X <= n){
ja[X].pb({2 * Y - X, niz[X] + niz[Y]});
}
}
int main()
{
ios::sync_with_stdio(false);
cout.tie(nullptr);
cin.tie(nullptr);
cin >> n;
ff(i,1,n)cin >> niz[i];
stack<int> stek;
fb(i,n,1){
while(!stek.empty() && niz[stek.top()] <= niz[i]){
L[stek.top()] = i;
stek.pop();
}
stek.push(i);
}
while(!stek.empty()){
stek.pop();
}
ff(i,1,n){
while(!stek.empty() && niz[stek.top()] <= niz[i]){
R[stek.top()] = i;
stek.pop();
}
stek.push(i);
}
// (i, i + 1), (i, R[i]), (L[i], i) - to su parovi za A i B
ff(i,1,n){
add(i, i + 1);
add(i, R[i]);
add(L[i], i);
}
cin >> q;
ff(i,1,q){
int l, r;
cin >> l >> r;
upit[l].pb({r, i});
}
build(1,1,n);
fb(i,n,1){
for(auto c : ja[i]){
lazyupd(1,1,n,c.fi,n,c.se);
}
// ako hocu da i bude C, za i vazi da mogu da uzmem bilo koju vrednost levo
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;
}
/**
5
5 2 1 5 3
3
1 4
2 5
1 5
// probati bojenje sahovski ili slicno
**/
# | 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... |