#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pb push_back
#define eb emplace_back
#define vi vector<int>
#define pi pair<int,int>
#define sz(v) (int)(v).size()
#define all(v) (v).begin(), (v).end()
#define compact(v) (v).erase(unique(all(v)), (v).end())
template<class T> using upq = priority_queue<T, vector<T>, greater<T>>;
template<class T> int lwrbound(const vector<T>& a, const T& b, const int s = 0){return int(lower_bound(s + all(a), b) - a.begin());}
template<class T> int uprbound(const vector<T>& a, const T& b, const int s = 0){return int(upper_bound(s + all(a), b) - a.begin());}
#define FOR(i, a, b) for(int i = (a); i <= (b); i++)
#define ROF(i, a, b) for(int i = (a); i >= (b); i--)
#define sumof(x) accumulate(all(x), 0ll)
#define dbg(x) "[" << #x " = " << (x) << "]"
#define el "\n"
using ll = long long;
using ld = long double;
template<class T> bool ckmx(T& a, const T b){return (a < b ? a = b, true : false);}
template<class T> bool ckmn(T& a, const T b){return (a > b ? a = b, true : false);}
struct Node{
int ans, Mx, trv;
Node(int ans = -1e9, int Mx = -1e9, int trv = -1e9): ans(ans), Mx(Mx), trv(trv) {}
Node operator + (const Node q) const{
Node ret;
ret.ans = max(ans, q.ans);
ret.Mx = max(Mx, q.Mx);
return ret;
}
};
struct SMT{
vector<Node> st; int trsz;
SMT(int n = 0): st((n << 2) | 1), trsz(n) {}
void apply(int id, int val){
ckmx(st[id].ans, val + st[id].Mx);
ckmx(st[id].trv, val);
}
void down(int id){
if(st[id].trv != -1e9){
apply(id << 1, st[id].trv);
apply(id << 1|1, st[id].trv);
st[id].trv = -1e9;
}
}
void setNode(int id, int l, int r, int x, int v){
if(l == r){
if(v) st[id].Mx = v;
else{
st[id].Mx = -1e9;
}
}
else{
int m = l+r>>1; down(id);
if(x <= m)
setNode(id << 1, l, m, x, v);
else setNode(id << 1|1, m+1, r, x, v);
st[id] = st[id << 1] + st[id << 1|1];
}
}
void setNode(int x, int v){
setNode(1, 1, trsz, x, v);
}
void update(int id, int l, int r, int u, int v, int val){
if(l >= u && r <= v){
apply(id, val);
return;
}
int m = l+r>>1; down(id);
if(u <= m) update(id << 1, l, m, u, v, val);
if(v > m) update(id << 1|1, m+1, r, u, v, val);
st[id] = st[id << 1] + st[id << 1|1];
}
void update(int l, int r, int val){
l = max(1, l);
if(l > r) return;
update(1, 1, trsz, l, r, val);
}
int get(int id, int l, int r, int u, int v){
if(l > v || r < u) return -1e9;
if(l >= u && r <= v) return st[id].ans;
int m = l+r>>1; down(id);
return max(get(id << 1, l, m, u, v), get(id << 1|1, m+1, r, u, v));
}
int query(int l, int r){
return get(1, 1, trsz, l, r);
}
};
void Main()
{
int N; cin >> N;
vi H(N + 1), A(N + 1), B(N + 1);
FOR(i, 1, N){
cin >> H[i] >> A[i] >> B[i];
}
int Q; cin >> Q;
vector<pi> q(Q + 1); vector<vi> quer(N + 1);
FOR(i, 1, Q){
cin >> q[i].fi >> q[i].se;
quer[q[i].se].eb(i);
}
vector<vector<pi>> op(N + 1);
vi ans(Q + 1, -1); SMT st1(N), st2(N);
FOR(i, 1, N){
for(pi e : op[i]){
int t = e.fi, w = e.se;
if(t > 0){
st1.setNode(w, -H[w]);
st2.setNode(w, +H[w]);
}
else st1.setNode(w, 0), st2.setNode(w, 0);
}
int L = i - B[i];
int R = i - A[i];
st1.update(L, R, +H[i]);
st2.update(L, R, -H[i]);
for(int j : quer[i]){
ans[j] = max(st1.query(q[j].fi, q[j].se), st2.query(q[j].fi, q[j].se));
}
if(i + A[i] <= N) op[i + A[i]].eb(+1, i);
if(i + B[i] + 1 <= N) op[i + B[i] + 1].eb(-1, i);
}
FOR(i, 1, Q){
cout << (ans[i] < 0 ? -1 : ans[i]) << el;
}
}
int32_t main()
{
ios_base::sync_with_stdio(0);
cin.tie(0); cout.tie(0);
#define name "InvMOD"
if(fopen(name".INP", "r")){
freopen(name".INP", "r", stdin);
freopen(name".OUT", "w", stdout);
}
int t = 1; while(t--) Main();
return 0;
}
Compilation message (stderr)
antennas.cpp: In function 'int32_t main()':
antennas.cpp:167:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
167 | freopen(name".INP", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
antennas.cpp:168:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
168 | freopen(name".OUT", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~| # | 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... |