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 <bits/stdc++.h>
using namespace std;
using ll = long long;
using pii = pair<int, int>;
#define pb push_back
#define ff first
#define ss second
#define ins insert
const int inf = 1e9;
const int infm = -inf;
struct ST{
struct data{
int a, b;
};
vector<data> t;
vector<int> p;
int n;
ST(int ns){
n = ns;
t.resize(4 * n);
p.resize(4 * n);
for (int i = 1; i < t.size(); i++){
t[i].a = t[i].b = p[i] = infm;
}
}
void apply(int v, int& x){
t[v].b = max(t[v].b, t[v].a + x);
p[v] = max(p[v], x);
}
void merge(int& v){
int vv = 2 * v;
t[v].a = max(t[vv].a, t[vv + 1].a);
t[v].b = max(t[vv].b, t[vv + 1].b);
}
void push(int& v, int& vv){
if (p[v] == infm) return;
apply(vv, p[v]);
apply(vv + 1, p[v]);
p[v] = infm;
}
void upd(int v, int tl, int tr, int& p, int& x){
if (tl == tr){
t[v].a = x;
return;
}
int tm = (tl + tr) / 2, vv = 2 * v;
push(v, vv);
if (p <= tm){
upd(vv, tl, tm, p, x);
}
else {
upd(vv + 1, tm + 1, tr, p, x);
}
merge(v);
}
void upd(int p, int x){
upd(1, 1, n, p, x);
}
void updr(int v, int tl, int tr, int& l, int& r, int& x){
if (l > tr || r < tl) return;
if (l <= tl && tr <= r){
apply(v, x);
return;
}
int tm = (tl + tr) / 2, vv = 2 * v;
push(v, vv);
updr(vv, tl, tm, l, r, x);
updr(vv + 1, tm + 1, tr, l, r, x);
merge(v);
}
void updr(int l, int r, int x){
updr(1, 1, n, l, r, x);
}
int get(int v, int tl, int tr, int& l, int& r){
if (l > tr || r < tl) return infm;
if (l <= tl && tr <= r){
return t[v].b;
}
int tm = (tl + tr) / 2, vv = 2 * v;
push(v, vv);
return max(get(vv, tl, tm, l, r), get(vv + 1, tm + 1, tr, l, r));
}
int get(int l, int r){
return get(1, 1, n, l, r);
}
};
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
int n; cin>>n;
vector<int> h(n + 1), a(n + 1), b(n + 1);
vector<int> st[n + 1], en[n + 1];
pii f[n + 1][2];
for (int i = 1; i <= n; i++){
cin>>h[i]>>a[i]>>b[i];
int l = max(1, i - b[i]), r = i - a[i];
if (l <= r){
f[i][0] = {l, r};
}
l = i + a[i]; r = min(n, i + b[i]);
if (l <= r){
f[i][1] = {l, r};
st[l].pb(i);
en[r].pb(i);
}
}
int q; cin>>q;
vector<pii> end[n + 1];
for (int i = 1; i <= q; i++){
int l, r; cin>>l>>r;
end[r].pb({l, i});
}
vector<int> out(q + 1, -1);
function<void()> solve = [&](){
ST T(n);
for (int i = 1; i <= n; i++) T.upd(i, -inf);
for (int i = 1; i <= n; i++){
for (int j: st[i]){
T.upd(j, h[j]);
}
auto &[l, r] = f[i][0];
if (l > 0) T.updr(l, r, -h[i]);
for (auto [ql, ii]: end[i]){
out[ii] = max(out[ii], T.get(ql, i));
}
for (int j: en[i]){
T.upd(j, -inf);
}
}
};
solve();
for (int i = 1; i <= n; i++) h[i] = -h[i];
solve();
for (int i = 1; i <= q; i++){
cout<<out[i]<<"\n";
}
}
Compilation message (stderr)
antennas.cpp: In constructor 'ST::ST(int)':
antennas.cpp:23:27: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<ST::data>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
23 | for (int i = 1; i < t.size(); 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... |