This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
// https://codeforces.com/blog/entry/66022?#comment-500622
#include <bits/stdc++.h>
using namespace std;
using ll = long long;
using ii = pair<int, int>;
using vi = vector<int>;
#define all(v) begin(v), end(v)
#define sz(v) (int)(v).size()
#define fi first
#define se second
const int inf = 1e9 + 9;
/*struct Node {
vector<int> c, d;
Node(int _, int n) {
c.assign(n, -inf);
d.assign(n, -inf);
}
void update_ci(int pos, int val) {
c[pos-1] = val;
}
void update_di(int L, int R, int val) {
for(int i = L-1; i < R; ++i) {
d[i] = max(d[i], c[i] + val);
}
}
int get_max_di(int L, int R) {
int ret = -inf;
for(int i = L-1; i < R; ++i) {
ret = max(ret, d[i]);
} return ret;
}
};*/
struct Node {
Node *l = 0, *r = 0;
int mx_di = -inf, mx_ci = -inf;
int lo, hi;
int lz = -2*inf;
Node(int _lo, int _hi) : lo(_lo), hi(_hi) {
if(lo < hi) {
int mid = (lo + hi) >> 1;
l = new Node(lo, mid);
r = new Node(mid+1, hi);
}
}
void upd(int val) {
lz = max(lz, val);
mx_di = (int)max((ll)mx_di, (ll)mx_ci + lz);
}
void push() {
if(lo < hi) {
l -> upd(lz);
r -> upd(lz);
}
lz = -2*inf;
}
void update_ci(int pos, int ci) {
if(lo == hi) {
mx_ci = ci;
lz = -2*inf;
} else {
push();
if(pos <= l -> hi)
l -> update_ci(pos, ci);
else
r -> update_ci(pos, ci);
mx_ci = max(l -> mx_ci, r -> mx_ci);
}
}
void update_di(int L, int R, int val) { // do d[i] max= c[i] + val, val negative in this problem
if(hi < L or lo > R) return;
if(L <= lo and hi <= R) {
upd(val);
} else {
push();
l -> update_di(L, R, val);
r -> update_di(L, R, val);
mx_di = max(l -> mx_di, r -> mx_di);
}
}
int get_max_di(int L, int R) { // max{ d[i] | i \in [L, R] }
if(hi < L or lo > R) return -inf;
if(L <= lo and hi <= R) return mx_di;
push();
return max(l->get_max_di(L,R), r->get_max_di(L,R));
}
};
const int N = 2e5 + 5;
int n, q;
int H[N], A[N], B[N];
int ans[N];
int L[N], R[N];
void solve() {
vector<vi> offline(n+2), queries_w_R_at(n+1);
for(int i = 1; i <= q; ++i) {
queries_w_R_at[R[i]].emplace_back(i);
}
for(int i = 1; i <= n; ++i) {
offline[min(n+1, i + A[i])].emplace_back(i);
offline[min(n+1, i + B[i] + 1)].emplace_back(-i);
}
Node *tr = new Node(1, n);
for(int i = 1; i <= n; ++i) {
for(int j : offline[i]) {
if(j < 0) { // del
tr -> update_ci(-j, -inf);
} else { // ins
tr -> update_ci(j, H[j]);
}
}
if(i - A[i] >= 1) {
tr -> update_di(max(1, i - B[i]), i - A[i], -H[i]);
}
for(int j : queries_w_R_at[i]) {
ans[j] = max(ans[j], tr -> get_max_di(L[j], R[j]));
}
}
}
int main(int argc, char const *argv[])
{
#ifdef LOCAL
freopen("in", "r", stdin);
#endif
fill(ans, ans+N, -inf);
scanf("%d", &n);
for(int i = 1; i <= n; ++i) {
scanf("%d %d %d", &H[i], &A[i], &B[i]);
}
scanf("%d", &q);
for(int i = 1; i <= q; ++i) {
scanf("%d %d", &L[i], &R[i]);
}
solve();
for(int i = 1, j = n; i < j; ++i, --j) {
swap(H[i], H[j]);
swap(A[i], A[j]);
swap(B[i], B[j]);
}
for(int i = 1; i <= q; ++i) {
L[i] = n - L[i] + 1;
R[i] = n - R[i] + 1;
swap(L[i], R[i]);
}
solve();
for(int i = 1; i <= q; ++i) {
printf("%d\n", ans[i] < 0 ? -1 : ans[i]);
}
return 0;
}
/*
* use std::array instead of std::vector, if u can
* overflow?
* array bounds
*/
Compilation message (stderr)
antennas.cpp: In function 'int main(int, const char**)':
antennas.cpp:133:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
133 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
antennas.cpp:135:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
135 | scanf("%d %d %d", &H[i], &A[i], &B[i]);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
antennas.cpp:137:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
137 | scanf("%d", &q);
| ~~~~~^~~~~~~~~~
antennas.cpp:139:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
139 | scanf("%d %d", &L[i], &R[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... |