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;
const int MAX_N = 1e5 + 10;
long long x[MAX_N], g[MAX_N], d[MAX_N];
int tree[4 * MAX_N];
void build(int idx, int l, int r) {
if(l == r) {
return void(tree[idx] = l);
}
int mid = (l + r) / 2;
build(idx * 2, l, mid);
build(idx * 2 + 1, mid + 1, r);
int L = tree[idx * 2], R = tree[idx * 2 + 1];
if(x[L] - d[L - 1] >= x[R] - x[R - 1]) {
tree[idx] = L;
}
else {
tree[idx] = R;
}
}
int query(int idx, int l, int r, int ql, int qr) {
if(r < ql or qr < l) {
return -1;
}
if(ql <= l and r <= qr) {
return tree[idx];
}
int mid = (l + r) / 2;
int L = query(idx * 2, l, mid, ql, qr);
int R = query(idx * 2 + 1, mid + 1, r, ql, qr);
if(L == -1) {
return R;
}
if(R == -1) {
return L;
}
if(x[L] - d[L - 1] >= x[R] - d[R - 1]) {
return L;
}
return R;
}
int main() {
cin.tie(nullptr)->sync_with_stdio(false);
int N;
cin >> N;
for(int i = 1; i <= N; i++) {
cin >> x[i] >> g[i] >> d[i];
g[i] += g[i - 1];
d[i] += d[i - 1];
}
build(1, 1, N);
long long ans = 0;
for(int i = 1; i <= N; i++) {
int l = 1, r = i - 1, pos = -1;
while(l <= r) {
int mid = (l + r) / 2;
int j = query(1, 1, N, l, mid);
if(x[i] - x[j] <= d[i] - d[j - 1]) {
r = mid - 1;
pos = mid;
}
else {
l = mid + 1;
}
}
ans = max(ans, g[i] - g[i - 1]);
if(pos != -1 and x[i] - x[pos] <= d[i] - d[pos - 1]) {
ans = max(ans, g[i] - g[pos - 1]);
}
}
cout << ans;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |