/**
* author: Haunted_Cpp
**/
#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC target("fma,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,tune=native")
#pragma GCC optimize("unroll-loops")
template<typename T> ostream &operator << (ostream &os, const vector<T> &v) { os << '{'; string sep; for (const auto &x : v) os << sep << x, sep = ", "; return os << '}'; }
template<typename T, size_t size> ostream &operator << (ostream &os, const array<T, size> &arr) { os << '{'; string sep; for (const auto &x : arr) os << sep << x, sep = ", "; return os << '}'; }
template<typename A, typename B> ostream &operator << (ostream &os, const pair<A, B> &p) { return os << '(' << p.first << ", " << p.second << ')'; }
void debug_out() { cerr << endl; }
template<typename Head, typename... Tail> void debug_out(Head H, Tail... T) { cerr << ' ' << H; debug_out(T...); }
#ifdef LOCAL
#define debug(...) cerr << "(" << #__VA_ARGS__ << "):", debug_out(__VA_ARGS__)
#else
#define debug(...) 47
#endif
class SegmentTree {
private:
struct Node {
long long prefix, sum, best;
Node () {
prefix = sum = best = 1e9;
}
void merge(Node l, Node r) {
prefix = min(l.prefix, l.sum + r.prefix);
sum = l.sum + r.sum;
best = min({l.best, prefix});
}
};
vector<Node> seg;
const int LO, HI;
void build(int l, int r, int node, const vector<int> &arr) {
if (l == r) {
seg[node].prefix = seg[node].sum = seg[node].best = arr[l];
return;
}
const int mid = l + (r - l) / 2;
build(l, mid, 2 * node + 1, arr);
build(mid + 1, r, 2 * node + 2, arr);
seg[node].merge(seg[2 * node + 1], seg[2 * node + 2]);
}
void modify(int where, int delta, int l, int r, int node) {
if (l > where || r < where) return;
if (l >= where && r <= where) {
seg[node].prefix = seg[node].sum = seg[node].best = delta;
return;
}
const int mid = l + (r - l) / 2;
modify(where, delta, l, mid, 2 * node + 1);
modify(where, delta, mid + 1, r, 2 * node + 2);
seg[node].merge(seg[2 * node + 1], seg[2 * node + 2]);
}
public:
SegmentTree(const vector<int> &arr) : LO(0), HI((int)arr.size() - 1) {
seg.clear();
seg.resize(4 * (int) arr.size());
build(LO, HI, 0, arr);
}
void modify(int where, int delta) {
modify(where, delta, LO, HI, 0);
}
long long worst_prefix() {
return seg[0].best;
}
};
int main () {
ios::sync_with_stdio(0);
cin.tie(0);
int n, d;
cin >> n >> d;
vector<tuple<int, int, int>> arr(n);
vector<pair<int, int>> top_up;
vector<int> distancias;
for (int i = 0; i < n; i++) {
int x, a, b;
cin >> x >> a >> b;
arr[i] = make_tuple(x, a, b);
distancias.emplace_back(b);
}
sort(arr.begin(), arr.end());
for (int i = 0; i < n; i++) top_up.emplace_back(get<2>(arr[i]), i);
sort(top_up.begin(), top_up.end());
vector<int> pre = {0};
vector<int> where_is_it(n);
long long s = 0;
for (int i = 0; i < n; i++) {
int dist = get<0>(arr[i]) - (i - 1 < 0 ? 0 : get<0>(arr[i - 1]));
s += dist;
distancias.emplace_back(s);
distancias.emplace_back(dist);
pre.emplace_back(-dist);
where_is_it[i] = pre.size();
pre.emplace_back(get<1>(arr[i]));
}
distancias.emplace_back(d - get<0>(arr.back()));
pre.emplace_back(-d + get<0>(arr.back()));
distancias.emplace_back(d);
sort(distancias.begin(), distancias.end());
distancias.erase(unique(distancias.begin(), distancias.end()), distancias.end());
SegmentTree seg(pre);
int rem = 0;
debug(distancias);
debug(pre);
for (auto cur : distancias) {
while(rem < n && get<0>(top_up[rem]) < cur) {
const int where = where_is_it[get<1>(top_up[rem])];
seg.modify(where, 0);
++rem;
}
seg.modify(0, cur);
if (seg.worst_prefix() >= 0) {
cout << cur << '\n';
return 0;
}
}
cout << "NO SOLUTION" << '\n';
return 0;
}
Compilation message
FuelStation.cpp: In function 'int main()':
FuelStation.cpp:112:20: warning: statement has no effect [-Wunused-value]
debug(distancias);
^
FuelStation.cpp:113:13: warning: statement has no effect [-Wunused-value]
debug(pre);
^
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Incorrect |
2 ms |
512 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
286 ms |
70096 KB |
Output is correct |
2 |
Correct |
272 ms |
69968 KB |
Output is correct |
3 |
Correct |
275 ms |
70096 KB |
Output is correct |
4 |
Correct |
264 ms |
69968 KB |
Output is correct |
5 |
Correct |
268 ms |
70120 KB |
Output is correct |
6 |
Correct |
298 ms |
70096 KB |
Output is correct |
7 |
Correct |
263 ms |
70096 KB |
Output is correct |
8 |
Correct |
265 ms |
70100 KB |
Output is correct |
9 |
Correct |
294 ms |
69968 KB |
Output is correct |
10 |
Correct |
285 ms |
70096 KB |
Output is correct |
11 |
Correct |
269 ms |
70220 KB |
Output is correct |
12 |
Correct |
289 ms |
70192 KB |
Output is correct |
13 |
Correct |
286 ms |
70092 KB |
Output is correct |
14 |
Correct |
274 ms |
70116 KB |
Output is correct |
15 |
Correct |
271 ms |
70044 KB |
Output is correct |
16 |
Correct |
278 ms |
70096 KB |
Output is correct |
17 |
Correct |
284 ms |
70096 KB |
Output is correct |
18 |
Correct |
272 ms |
70144 KB |
Output is correct |
19 |
Correct |
269 ms |
70100 KB |
Output is correct |
20 |
Correct |
278 ms |
70136 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
11 ms |
2944 KB |
Output is correct |
3 |
Correct |
8 ms |
2944 KB |
Output is correct |
4 |
Correct |
8 ms |
2944 KB |
Output is correct |
5 |
Correct |
9 ms |
2884 KB |
Output is correct |
6 |
Correct |
10 ms |
2944 KB |
Output is correct |
7 |
Correct |
10 ms |
2944 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
12 ms |
2944 KB |
Output is correct |
15 |
Correct |
14 ms |
2944 KB |
Output is correct |
16 |
Correct |
14 ms |
2944 KB |
Output is correct |
17 |
Correct |
12 ms |
2944 KB |
Output is correct |
18 |
Correct |
12 ms |
2944 KB |
Output is correct |
19 |
Correct |
9 ms |
2944 KB |
Output is correct |
20 |
Correct |
9 ms |
2944 KB |
Output is correct |
21 |
Correct |
8 ms |
2944 KB |
Output is correct |
22 |
Correct |
11 ms |
2944 KB |
Output is correct |
23 |
Correct |
9 ms |
2944 KB |
Output is correct |
24 |
Correct |
9 ms |
2944 KB |
Output is correct |
25 |
Correct |
8 ms |
2944 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
11 ms |
2944 KB |
Output is correct |
3 |
Correct |
8 ms |
2944 KB |
Output is correct |
4 |
Correct |
8 ms |
2944 KB |
Output is correct |
5 |
Correct |
9 ms |
2884 KB |
Output is correct |
6 |
Correct |
10 ms |
2944 KB |
Output is correct |
7 |
Correct |
10 ms |
2944 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
12 ms |
2944 KB |
Output is correct |
15 |
Correct |
14 ms |
2944 KB |
Output is correct |
16 |
Correct |
14 ms |
2944 KB |
Output is correct |
17 |
Correct |
12 ms |
2944 KB |
Output is correct |
18 |
Correct |
12 ms |
2944 KB |
Output is correct |
19 |
Correct |
9 ms |
2944 KB |
Output is correct |
20 |
Correct |
9 ms |
2944 KB |
Output is correct |
21 |
Correct |
8 ms |
2944 KB |
Output is correct |
22 |
Correct |
11 ms |
2944 KB |
Output is correct |
23 |
Correct |
9 ms |
2944 KB |
Output is correct |
24 |
Correct |
9 ms |
2944 KB |
Output is correct |
25 |
Correct |
8 ms |
2944 KB |
Output is correct |
26 |
Correct |
1 ms |
384 KB |
Output is correct |
27 |
Correct |
286 ms |
70028 KB |
Output is correct |
28 |
Correct |
274 ms |
70232 KB |
Output is correct |
29 |
Correct |
276 ms |
70380 KB |
Output is correct |
30 |
Correct |
274 ms |
70368 KB |
Output is correct |
31 |
Correct |
281 ms |
70316 KB |
Output is correct |
32 |
Correct |
278 ms |
70228 KB |
Output is correct |
33 |
Correct |
1 ms |
384 KB |
Output is correct |
34 |
Correct |
1 ms |
384 KB |
Output is correct |
35 |
Correct |
0 ms |
384 KB |
Output is correct |
36 |
Correct |
1 ms |
384 KB |
Output is correct |
37 |
Correct |
1 ms |
384 KB |
Output is correct |
38 |
Correct |
1 ms |
384 KB |
Output is correct |
39 |
Correct |
521 ms |
70324 KB |
Output is correct |
40 |
Correct |
481 ms |
70220 KB |
Output is correct |
41 |
Correct |
485 ms |
70224 KB |
Output is correct |
42 |
Correct |
460 ms |
70484 KB |
Output is correct |
43 |
Correct |
464 ms |
70348 KB |
Output is correct |
44 |
Correct |
324 ms |
70224 KB |
Output is correct |
45 |
Correct |
320 ms |
70224 KB |
Output is correct |
46 |
Correct |
276 ms |
74164 KB |
Output is correct |
47 |
Correct |
260 ms |
75604 KB |
Output is correct |
48 |
Correct |
247 ms |
75524 KB |
Output is correct |
49 |
Correct |
255 ms |
73432 KB |
Output is correct |
50 |
Correct |
267 ms |
73828 KB |
Output is correct |
51 |
Correct |
261 ms |
73812 KB |
Output is correct |
52 |
Correct |
269 ms |
73572 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Incorrect |
2 ms |
512 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Incorrect |
2 ms |
512 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
0 ms |
384 KB |
Output is correct |
3 |
Incorrect |
2 ms |
512 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |