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;
vector <pair <long long, long long>> vec;
struct node
{
long long s, e, m;
long long mn, lz;
node *l, *r;
node (long long _s, long long _e) : s(_s), e(_e), m((_s+_e)/2), lz(0)
{
if (s==e)
{
mn-= vec[s].first;
return;
}
l=new node(s, m);
r=new node(m+1, e);
mn=min(l->mn, r->mn);
}
void prop()
{
l->update(s, m, lz);
r->update(m+1, e, lz);
lz = 0;
mn=min(l->mn, r->mn);
}
void update(long long x, long long y, long long v)
{
if (x > y) return;
if (x<=s && y>=e)
{
mn+=v; lz+=v; return;
}
if (lz!=0) prop();
if (x<=m) l->update(x, y, v);
if (y>=m+1) r->update(x, y, v);
mn=min(l->mn, r->mn);
}
};
int32_t main()
{
ios_base::sync_with_stdio(false);
cin.tie(0); cout.tie(0);
// freopen("FFUEL.INP", "r", stdin);
// freopen("FFUEL.OUT", "w", stdout);
long long n, d, x, a, b, ans;
vector <tuple <long long, long long, long long>> vec2;
cin>>n>>d;
ans=d;
for (long long i=0; i<n; i++)
{
cin>>x>>a>>b;
vec.emplace_back(x, i);
vec2.emplace_back(b, a, x);
}
vec.emplace_back(d, n);
vec2.emplace_back(2000000000, 0, d);
sort(vec.begin(), vec.end());
long long cnt = n+1;
for (long long i=n; i>=0; i--)
{
if (i!=n && vec[i].first != vec[i+1].first) cnt = i+1;
get<2>(vec2[vec[i].second]) = cnt;
}
sort(vec2.begin(), vec2.end());
node *root = new node(0, n);
for (long long i = 0; i<n; i++)
{
root->update(get<2>(vec2[i]), n, get<1>(vec2[i]));
}
long long left = 0;
for (long long i = 0; i<=n; i++)
{
root->update(0, n, get<0>(vec2[i]) - (i==0 ? 0 : get<0>(vec2[i-1])));
if (i<n-1 && get<0>(vec2[i])==get<0>(vec2[i+1])) continue;
while (get<0>(vec2[left]) != get<0>(vec2[i]))
{
root->update(get<2>(vec2[left]), n, -get<1>(vec2[left]));
left++;
}
if (root->mn < 0) continue;
ans=min(ans, get<0>(vec2[i]) - root->mn);
}
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... |
# | 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... |