// Author: 4uckd3v - Nguyen Cao Duc
#include <bits/stdc++.h>
using namespace std;
#define all(x) (x).begin(), (x).end()
typedef long long ll;
const int MAX_N = 3e5 + 5;
const int MOD = 1e9 + 7;
struct Station {
int x, a, b;
Station() {};
Station(int x, int a, int b) : x(x), a(a), b(b) {};
bool operator<(const Station &other) const {
return b > other.b;
};
};
int n, D;
int seg[4 * MAX_N + 5], lazy[4 * MAX_N + 5];
bool added[MAX_N + 5];
Station a[MAX_N + 5];
void down(int node) {
if (lazy[node] == 0) return;
for (int i = 0; i < 2; i++) {
seg[node << 1 | i] += lazy[node];
lazy[node << 1 | i] += lazy[node];
};
lazy[node] = 0;
};
void update(int node, int l, int r, int u, int v, int val) {
if (l > v || r < u) return;
if (l >= u && r <= v) {
seg[node] += val;
lazy[node] += val;
return;
};
int mid = (l + r) >> 1;
down(node);
update(node << 1, l, mid, u, v, val);
update(node << 1 | 1, mid + 1, r, u, v, val);
seg[node] = min(seg[node << 1], seg[node << 1 | 1]);
};
void update(int l, int r, int val) {
update(1, 1, n + 1, l, r, val);
};
int get(int node, int l, int r, int pos) {
if (l == r) return seg[node];
int mid = (l + r) >> 1;
down(node);
if (pos <= mid)
return get(node << 1, l, mid, pos);
else
return get(node << 1 | 1, mid + 1, r, pos);
};
int get(int node, int l, int r, int u, int v) {
if (l > v || r < u) return 2e9;
if (l >= u && r <= v) return seg[node];
int mid = (l + r) >> 1;
down(node);
return min(get(node << 1, l, mid, u, v), get(node << 1 | 1, mid + 1, r, u, v));
};
int get(int pos) { return get(1, 1, n + 1, pos); };
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
if (fopen("MAIN.INP", "r")) {
freopen("MAIN.INP", "r", stdin);
freopen("MAIN.OUT", "w", stdout);
};
cin >> n >> D;
vector<int> compress;
compress.reserve(n + 1);
for (int i = 1; i <= n; i++) {
cin >> a[i].x >> a[i].a >> a[i].b;
compress.push_back(a[i].x);
};
compress.push_back(D);
sort(all(compress));
compress.resize(unique(all(compress)) - compress.begin());
for (int i = 1; i <= n; i++) a[i].x = lower_bound(all(compress), a[i].x) - compress.begin() + 1;
sort(a + 1, a + n + 1);
int sz = compress.size();
for (int i = 1; i <= sz; i++) update(i, i, -compress[i - 1]);
// update(sz, sz, -D);
// cout << sz << endl;
int res = -D;
for (int i = 1; i <= n; i++) {
// cout << "before update debug:\n";
// for (int j = 1; j <= sz; j++) cout << get(j) << ' ';
// cout << endl;
// if (!added[a[i].x]) {
// update(a[i].x + 1, sz, compress[a[i].x - 1]);
// added[a[i].x] = true;
// };
update(a[i].x + 1, sz, a[i].a);
// cout << "Add:" << ' ' << a[i].x << ' ' << a[i].a << ' ' << get(1, 1, n + 1, 1, sz) << endl;
// cout << "after update debug:\n";
// for (int j = 1; j <= sz; j++) cout << get(j) << ' ';
// cout << endl;
if (-get(1, 1, n + 1, 1, sz) <= a[i].b)
res = max(res, get(1, 1, n + 1, 1, sz));
};
cout << -res << '\n';
};
Compilation message (stderr)
FuelStation.cpp: In function 'int main()':
FuelStation.cpp:87:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
87 | freopen("MAIN.INP", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
FuelStation.cpp:88:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
88 | freopen("MAIN.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... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |