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>
#define ll long long
#define fi first
#define se second
#define sz(x) (int)x.size()
#define ALL(v) v.begin(),v.end()
#define MASK(k) (1LL << (k))
#define BIT(x, i) (((x) >> (i)) & 1)
#define oo (ll)1e18
#define INF (ll)1e9
#define MOD (ll)(998244353)
using namespace std;
template<class T1, class T2>
bool maximize(T1 &a, T2 b){if(a < b){a = b; return true;} return false;}
template<class T1, class T2>
bool minimize(T1 &a, T2 b){if(a > b){a = b; return true;} return false;}
template<class T1, class T2>
void add(T1 &a, T2 b){a += b; if(a >= MOD) a -= MOD;}
template<class T1, class T2>
void sub(T1 &a, T2 b){a -= b; if(a < 0) a += MOD;}
template<class T>
void cps(T &v){sort(ALL(v)); v.resize(unique(ALL(v)) - v.begin());}
const int MAX = 3e5 + 10;
int N, D;
tuple<int, int, int> a[MAX];
ll Min[MAX * 4], laz[MAX * 4];
void push(int i){
Min[i * 2] += laz[i];
Min[i * 2 + 1] += laz[i];
laz[i * 2] += laz[i];
laz[i * 2 + 1] += laz[i];
laz[i] = 0;
}
void update(int i, int l, int r, int u, int v, ll c){
if(r < u || v < l) return;
if(u <= l && r <= v){
Min[i] += c;
laz[i] += c;
return;
}
push(i);
int m = (l + r) >> 1;
update(i * 2, l, m, u, v, c);
update(i * 2 + 1, m + 1, r, u, v, c);
Min[i] = min(Min[i * 2], Min[i * 2 + 1]);
}
void solve(){
cin >> N >> D;
for(int i=1; i<=N; i++){
cin >> get<0>(a[i]) >> get<1>(a[i]) >> get<2>(a[i]);
}
sort(a + 1, a + N + 1, [&](tuple<int, int, int> a, tuple<int, int, int> b){
return get<2>(a) > get<2>(b);
});
a[N + 1] = make_tuple(D, 0, 0);
vector<int> v;
for(int i=1; i<=N; i++){
v.push_back(get<0>(a[i]));
}
v.push_back(0);
v.push_back(D);
cps(v);
for(int i=0; i<sz(v); i++){
update(1, 1, sz(v), i + 1, i + 1, -v[i]);
}
ll ans = D;
for(int i=1; i<=N; i++){
int x = lower_bound(ALL(v), get<0>(a[i])) - v.begin() + 1;
update(1, 1, sz(v), x + 1, sz(v), get<1>(a[i]));
if(abs(Min[1]) <= get<2>(a[i])){
minimize(ans, abs(Min[1]));
}
}
cout << ans << '\n';
}
int main(){
ios_base::sync_with_stdio(0); cin.tie(0);
// freopen("split.inp", "r", stdin);
// freopen("split.out", "w", stdout);
int tt = 1;
// cin >> tt;
while(tt--){
solve();
}
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... |
# | 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... |