// 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 = -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 = max(mx_di, mx_ci + lz);
}
void push() {
if(lo < hi) {
l -> upd(lz);
r -> upd(lz);
}
lz = -inf;
}
void update_ci(int pos, int ci) {
if(lo == hi) {
mx_ci = ci;
} 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
antennas.cpp: In function 'int main(int, const char**)':
antennas.cpp:132:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
132 | scanf("%d", &n);
| ~~~~~^~~~~~~~~~
antennas.cpp:134:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
134 | scanf("%d %d %d", &H[i], &A[i], &B[i]);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
antennas.cpp:136:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
136 | scanf("%d", &q);
| ~~~~~^~~~~~~~~~
antennas.cpp:138:10: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
138 | scanf("%d %d", &L[i], &R[i]);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1100 KB |
Output is correct |
2 |
Correct |
1 ms |
1100 KB |
Output is correct |
3 |
Correct |
1 ms |
1096 KB |
Output is correct |
4 |
Correct |
1 ms |
1100 KB |
Output is correct |
5 |
Correct |
1 ms |
1100 KB |
Output is correct |
6 |
Correct |
1 ms |
1100 KB |
Output is correct |
7 |
Correct |
1 ms |
1100 KB |
Output is correct |
8 |
Correct |
1 ms |
1076 KB |
Output is correct |
9 |
Correct |
1 ms |
1100 KB |
Output is correct |
10 |
Correct |
1 ms |
1100 KB |
Output is correct |
11 |
Correct |
1 ms |
1092 KB |
Output is correct |
12 |
Correct |
1 ms |
1100 KB |
Output is correct |
13 |
Correct |
2 ms |
1092 KB |
Output is correct |
14 |
Correct |
1 ms |
1100 KB |
Output is correct |
15 |
Correct |
1 ms |
1100 KB |
Output is correct |
16 |
Correct |
1 ms |
1100 KB |
Output is correct |
17 |
Correct |
1 ms |
1100 KB |
Output is correct |
18 |
Correct |
1 ms |
1100 KB |
Output is correct |
19 |
Correct |
1 ms |
1100 KB |
Output is correct |
20 |
Correct |
1 ms |
1100 KB |
Output is correct |
21 |
Correct |
1 ms |
1100 KB |
Output is correct |
22 |
Correct |
1 ms |
1100 KB |
Output is correct |
23 |
Correct |
1 ms |
1100 KB |
Output is correct |
24 |
Correct |
1 ms |
1100 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1100 KB |
Output is correct |
2 |
Correct |
1 ms |
1100 KB |
Output is correct |
3 |
Correct |
1 ms |
1096 KB |
Output is correct |
4 |
Correct |
1 ms |
1100 KB |
Output is correct |
5 |
Correct |
1 ms |
1100 KB |
Output is correct |
6 |
Correct |
1 ms |
1100 KB |
Output is correct |
7 |
Correct |
1 ms |
1100 KB |
Output is correct |
8 |
Correct |
1 ms |
1076 KB |
Output is correct |
9 |
Correct |
1 ms |
1100 KB |
Output is correct |
10 |
Correct |
1 ms |
1100 KB |
Output is correct |
11 |
Correct |
1 ms |
1092 KB |
Output is correct |
12 |
Correct |
1 ms |
1100 KB |
Output is correct |
13 |
Correct |
2 ms |
1092 KB |
Output is correct |
14 |
Correct |
1 ms |
1100 KB |
Output is correct |
15 |
Correct |
1 ms |
1100 KB |
Output is correct |
16 |
Correct |
1 ms |
1100 KB |
Output is correct |
17 |
Correct |
1 ms |
1100 KB |
Output is correct |
18 |
Correct |
1 ms |
1100 KB |
Output is correct |
19 |
Correct |
1 ms |
1100 KB |
Output is correct |
20 |
Correct |
1 ms |
1100 KB |
Output is correct |
21 |
Correct |
1 ms |
1100 KB |
Output is correct |
22 |
Correct |
1 ms |
1100 KB |
Output is correct |
23 |
Correct |
1 ms |
1100 KB |
Output is correct |
24 |
Correct |
1 ms |
1100 KB |
Output is correct |
25 |
Correct |
134 ms |
5704 KB |
Output is correct |
26 |
Correct |
26 ms |
1740 KB |
Output is correct |
27 |
Correct |
301 ms |
7528 KB |
Output is correct |
28 |
Correct |
318 ms |
7620 KB |
Output is correct |
29 |
Correct |
143 ms |
5828 KB |
Output is correct |
30 |
Correct |
212 ms |
5616 KB |
Output is correct |
31 |
Correct |
111 ms |
6060 KB |
Output is correct |
32 |
Correct |
325 ms |
7552 KB |
Output is correct |
33 |
Correct |
256 ms |
7364 KB |
Output is correct |
34 |
Correct |
155 ms |
4428 KB |
Output is correct |
35 |
Correct |
251 ms |
7748 KB |
Output is correct |
36 |
Correct |
318 ms |
7584 KB |
Output is correct |
37 |
Correct |
174 ms |
4360 KB |
Output is correct |
38 |
Correct |
318 ms |
6796 KB |
Output is correct |
39 |
Correct |
44 ms |
2116 KB |
Output is correct |
40 |
Correct |
306 ms |
6700 KB |
Output is correct |
41 |
Correct |
228 ms |
5428 KB |
Output is correct |
42 |
Correct |
321 ms |
6692 KB |
Output is correct |
43 |
Correct |
104 ms |
3056 KB |
Output is correct |
44 |
Correct |
315 ms |
6652 KB |
Output is correct |
45 |
Correct |
53 ms |
2348 KB |
Output is correct |
46 |
Correct |
314 ms |
6668 KB |
Output is correct |
47 |
Correct |
79 ms |
2636 KB |
Output is correct |
48 |
Correct |
311 ms |
6700 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Execution timed out |
3080 ms |
16620 KB |
Time limit exceeded |
2 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
1100 KB |
Output is correct |
2 |
Correct |
1 ms |
1100 KB |
Output is correct |
3 |
Correct |
1 ms |
1096 KB |
Output is correct |
4 |
Correct |
1 ms |
1100 KB |
Output is correct |
5 |
Correct |
1 ms |
1100 KB |
Output is correct |
6 |
Correct |
1 ms |
1100 KB |
Output is correct |
7 |
Correct |
1 ms |
1100 KB |
Output is correct |
8 |
Correct |
1 ms |
1076 KB |
Output is correct |
9 |
Correct |
1 ms |
1100 KB |
Output is correct |
10 |
Correct |
1 ms |
1100 KB |
Output is correct |
11 |
Correct |
1 ms |
1092 KB |
Output is correct |
12 |
Correct |
1 ms |
1100 KB |
Output is correct |
13 |
Correct |
2 ms |
1092 KB |
Output is correct |
14 |
Correct |
1 ms |
1100 KB |
Output is correct |
15 |
Correct |
1 ms |
1100 KB |
Output is correct |
16 |
Correct |
1 ms |
1100 KB |
Output is correct |
17 |
Correct |
1 ms |
1100 KB |
Output is correct |
18 |
Correct |
1 ms |
1100 KB |
Output is correct |
19 |
Correct |
1 ms |
1100 KB |
Output is correct |
20 |
Correct |
1 ms |
1100 KB |
Output is correct |
21 |
Correct |
1 ms |
1100 KB |
Output is correct |
22 |
Correct |
1 ms |
1100 KB |
Output is correct |
23 |
Correct |
1 ms |
1100 KB |
Output is correct |
24 |
Correct |
1 ms |
1100 KB |
Output is correct |
25 |
Correct |
134 ms |
5704 KB |
Output is correct |
26 |
Correct |
26 ms |
1740 KB |
Output is correct |
27 |
Correct |
301 ms |
7528 KB |
Output is correct |
28 |
Correct |
318 ms |
7620 KB |
Output is correct |
29 |
Correct |
143 ms |
5828 KB |
Output is correct |
30 |
Correct |
212 ms |
5616 KB |
Output is correct |
31 |
Correct |
111 ms |
6060 KB |
Output is correct |
32 |
Correct |
325 ms |
7552 KB |
Output is correct |
33 |
Correct |
256 ms |
7364 KB |
Output is correct |
34 |
Correct |
155 ms |
4428 KB |
Output is correct |
35 |
Correct |
251 ms |
7748 KB |
Output is correct |
36 |
Correct |
318 ms |
7584 KB |
Output is correct |
37 |
Correct |
174 ms |
4360 KB |
Output is correct |
38 |
Correct |
318 ms |
6796 KB |
Output is correct |
39 |
Correct |
44 ms |
2116 KB |
Output is correct |
40 |
Correct |
306 ms |
6700 KB |
Output is correct |
41 |
Correct |
228 ms |
5428 KB |
Output is correct |
42 |
Correct |
321 ms |
6692 KB |
Output is correct |
43 |
Correct |
104 ms |
3056 KB |
Output is correct |
44 |
Correct |
315 ms |
6652 KB |
Output is correct |
45 |
Correct |
53 ms |
2348 KB |
Output is correct |
46 |
Correct |
314 ms |
6668 KB |
Output is correct |
47 |
Correct |
79 ms |
2636 KB |
Output is correct |
48 |
Correct |
311 ms |
6700 KB |
Output is correct |
49 |
Execution timed out |
3080 ms |
16620 KB |
Time limit exceeded |
50 |
Halted |
0 ms |
0 KB |
- |