#pragma GCC optimize ("O3")
#pragma GCC target ("sse4")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int, int> pi;
#define debug(x) cerr << #x << ": " << x << endl
#define debug2(x, y) debug(x), debug(y)
#define repn(i, a, b) for(int i = (int)(a); i < (int)(b); i++)
#define rep(i, a) for(int i = 0; i < (int)(a); i++)
#define all(v) v.begin(), v.end()
#define mp make_pair
#define pb push_back
#define lb lower_bound
#define ub upper_bound
#define fi first
#define se second
#define sq(x) ((x) * (x))
const int mxN = 1e5 + 5;
template<class T> T gcd(T a, T b){ return ((b == 0) ? a : gcd(b, a % b)); }
int m, n;
int a[mxN], b[mxN], c[mxN], d[mxN];
vi comp;
int find(int nm){
return lower_bound(comp.begin(), comp.end(), nm) - comp.begin();
}
struct seg{
ll dat[4 * mxN];
int sz;
seg(int s){
sz = s;
for(int i = 0; i < (4 * mxN); i++){
dat[i] = 1e18;
}
}
void update(int i, int s, int e, int x, ll v){
if(s == e){
dat[i] = min(dat[i], v);
return;
}
int m = (s + e) / 2;
if(x <= m) update(i << 1, s, m, x, v);
else update(i << 1 | 1, m + 1, e, x, v);
dat[i] = min(dat[i << 1], dat[i << 1 | 1]);
}
void update(int x, ll v){
update(1, 0, sz, x, v);
}
ll query(int i, int s, int e, int x, int y){
if(e < x || y < s) return 1e18;
if(x <= s && e <= y) return dat[i];
int m = (s + e) / 2;
return min(query(i << 1, s, m, x, y), query(i << 1 | 1, m + 1, e, x, y));
}
ll query(int x, int y){
return query(1, 0, sz, x, y);
}
};
int main(){
ios_base::sync_with_stdio(false);
cin.tie(0);
//freopen("input.in", "r", stdin);
//freopen("output.out", "w", stdout);
cin >> m >> n;
rep(i, m) cin >> a[i] >> b[i] >> c[i] >> d[i], comp.pb(c[i]);
comp.push_back(1);
comp.push_back(n);
sort(all(comp));
comp.erase(unique(all(comp)), comp.end());
int tot = comp.size() - 1;
seg lseg(tot), rseg(tot);
rep(i, m){
a[i] = find(a[i]);
b[i] = find(b[i] + 1) - 1;
c[i] = find(c[i]);
}
lseg.update(0, 0);
rseg.update(tot, 0);
ll ans = 1e18;
rep(i, m){
ans = min(ans, lseg.query(a[i], b[i]) + rseg.query(a[i], b[i]) + d[i]);
lseg.update(c[i], lseg.query(a[i], b[i]) + d[i]);
rseg.update(c[i], rseg.query(a[i], b[i]) + d[i]);
}
cout << (ans == 1e18 ? -1 : ans) << endl;
return 0;
}
/*
Things to look out for:
- Integer overflows
- Array bounds
- Special cases
Be careful!
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
6656 KB |
Output is correct |
2 |
Correct |
4 ms |
6656 KB |
Output is correct |
3 |
Correct |
4 ms |
6656 KB |
Output is correct |
4 |
Correct |
4 ms |
6656 KB |
Output is correct |
5 |
Correct |
4 ms |
6656 KB |
Output is correct |
6 |
Correct |
5 ms |
6656 KB |
Output is correct |
7 |
Correct |
4 ms |
6656 KB |
Output is correct |
8 |
Correct |
4 ms |
6656 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
6656 KB |
Output is correct |
2 |
Correct |
4 ms |
6656 KB |
Output is correct |
3 |
Correct |
4 ms |
6656 KB |
Output is correct |
4 |
Correct |
4 ms |
6656 KB |
Output is correct |
5 |
Correct |
4 ms |
6656 KB |
Output is correct |
6 |
Correct |
5 ms |
6656 KB |
Output is correct |
7 |
Correct |
4 ms |
6656 KB |
Output is correct |
8 |
Correct |
4 ms |
6656 KB |
Output is correct |
9 |
Correct |
4 ms |
6656 KB |
Output is correct |
10 |
Correct |
4 ms |
6656 KB |
Output is correct |
11 |
Correct |
4 ms |
6656 KB |
Output is correct |
12 |
Correct |
4 ms |
6656 KB |
Output is correct |
13 |
Correct |
4 ms |
6656 KB |
Output is correct |
14 |
Correct |
4 ms |
6656 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
6656 KB |
Output is correct |
2 |
Correct |
4 ms |
6656 KB |
Output is correct |
3 |
Correct |
4 ms |
6656 KB |
Output is correct |
4 |
Correct |
4 ms |
6656 KB |
Output is correct |
5 |
Correct |
4 ms |
6656 KB |
Output is correct |
6 |
Correct |
5 ms |
6656 KB |
Output is correct |
7 |
Correct |
4 ms |
6656 KB |
Output is correct |
8 |
Correct |
4 ms |
6656 KB |
Output is correct |
9 |
Correct |
4 ms |
6656 KB |
Output is correct |
10 |
Correct |
4 ms |
6656 KB |
Output is correct |
11 |
Correct |
4 ms |
6656 KB |
Output is correct |
12 |
Correct |
4 ms |
6656 KB |
Output is correct |
13 |
Correct |
4 ms |
6656 KB |
Output is correct |
14 |
Correct |
4 ms |
6656 KB |
Output is correct |
15 |
Correct |
4 ms |
6656 KB |
Output is correct |
16 |
Correct |
4 ms |
6656 KB |
Output is correct |
17 |
Correct |
6 ms |
6656 KB |
Output is correct |
18 |
Correct |
5 ms |
6656 KB |
Output is correct |
19 |
Correct |
6 ms |
6656 KB |
Output is correct |
20 |
Correct |
6 ms |
6656 KB |
Output is correct |
21 |
Correct |
5 ms |
6656 KB |
Output is correct |
22 |
Correct |
5 ms |
6656 KB |
Output is correct |
23 |
Correct |
5 ms |
6656 KB |
Output is correct |
24 |
Correct |
5 ms |
6656 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
6656 KB |
Output is correct |
2 |
Correct |
4 ms |
6656 KB |
Output is correct |
3 |
Correct |
4 ms |
6656 KB |
Output is correct |
4 |
Correct |
4 ms |
6656 KB |
Output is correct |
5 |
Correct |
4 ms |
6656 KB |
Output is correct |
6 |
Correct |
5 ms |
6656 KB |
Output is correct |
7 |
Correct |
4 ms |
6656 KB |
Output is correct |
8 |
Correct |
4 ms |
6656 KB |
Output is correct |
9 |
Correct |
4 ms |
6656 KB |
Output is correct |
10 |
Correct |
4 ms |
6656 KB |
Output is correct |
11 |
Correct |
4 ms |
6656 KB |
Output is correct |
12 |
Correct |
4 ms |
6656 KB |
Output is correct |
13 |
Correct |
4 ms |
6656 KB |
Output is correct |
14 |
Correct |
4 ms |
6656 KB |
Output is correct |
15 |
Correct |
4 ms |
6656 KB |
Output is correct |
16 |
Correct |
4 ms |
6656 KB |
Output is correct |
17 |
Correct |
6 ms |
6656 KB |
Output is correct |
18 |
Correct |
5 ms |
6656 KB |
Output is correct |
19 |
Correct |
6 ms |
6656 KB |
Output is correct |
20 |
Correct |
6 ms |
6656 KB |
Output is correct |
21 |
Correct |
5 ms |
6656 KB |
Output is correct |
22 |
Correct |
5 ms |
6656 KB |
Output is correct |
23 |
Correct |
5 ms |
6656 KB |
Output is correct |
24 |
Correct |
5 ms |
6656 KB |
Output is correct |
25 |
Correct |
25 ms |
7168 KB |
Output is correct |
26 |
Correct |
63 ms |
8064 KB |
Output is correct |
27 |
Correct |
187 ms |
10612 KB |
Output is correct |
28 |
Correct |
172 ms |
12528 KB |
Output is correct |
29 |
Correct |
128 ms |
9592 KB |
Output is correct |
30 |
Correct |
221 ms |
12656 KB |
Output is correct |
31 |
Correct |
271 ms |
12532 KB |
Output is correct |
32 |
Correct |
263 ms |
12532 KB |
Output is correct |
33 |
Correct |
35 ms |
7808 KB |
Output is correct |
34 |
Correct |
115 ms |
9592 KB |
Output is correct |
35 |
Correct |
169 ms |
12660 KB |
Output is correct |
36 |
Correct |
283 ms |
12532 KB |
Output is correct |
37 |
Correct |
251 ms |
12616 KB |
Output is correct |
38 |
Correct |
220 ms |
12532 KB |
Output is correct |