#include <bits/stdc++.h>
using namespace std;
#define fast ios::sync_with_stdio(0);cin.tie(0);
#define s second
#define f first
typedef long long ll;
const ll MOD = 1e9+9;
const ll LOGN = 22;
const ll MAXN = 1e5 + 100;
vector<int> cc;
struct Machine {
int l, r, centr;
ll cost;
void read() {
cin >> l >> r >> centr >> cost;
cc.push_back(l);
cc.push_back(r);
cc.push_back(centr);
}
Machine(int _l, int _r, int _centr, ll _cost) : l(_l), r(_r), centr(_centr), cost(_cost) { }
Machine() { }
};
vector<Machine> v;
int n;
ll sg[4*MAXN], to_right[MAXN], to_left[MAXN];
void init(int k, int a, int b) {
if (a == b) {
sg[k] = 1e16;
return ;
}
init(2*k, a, (a+b)/2);
init(2*k+1, (a+b)/2+1, b);
sg[k] = min(sg[2*k], sg[2*k+1]);
}
void update(int k, int a, int b, int plc, ll val) {
if (b < plc || a > plc)
return ;
if (a == b) {
sg[k] = min(sg[k], val);
return ;
}
update(2*k, a, (a+b)/2, plc, val);
update(2*k+1, (a+b)/2+1, b, plc, val);
sg[k] = min(sg[2*k], sg[2*k+1]);
}
ll query(int k, int a, int b, int q_l, int q_r) {
if (q_l > b || q_r < a)
return 1e16;
if (q_l <= a && b <= q_r)
return sg[k];
return min(query(2*k, a, (a+b)/2, q_l, q_r),
query(2*k+1, (a+b)/2+1, b, q_l, q_r));
}
int index(int k) {
return lower_bound(cc.begin(), cc.end(), k) - cc.begin();
}
int main() {
int M, N;
cin >> M >> N;
v = vector<Machine>(M+1);
for (int i = 1; i <= M; i++)
v[i].read();
sort(cc.begin(), cc.end());
cc.erase(unique(cc.begin(), cc.end()), cc.end());
n = cc.size();
init(1, 1, n);
for (int i = 1; i <= M; i++) {
ll Q = query(1, 1, n, index(v[i].l), index(v[i].r));
if (v[i].l == 1)
Q = 0;
to_left[i] = Q + v[i].cost;
update(1, 1, n, index(v[i].centr), to_left[i]);
}
init(1, 1, n);
for (int i = 1; i <= M; i++) {
ll Q = query(1, 1, n, index(v[i].l), index(v[i].r));
if (v[i].r == N)
Q = 0;
to_right[i] = Q + v[i].cost;
update(1, 1, n, index(v[i].centr), to_right[i]);
}
ll mn = 1e16;
for (int i = 1; i <= M; i++)
mn = min(mn, to_left[i] + to_right[i] - v[i].cost);
if (mn >= 1e14)
mn = -1;
cout << mn << "\n";
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2484 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2484 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2392 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2484 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2392 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2496 KB |
Output is correct |
15 |
Correct |
1 ms |
2392 KB |
Output is correct |
16 |
Correct |
1 ms |
2392 KB |
Output is correct |
17 |
Correct |
3 ms |
2396 KB |
Output is correct |
18 |
Correct |
2 ms |
2396 KB |
Output is correct |
19 |
Correct |
3 ms |
2396 KB |
Output is correct |
20 |
Correct |
3 ms |
2584 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
3 ms |
2512 KB |
Output is correct |
23 |
Correct |
3 ms |
2396 KB |
Output is correct |
24 |
Correct |
3 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
0 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2484 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2392 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2496 KB |
Output is correct |
15 |
Correct |
1 ms |
2392 KB |
Output is correct |
16 |
Correct |
1 ms |
2392 KB |
Output is correct |
17 |
Correct |
3 ms |
2396 KB |
Output is correct |
18 |
Correct |
2 ms |
2396 KB |
Output is correct |
19 |
Correct |
3 ms |
2396 KB |
Output is correct |
20 |
Correct |
3 ms |
2584 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
3 ms |
2512 KB |
Output is correct |
23 |
Correct |
3 ms |
2396 KB |
Output is correct |
24 |
Correct |
3 ms |
2396 KB |
Output is correct |
25 |
Correct |
26 ms |
3336 KB |
Output is correct |
26 |
Correct |
68 ms |
6612 KB |
Output is correct |
27 |
Correct |
203 ms |
9772 KB |
Output is correct |
28 |
Correct |
216 ms |
9920 KB |
Output is correct |
29 |
Correct |
154 ms |
8692 KB |
Output is correct |
30 |
Correct |
267 ms |
10032 KB |
Output is correct |
31 |
Runtime error |
147 ms |
20928 KB |
Execution killed with signal 11 |
32 |
Halted |
0 ms |
0 KB |
- |