#include<bits/stdc++.h>
#define ii pair<int, int>
#define ll pair<long long, long long>
#define fi first
#define se second
#define pb push_back
using namespace std;
const int mod[2] = {1000000007, 998244353};
const int N = 3e5 + 2;
const string NAME = "ffuel";
int n, D;
vector<int> vct;
struct SegTree{
int n, h;
vector<long long> tree, lazy;
SegTree(int _n) : n(_n){
tree.resize(2 * n, 0);
lazy.resize(n, 0);
h = sizeof(int) * 8 - __builtin_clz(n);
}
void build(){
for(int i = 0; i < 2 * n; ++i)
tree[i] = 0;
for(int i = 0; i < n; ++i)
lazy[i] = 0;
}
void up(int u){
while(u > 1)
u >>= 1, tree[u] = max(tree[u << 1], tree[u << 1 | 1]) + lazy[u];
}
void apply(int u, long long val){
tree[u] += val;
if(u < n)
lazy[u] += val;
}
void down(int u){
for(int s = h; s; --s){
int i = u >> s;
if(lazy[i]){
apply(i << 1, lazy[i]);
apply(i << 1 | 1, lazy[i]);
lazy[i] = 0;
}
}
}
void update(int l, int r, long long val){
l += n, r += n;
int l0 = l, r0 = r;
for(; l < r; l >>= 1, r >>= 1){
if(l & 1)
apply(l++, val);
if(r & 1)
apply(--r, val);
}
up(l0);
up(r0 - 1);
}
long long query(int l, int r){
l += n, r += n;
down(l);
down(r - 1);
long long res = -2e16;
for(; l < r; l >>= 1, r >>= 1){
if(l & 1)
res = max(res, tree[l++]);
if(r & 1)
res = max(tree[--r], res);
}
return res;
}
};
struct pnt{
int x, a, b;
bool operator < (const pnt &other) const{
if(b != other.b)
return b < other.b;
return x < other.x;
}
} a[N];
void inp(){
cin >> n >> D;
vct.pb(D);
for(int i = 1; i <= n; ++i)
cin >> a[i].x >> a[i].a >> a[i].b, vct.pb(a[i].x);
}
void solve(){
sort(a + 1, a + n + 1);
sort(vct.begin(), vct.end());
vct.resize(unique(vct.begin(), vct.end()) - vct.begin());
int m = vct.size();
SegTree IT(m);
for(int i = 0; i < m; ++i)
IT.update(i, i + 1, vct[i]);
for(int i = 1; i <= n; ++i)
a[i].x = lower_bound(vct.begin(), vct.end(), a[i].x) - vct.begin();
long long ans = D;
for(int i = 1; i <= n; ++i){
IT.update(a[i].x + 1, m, -a[i].a);
long long val = max(0LL, IT.query(0, m));
if(val <= a[i].b)
ans = min(ans, val);
}
cout << ans;
}
int main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
if(fopen((NAME + ".inp").c_str(), "r")){
freopen((NAME + ".inp").c_str(), "r", stdin);
freopen((NAME + ".out").c_str(), "w", stdout);
}
inp();
solve();
}
Compilation message
FuelStation.cpp: In function 'int main()':
FuelStation.cpp:139:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
139 | freopen((NAME + ".inp").c_str(), "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
FuelStation.cpp:140:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
140 | freopen((NAME + ".out").c_str(), "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
508 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
560 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
153 ms |
11256 KB |
Output is correct |
2 |
Correct |
157 ms |
10868 KB |
Output is correct |
3 |
Correct |
145 ms |
10908 KB |
Output is correct |
4 |
Correct |
123 ms |
10692 KB |
Output is correct |
5 |
Correct |
131 ms |
10692 KB |
Output is correct |
6 |
Correct |
152 ms |
12740 KB |
Output is correct |
7 |
Correct |
146 ms |
12740 KB |
Output is correct |
8 |
Correct |
125 ms |
13256 KB |
Output is correct |
9 |
Correct |
144 ms |
6624 KB |
Output is correct |
10 |
Correct |
123 ms |
10692 KB |
Output is correct |
11 |
Correct |
144 ms |
10604 KB |
Output is correct |
12 |
Correct |
122 ms |
10624 KB |
Output is correct |
13 |
Correct |
148 ms |
12740 KB |
Output is correct |
14 |
Correct |
147 ms |
10948 KB |
Output is correct |
15 |
Correct |
141 ms |
6680 KB |
Output is correct |
16 |
Correct |
156 ms |
11460 KB |
Output is correct |
17 |
Correct |
124 ms |
6596 KB |
Output is correct |
18 |
Correct |
144 ms |
13256 KB |
Output is correct |
19 |
Correct |
116 ms |
6596 KB |
Output is correct |
20 |
Correct |
152 ms |
6668 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
5 ms |
592 KB |
Output is correct |
3 |
Correct |
7 ms |
848 KB |
Output is correct |
4 |
Correct |
7 ms |
848 KB |
Output is correct |
5 |
Correct |
7 ms |
848 KB |
Output is correct |
6 |
Correct |
7 ms |
848 KB |
Output is correct |
7 |
Correct |
7 ms |
848 KB |
Output is correct |
8 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
5 ms |
592 KB |
Output is correct |
3 |
Correct |
7 ms |
848 KB |
Output is correct |
4 |
Correct |
7 ms |
848 KB |
Output is correct |
5 |
Correct |
7 ms |
848 KB |
Output is correct |
6 |
Correct |
7 ms |
848 KB |
Output is correct |
7 |
Correct |
7 ms |
848 KB |
Output is correct |
8 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
9 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
508 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
560 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
1 ms |
336 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
336 KB |
Output is correct |
20 |
Correct |
1 ms |
592 KB |
Output is correct |
21 |
Correct |
1 ms |
336 KB |
Output is correct |
22 |
Incorrect |
1 ms |
508 KB |
Output isn't correct |
23 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
508 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
560 KB |
Output is correct |
16 |
Correct |
1 ms |
336 KB |
Output is correct |
17 |
Correct |
5 ms |
592 KB |
Output is correct |
18 |
Correct |
7 ms |
848 KB |
Output is correct |
19 |
Correct |
7 ms |
848 KB |
Output is correct |
20 |
Correct |
7 ms |
848 KB |
Output is correct |
21 |
Correct |
7 ms |
848 KB |
Output is correct |
22 |
Correct |
7 ms |
848 KB |
Output is correct |
23 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
24 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
1 ms |
336 KB |
Output is correct |
3 |
Correct |
1 ms |
336 KB |
Output is correct |
4 |
Correct |
1 ms |
336 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
1 ms |
336 KB |
Output is correct |
7 |
Correct |
1 ms |
336 KB |
Output is correct |
8 |
Correct |
1 ms |
336 KB |
Output is correct |
9 |
Correct |
1 ms |
336 KB |
Output is correct |
10 |
Correct |
1 ms |
336 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
336 KB |
Output is correct |
13 |
Correct |
1 ms |
508 KB |
Output is correct |
14 |
Correct |
1 ms |
336 KB |
Output is correct |
15 |
Correct |
1 ms |
560 KB |
Output is correct |
16 |
Correct |
153 ms |
11256 KB |
Output is correct |
17 |
Correct |
157 ms |
10868 KB |
Output is correct |
18 |
Correct |
145 ms |
10908 KB |
Output is correct |
19 |
Correct |
123 ms |
10692 KB |
Output is correct |
20 |
Correct |
131 ms |
10692 KB |
Output is correct |
21 |
Correct |
152 ms |
12740 KB |
Output is correct |
22 |
Correct |
146 ms |
12740 KB |
Output is correct |
23 |
Correct |
125 ms |
13256 KB |
Output is correct |
24 |
Correct |
144 ms |
6624 KB |
Output is correct |
25 |
Correct |
123 ms |
10692 KB |
Output is correct |
26 |
Correct |
144 ms |
10604 KB |
Output is correct |
27 |
Correct |
122 ms |
10624 KB |
Output is correct |
28 |
Correct |
148 ms |
12740 KB |
Output is correct |
29 |
Correct |
147 ms |
10948 KB |
Output is correct |
30 |
Correct |
141 ms |
6680 KB |
Output is correct |
31 |
Correct |
156 ms |
11460 KB |
Output is correct |
32 |
Correct |
124 ms |
6596 KB |
Output is correct |
33 |
Correct |
144 ms |
13256 KB |
Output is correct |
34 |
Correct |
116 ms |
6596 KB |
Output is correct |
35 |
Correct |
152 ms |
6668 KB |
Output is correct |
36 |
Correct |
1 ms |
336 KB |
Output is correct |
37 |
Correct |
5 ms |
592 KB |
Output is correct |
38 |
Correct |
7 ms |
848 KB |
Output is correct |
39 |
Correct |
7 ms |
848 KB |
Output is correct |
40 |
Correct |
7 ms |
848 KB |
Output is correct |
41 |
Correct |
7 ms |
848 KB |
Output is correct |
42 |
Correct |
7 ms |
848 KB |
Output is correct |
43 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
44 |
Halted |
0 ms |
0 KB |
- |