#include <bits/stdc++.h>
using namespace std;
const long long INFll = 1e18;
const int INFii = 1e9;
typedef long long ll;
typedef int ii;
typedef double dbl;
#define endl '\n'
#define sc second
#define fr first
#define mp make_pair
#define pb push_back
#define maxn 110000
//LEMBRAR DE MUDAR
ll n, m, dp1[5*maxn], dp2[5*maxn], tr[20*maxn][2];
ll a[maxn], b[maxn], c[maxn], d[maxn];
void build(ll no, ll l, ll r) {
tr[no][0] = tr[no][1] = INFll;
ll mid = (l+r)/2;
ll f1 = 2*no;
ll f2 = 2*no+1;
if(l == r) return;
build(f1,l,mid);
build(f2,mid+1,r);
}
//seg de minimo
void att(ll no, ll l, ll r, ll pos, ll val, ll p) {
if(l > pos || r < pos) return;
else if(l == r) {
tr[no][p] = min(val,tr[no][p]);
}
else {
ll mid = (l+r)/2;
ll f1 = 2*no;
ll f2 = 2*no+1;
att(f1,l,mid,pos,val,p);
att(f2,mid+1,r,pos,val,p);
tr[no][p] = min(tr[f1][p],tr[f2][p]);
}
}
ll query(ll no, ll l, ll r, ll L, ll R, ll p) {
if(l > R || r < L) return INFll;
else if(l >= L && r <= R) {
return tr[no][p];
}
else {
ll mid = (l+r)/2;
ll f1 = 2*no;
ll f2 = 2*no+1;
return min(query(f1,l,mid,L,R,p),query(f2,mid+1,r,L,R,p));
}
}
int main() {
ios::sync_with_stdio(false); cin.tie(0);
//freopen("in.in", "r", stdin);
//freopen("____.out", "w", stdout);
cin >> m >> n;
vector<ll> comp;
for(ii i = 1; i <= m; i++) {
cin >> a[i] >> b[i] >> c[i] >> d[i];
if(a[i]-1 != 0) comp.pb(a[i]-1);
comp.pb(a[i]);
comp.pb(c[i]);
comp.pb(b[i]);
if(b[i]+1 != n+1) comp.pb(b[i]+1);
}
sort(comp.begin(),comp.end());
comp.erase(unique(comp.begin(),comp.end()),comp.end());
n = comp.size();
build(1,1,n);
for(ll i = 1; i <= n; i++) {
dp1[i] = dp2[i] = INFll;
}
dp1[1] = 0;
att(1,1,n,1,0,0);
dp2[n] = 0;
att(1,1,n,n,0,1);
for(ll i = 1; i <= m; i++) {
a[i] = upper_bound(comp.begin(),comp.end(),a[i]) - comp.begin();
b[i] = upper_bound(comp.begin(),comp.end(),b[i]) - comp.begin();
c[i] = upper_bound(comp.begin(),comp.end(),c[i]) - comp.begin();
}
ll ans = INFll;
for(ll i = 1; i <= m; i++) {
ll mn1 = query(1,1,n,a[i],b[i],0);
dp1[c[i]] = min(dp1[c[i]], mn1+d[i]);
att(1,1,n,c[i],mn1+d[i],0);
ll mn2 = query(1,1,n,a[i],b[i],1);
dp2[c[i]] = min(dp1[c[i]], mn2+d[i]);
att(1,1,n,c[i],mn2+d[i],1);
ans = min(ans, mn1+mn2+d[i]);
}
if(ans >= (ll) 1e16) cout << -1 << endl;
else cout << ans << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
324 KB |
Output is correct |
5 |
Correct |
1 ms |
324 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
324 KB |
Output is correct |
5 |
Correct |
1 ms |
324 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
2 ms |
332 KB |
Output is correct |
10 |
Correct |
2 ms |
332 KB |
Output is correct |
11 |
Correct |
2 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
328 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
324 KB |
Output is correct |
5 |
Correct |
1 ms |
324 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
2 ms |
332 KB |
Output is correct |
10 |
Correct |
2 ms |
332 KB |
Output is correct |
11 |
Correct |
2 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
328 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
332 KB |
Output is correct |
17 |
Correct |
3 ms |
596 KB |
Output is correct |
18 |
Correct |
3 ms |
460 KB |
Output is correct |
19 |
Correct |
4 ms |
816 KB |
Output is correct |
20 |
Correct |
3 ms |
588 KB |
Output is correct |
21 |
Correct |
2 ms |
592 KB |
Output is correct |
22 |
Correct |
4 ms |
844 KB |
Output is correct |
23 |
Correct |
3 ms |
872 KB |
Output is correct |
24 |
Correct |
3 ms |
844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
1 ms |
344 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
324 KB |
Output is correct |
5 |
Correct |
1 ms |
324 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
2 ms |
332 KB |
Output is correct |
10 |
Correct |
2 ms |
332 KB |
Output is correct |
11 |
Correct |
2 ms |
332 KB |
Output is correct |
12 |
Correct |
1 ms |
332 KB |
Output is correct |
13 |
Correct |
1 ms |
328 KB |
Output is correct |
14 |
Correct |
1 ms |
332 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
332 KB |
Output is correct |
17 |
Correct |
3 ms |
596 KB |
Output is correct |
18 |
Correct |
3 ms |
460 KB |
Output is correct |
19 |
Correct |
4 ms |
816 KB |
Output is correct |
20 |
Correct |
3 ms |
588 KB |
Output is correct |
21 |
Correct |
2 ms |
592 KB |
Output is correct |
22 |
Correct |
4 ms |
844 KB |
Output is correct |
23 |
Correct |
3 ms |
872 KB |
Output is correct |
24 |
Correct |
3 ms |
844 KB |
Output is correct |
25 |
Correct |
33 ms |
2828 KB |
Output is correct |
26 |
Correct |
94 ms |
8488 KB |
Output is correct |
27 |
Correct |
310 ms |
18156 KB |
Output is correct |
28 |
Correct |
206 ms |
11344 KB |
Output is correct |
29 |
Correct |
228 ms |
12064 KB |
Output is correct |
30 |
Correct |
289 ms |
12668 KB |
Output is correct |
31 |
Correct |
399 ms |
23224 KB |
Output is correct |
32 |
Correct |
371 ms |
22000 KB |
Output is correct |
33 |
Correct |
64 ms |
8128 KB |
Output is correct |
34 |
Correct |
210 ms |
17992 KB |
Output is correct |
35 |
Correct |
278 ms |
35572 KB |
Output is correct |
36 |
Correct |
469 ms |
35556 KB |
Output is correct |
37 |
Correct |
417 ms |
35520 KB |
Output is correct |
38 |
Correct |
400 ms |
35504 KB |
Output is correct |