#include <bits/stdc++.h>
#define se second
#define fs first
#define mp make_pair
#define pb push_back
#define ll long long
#define ii pair<ll,ll>
#define ld long double
#define SZ(v) (int)v.size()
#define ALL(v) v.begin(), v.end()
#define bit(msk, i) ((msk >> i) & 1)
#define iter(id, v) for(auto id : v)
#define rep(i,m,n) for(int i=(m); i<=(n); i++)
#define reb(i,m,n) for(int i=(m); i>=(n); i--)
using namespace std;
mt19937_64 rd(chrono :: steady_clock :: now().time_since_epoch().count());
ll Rand(ll l, ll r) { return uniform_int_distribution<ll> (l, r)(rd); }
const int N =3e5 + 7;
const int Mod = 1e9 + 7;
const int szBL = 240;
const int INF = 2e9;
const int BASE = 137;
struct Segment_Tree {
struct Node {
ll sum, pre;
};
int m;
Node st[N << 2];
void init (int n) {
m = n;
}
Node mer (Node lf, Node rt) {
Node cur;
cur.pre = min(lf.pre, lf.sum + rt.pre);
cur.sum = lf.sum + rt.sum;
return cur;
}
void update (int id, int l, int r, int pos, ll val) {
if (l > pos || r < pos) return;
if (l == r) {
st[id].sum += val;
st[id].pre = min (0LL, st[id].sum);
return;
}
int mid = l + r >> 1;
update (id << 1, l, mid, pos, val);
update (id << 1 | 1, mid + 1, r, pos, val);
st[id] = mer(st[id << 1], st[id << 1 | 1]);
// cout << l <<" "<<r<<" "<<st[id].mn <<" "<<pos<<" "<<val<<"\n";
}
void update (int pos, ll val) {
update (1, 1, m, pos, val);
}
Node get (int id, int l, int r, int u, int v) {
if (l > v || r < u) return {0, 0};
if (l >= u && r <= v) {
return st[id];
}
int mid = l + r >> 1;
// cout << l <<" "<<r<<" "<<mer(get (id << 1, l, mid, u, v), get (id << 1 | 1, mid + 1, r, u, v)).pre <<"\n";
return mer(get (id << 1, l, mid, u, v), get (id << 1 | 1, mid + 1, r, u, v));
// st[id] = mer(st[id << 1], st[id << 1 | 1]);
// cout << l <<" "<<r<<" "<<st[id].mn <<" "<<pos<<" "<<val<<"\n";
}
}ST;
struct Data {
int X, a, b, id;
};
struct Data2 {
int X, a, id;
};
int n, m, D;
Data a[N];
vector<Data2> events[N];
void solution () {
cin >> n >> D;
rep (i, 1, n) {
cin >> a[i].X >> a[i].a >> a[i].b;
}
sort (a + 1, a + 1 + n, [] (Data A, Data B) { return A.X < B.X; });
rep (i, 1, n) a[i].id = i;
vector<int> vals = {0};
rep (i, 1, n) {
vals.push_back(a[i].b);
}
sort (ALL(vals));
vals.resize(m = unique(ALL(vals)) - vals.begin());
ST.init(n + 1);
ST.update(n + 1, -D);
rep (i, 1, n) {
a[i].b = lower_bound(ALL(vals), a[i].b) - vals.begin();
events[a[i].b].push_back({a[i].X, a[i].a, a[i].id});
ST.update (a[i].id, -a[i].X);
ST.update (a[i].id + 1, a[i].X);
ST.update (a[i].id + 1, a[i].a);
// cout << a[i].id<<" "<<-a[i].X<<"\n";
// cout << a[i].id + 1<<" "<<a[i].X<<"\n";
// cout << a[i].id + 1<<" "<<a[i].a<<"\n";
}
ll res = INF;
rep (i, 1, m - 1) {
res = min(res, max(vals[i] + 1, -ST.st[1].pre));
iter (&id, events[i]) {
ST.update(id.id, id.X);
ST.update(id.id + 1, -id.X);
ST.update(id.id + 1, -id.a);
}
}
cout << res <<"\n";
}
#define file(name) freopen(name".inp", "r", stdin); \
freopen(name".out", "w", stdout);
/*
*/
int main () {
// file("c");
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
ll num_Test = 1;
// cin >> num_Test;
while(num_Test--)
solution();
}
/*
no bug challenge +2
more insight about dp0/1
*/
Compilation message
FuelStation.cpp: In member function 'void Segment_Tree::update(int, int, int, int, long long int)':
FuelStation.cpp:51:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
51 | int mid = l + r >> 1;
| ~~^~~
FuelStation.cpp: In member function 'Segment_Tree::Node Segment_Tree::get(int, int, int, int, int)':
FuelStation.cpp:65:21: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
65 | int mid = l + r >> 1;
| ~~^~~
FuelStation.cpp: In function 'void solution()':
FuelStation.cpp:114:54: error: no matching function for call to 'max(__gnu_cxx::__alloc_traits<std::allocator<int>, int>::value_type, long long int)'
114 | res = min(res, max(vals[i] + 1, -ST.st[1].pre));
| ^
In file included from /usr/include/c++/10/bits/specfun.h:45,
from /usr/include/c++/10/cmath:1927,
from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:41,
from FuelStation.cpp:1:
/usr/include/c++/10/bits/stl_algobase.h:254:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
254 | max(const _Tp& __a, const _Tp& __b)
| ^~~
/usr/include/c++/10/bits/stl_algobase.h:254:5: note: template argument deduction/substitution failed:
FuelStation.cpp:114:54: note: deduced conflicting types for parameter 'const _Tp' ('int' and 'long long int')
114 | res = min(res, max(vals[i] + 1, -ST.st[1].pre));
| ^
In file included from /usr/include/c++/10/bits/specfun.h:45,
from /usr/include/c++/10/cmath:1927,
from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:41,
from FuelStation.cpp:1:
/usr/include/c++/10/bits/stl_algobase.h:300:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
300 | max(const _Tp& __a, const _Tp& __b, _Compare __comp)
| ^~~
/usr/include/c++/10/bits/stl_algobase.h:300:5: note: template argument deduction/substitution failed:
FuelStation.cpp:114:54: note: deduced conflicting types for parameter 'const _Tp' ('int' and 'long long int')
114 | res = min(res, max(vals[i] + 1, -ST.st[1].pre));
| ^
In file included from /usr/include/c++/10/algorithm:62,
from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:65,
from FuelStation.cpp:1:
/usr/include/c++/10/bits/stl_algo.h:3480:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(std::initializer_list<_Tp>)'
3480 | max(initializer_list<_Tp> __l)
| ^~~
/usr/include/c++/10/bits/stl_algo.h:3480:5: note: template argument deduction/substitution failed:
FuelStation.cpp:114:54: note: mismatched types 'std::initializer_list<_Tp>' and 'int'
114 | res = min(res, max(vals[i] + 1, -ST.st[1].pre));
| ^
In file included from /usr/include/c++/10/algorithm:62,
from /usr/include/x86_64-linux-gnu/c++/10/bits/stdc++.h:65,
from FuelStation.cpp:1:
/usr/include/c++/10/bits/stl_algo.h:3486:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(std::initializer_list<_Tp>, _Compare)'
3486 | max(initializer_list<_Tp> __l, _Compare __comp)
| ^~~
/usr/include/c++/10/bits/stl_algo.h:3486:5: note: template argument deduction/substitution failed:
FuelStation.cpp:114:54: note: mismatched types 'std::initializer_list<_Tp>' and 'int'
114 | res = min(res, max(vals[i] + 1, -ST.st[1].pre));
| ^