#include <bits/stdc++.h>
#define FOR(i,x,n) for(int i=x; i<n; i++)
#define F0R(i,n) FOR(i,0,n)
#define ROF(i,x,n) for(int i=n-1; i>=x; i--)
#define R0F(i,n) ROF(i,0,n)
#define WTF cout << "WTF" << endl
#define IOS ios::sync_with_stdio(false); cin.tie(0)
#define F first
#define S second
#define pb push_back
#define ALL(x) x.begin(), x.end()
#define RALL(x) x.rbegin(), x.rend()
using namespace std;
typedef long long LL;
typedef pair<int, int> PII;
typedef pair<LL, LL> PLL;
typedef vector<int> VI;
typedef vector<LL> VLL;
typedef vector<PII> VPII;
typedef vector<PLL> VPLL;
const int N = 1e6 + 7;
const LL INF = 5e17 + 7;
#define L 0
#define R 1
#define lc now << 1
#define rc now << 1 | 1
LL n, m;
LL tree[2][N << 2], ns[N][4];
VLL comp;
LL get(int pl, int lq, int rq, int now = 1, int ls = 0, int rs = m - 1) {
if (rq < lq || rq < ls || rs < lq) return INF;
if (lq <= ls && rs <= rq) return tree[pl][now];
int mid = (ls + rs) >> 1;
return min(get(pl, lq, rq, lc, ls, mid), get(pl, lq, rq, rc, mid + 1, rs));
}
void mnfy(int pl, int id, LL val, int now = 1, int ls = 0, int rs = m - 1) {
if (ls == rs) {
tree[pl][now] = min(tree[pl][now], val);
return;
}
int mid = (ls + rs) >> 1;
if (id <= mid) mnfy(pl, id, val, lc, ls, mid);
else mnfy(pl, id, val, rc, mid + 1, rs);
tree[pl][now] = min(tree[pl][lc], tree[pl][rc]);
return;
}
void build(int now = 1, int ls = 0, int rs = m - 1) {
tree[L][now] = tree[R][now] = INF;
if (ls == rs) return;
int mid = (ls + rs) >> 1;
build(lc, ls, mid); build(rc, mid + 1, rs);
return;
}
int main() {
IOS;
cin >> n >> m;
F0R(i, n) {
F0R(j, 3) {
cin >> ns[i][j];
comp.pb(ns[i][j]);
comp.pb(max(ns[i][j] - 1, 1ll));
comp.pb(min(ns[i][j] + 1, m));
}
cin >> ns[i][3];
}
comp.pb(1); comp.pb(n);
sort(ALL(comp));
comp.resize(unique(ALL(comp)) - comp.begin());
F0R(i, n) F0R(j, 3) ns[i][j] = lower_bound(ALL(comp), ns[i][j]) - comp.begin();
if (m == 1) {
cout << 0 << endl;
return 0;
}
/*for(int on : comp) cout << on << ' ';
cout << endl;*/
m = comp.size();
//cout << "size = " << m << endl;
build();
mnfy(L, 0, 0);
mnfy(R, m - 1, 0);
LL ans = INF;
F0R(i, n) {
ans = min(ans, get(L, ns[i][0], ns[i][1]) + get(R, ns[i][0], ns[i][1]) + ns[i][3]);
LL mn = get(L, ns[i][0], ns[i][1]);
mnfy(L, ns[i][2], mn + ns[i][3]);
mn = get(R, ns[i][0], ns[i][1]);
mnfy(R, ns[i][2], mn + ns[i][3]);
/*cout << "L : ";
F0R(i, m) cout << get(L, i, i) << ' ';
cout << endl;
cout << "R : ";
F0R(i, m) cout << get(R, i, i) << ' ';
cout << endl;*/
}
cout << (ans < INF ? ans : -1);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
328 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
328 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
3 ms |
724 KB |
Output is correct |
18 |
Correct |
2 ms |
596 KB |
Output is correct |
19 |
Correct |
3 ms |
856 KB |
Output is correct |
20 |
Correct |
3 ms |
724 KB |
Output is correct |
21 |
Correct |
2 ms |
724 KB |
Output is correct |
22 |
Correct |
3 ms |
1112 KB |
Output is correct |
23 |
Correct |
2 ms |
1108 KB |
Output is correct |
24 |
Correct |
3 ms |
1108 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
1 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
336 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
328 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
3 ms |
724 KB |
Output is correct |
18 |
Correct |
2 ms |
596 KB |
Output is correct |
19 |
Correct |
3 ms |
856 KB |
Output is correct |
20 |
Correct |
3 ms |
724 KB |
Output is correct |
21 |
Correct |
2 ms |
724 KB |
Output is correct |
22 |
Correct |
3 ms |
1112 KB |
Output is correct |
23 |
Correct |
2 ms |
1108 KB |
Output is correct |
24 |
Correct |
3 ms |
1108 KB |
Output is correct |
25 |
Correct |
29 ms |
3804 KB |
Output is correct |
26 |
Correct |
83 ms |
11984 KB |
Output is correct |
27 |
Correct |
262 ms |
18296 KB |
Output is correct |
28 |
Correct |
204 ms |
14552 KB |
Output is correct |
29 |
Correct |
164 ms |
15664 KB |
Output is correct |
30 |
Correct |
249 ms |
16556 KB |
Output is correct |
31 |
Correct |
372 ms |
30892 KB |
Output is correct |
32 |
Correct |
330 ms |
30900 KB |
Output is correct |
33 |
Correct |
48 ms |
11260 KB |
Output is correct |
34 |
Correct |
176 ms |
23864 KB |
Output is correct |
35 |
Correct |
238 ms |
47328 KB |
Output is correct |
36 |
Correct |
382 ms |
47328 KB |
Output is correct |
37 |
Correct |
329 ms |
47292 KB |
Output is correct |
38 |
Correct |
322 ms |
47272 KB |
Output is correct |