#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define pf push_front
#define mp make_pair
#define fi first
#define se second
#define int long long
#define all(x) (x).begin(), (x).end()
typedef long double ld;
typedef long long ll;
typedef pair<ll,ll> pll;
typedef pair<int,int> pii;
typedef vector<int> vi;
typedef vector<ll> vll;
typedef vector<bool> vb;
typedef vector<vector<int>> vvi;
typedef vector<vector<bool>> vvb;
typedef vector<vector<ll>> vvll;
typedef vector<string> vs;
typedef vector<vector<string>> vvs;
typedef vector<char> vc;
typedef vector<vector<char>> vvc;
typedef map<int, int> mii;
typedef unordered_map<int, int> umii;
const int mod = 1e9 + 7;
const int inf = INTMAX_MAX;
const bool tc = false;
int n, d; int cnt;
const int N = 3e5 + 5;
const int defval = 1e18;
int seg[4 * N]; int lazy[4 * N];
int a[N];
void push(int l, int r, int pos) {
seg[pos] += lazy[pos];
if (l != r) {
lazy[pos * 2] += lazy[pos];
lazy[pos * 2 + 1] += lazy[pos];
}
lazy[pos] = 0;
}
int segfunction(int x, int y) {
return min(x, y);
}
int query_util(int l, int r, int pos, int ql, int qr) {
if (l > r) return defval;
push(l, r, pos);
if (ql > r || l > qr) {
return defval;
}
if (ql <= l && r <= qr) {
return seg[pos];
}
int mid = (l + r) / 2;
return segfunction(query_util(l, mid, pos * 2, ql, qr), query_util(mid + 1, r, pos * 2 + 1, ql, qr));
}
void build_util(int l, int r, int pos) {
if (l == r) {
seg[pos] = a[l];
return;
}
int mid = (l + r) / 2;
build_util(l, mid, pos * 2);
build_util(mid + 1, r, pos * 2 + 1);
seg[pos] = segfunction(seg[pos * 2], seg[pos * 2 + 1]);
}
void update_util(int l, int r, int pos, int ql, int qr, int qval) {
if (l > r) return;
push(l, r, pos);
if (qr < l || r < ql) return;
if (ql <= l && r <= qr) {
lazy[pos] += qval;
push(l, r, pos);
return;
}
int mid = (l + r) / 2;
update_util(l, mid, pos * 2, ql, qr, qval);
update_util(mid + 1, r, pos * 2 + 1, ql, qr, qval);
seg[pos] = segfunction(seg[pos * 2], seg[pos * 2 + 1]);
}
void build() {
build_util(0, cnt - 1, 1);
}
void update(int l, int r, int v) {
update_util(0, cnt - 1, 1, l, r, v);
}
int query(int l, int r) {
return query_util(0, cnt - 1, 1, l, r);
}
struct st {
int x, a, b;
bool operator<(st other) {
return b > other.b;
}
};
map<int, vector<st>> fuels;
inline void solve() {
cin >> n >> d; swap(n, d);
int ndn = 1; int nd0 = 1; vector<st> fv;
for (int i = 0; i < d; i++) {
int x, a, b;
cin >> x >> a >> b;
st s; s.x = x; s.a = a; s.b = b;
fuels[x].pb(s); fv.pb(s);
if (s.x == n) ndn = 0;
if (s.x == 0) nd0 = 0;
}
if (nd0) {
st s; s.x = 0;
s.a = 0;
s.b = 0;
fuels[s.x].pb(s);
fv.pb(s);
//cnt++;
}
if (ndn) {
st s; s.x = n;
s.a = 0;
s.b = 0;
fuels[s.x].pb(s);
fv.pb(s);
}
mii id; // id for each x
sort(all(fv));
vector<vector<st>> sv;
for (auto it = fuels.begin(); it != fuels.end(); it++) {
int x = it->first;
vector<st> s = it->second;
sv.pb(s);
id[s[0].x] = cnt;
cnt++;
//cout << "buruuu\n";
}
for (int i = 1; i < sv.size(); i++) {
a[i] = a[i - 1] - (sv[i][0].x - sv[i - 1][0].x);
}
build();
// n -> distance, d -> number of fuel stations
int ans = n;
for (int i = fv.size() - 1; i >= 0; i--) {
st f = fv[i];
update(0, cnt - 1, f.b);
//cout << "consider " << f.b << '\n';
int x = f.x; int j = id[x];
update(j + 1, cnt - 1, f.a);
//cout << j + 1 << " -> " << cnt - 1 << '\n';
int mn = query(0, cnt - 1);
//cout << "minimum was " << mn << '\n';
update(0, cnt - 1, -f.b);
if (mn < 0) continue;
int cand = f.b;
int spr = mn;
if (cand > spr) {
cand -= spr;
}
ans = min(ans, cand);
}
cout << ans << '\n';
}
void setIO(string s) {
freopen((s + ".in").c_str(), "r", stdin);
freopen((s + ".out").c_str(), "w", stdout);
}
signed main() {
ios::sync_with_stdio(false);
cout.tie(0);
cin.tie(0);
//setIO();
int t = 1;
if (tc) {
cin >> t;
}
while (t--) {
solve();
}
}
컴파일 시 표준 에러 (stderr) 메시지
FuelStation.cpp: In function 'void setIO(std::string)':
FuelStation.cpp:195:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
195 | freopen((s + ".in").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
FuelStation.cpp:196:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
196 | freopen((s + ".out").c_str(), "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... |