#include <bits/stdc++.h>
#define fr(i, n, m) for(int i = (n); i < (m); i ++)
#define pb push_back
#define st first
#define nd second
#define pq priority_queue
#define all(x) begin(x), end(x)
#include <time.h>
#include <cmath>
using namespace std;
typedef long long ll;
typedef long double ld;
typedef pair<int,int> pii;
const ll inf = 1e17;
const ll mod = 1000000007;
const ld eps = 1e-13;
const ld pi = 3.14159265359;
mt19937 _rand(time(NULL));
clock_t timer = clock();
const int mxn = 5e6 + 5;
ll seg1[mxn];
ll seg2[mxn];
void update(int k, int l, int r, int pos, ll val){
if(r < pos || pos < l) return;
if(l == r){
if(val < seg1[k]) seg1[k] = val;
return;
}
int mid = (l + r) / 2;
update(k * 2, l, mid, pos, val);
update(k * 2 + 1, mid + 1, r, pos, val);
seg1[k] = min(seg1[k * 2], seg1[k * 2 + 1]);
}
ll query(int k, int l, int r, int x, int y){
if(r < x || y < l) return inf;
if(x <= l && r <= y){
return seg1[k];
}
int mid = (l + r) / 2;
return min(query(k * 2, l, mid, x, y), query(k * 2 + 1, mid + 1, r, x, y));
}
void update2(int k, int l, int r, int pos, ll val){
if(r < pos || pos < l) return;
if(l == r){
if(val < seg2[k])seg2[k] = val;
return;
}
int mid = (l + r) / 2;
update2(k * 2, l, mid, pos, val);
update2(k * 2 + 1, mid + 1, r, pos, val);
seg2[k] = min(seg2[k * 2], seg2[k * 2 + 1]);
}
ll query2(int k, int l, int r, int x, int y){
if(r < x || y < l) return inf;
if(x <= l && r <= y){
return seg2[k];
}
int mid = (l + r) /2 ;
return min(query2(k * 2, l, mid, x, y), query2(k * 2 + 1, mid + 1, r, x, y));
}
int maxbound = 1e5;
void solve(){
fr(i,0 , mxn){
seg1[i] = seg2[i] = inf;
}
int M, N;
cin >> M >> N;
int a[M], b[M], c[M], d[M];
vector<pair<int, pii> > v;
fr(i, 0, M){
cin >> a[i] >> b[i] >> c[i] >> d[i];
v.pb({a[i], {-1, i}});
v.pb({b[i], {1, i}});
v.pb({c[i], {0, i}});
}
sort(all(v));
int val = 0;
if(v[0].st != 1 || v.back().st != N){
cout << -1<<endl;
return;
}
if(v[0].nd.st == -1) a[v[0].nd.nd] = val;
else if(v[0].nd.st == 1) b[v[0].nd.nd] = val;
else c[v[0].nd.nd] = val;
fr(i, 1, (int)(v.size())){
if(v[i].st != v[i - 1].st) val ++;
if(v[i].nd.st == -1) a[v[i].nd.nd] = val;
else if(v[i].nd.st == 1) b[v[i].nd.nd] = val;
else c[v[i].nd.nd] = val;
}
maxbound = val;
ll ans = inf;
fr(i, 0, M){
int l = a[i], r = b[i], mid = c[i];
ll costleft = d[i];
if(l != 0){
costleft = min(inf, query(1, 0, maxbound, l, r) + d[i]);
}
update(1, 0, maxbound, mid, costleft);
ll costright = d[i];
if(r != maxbound){
costright = min(inf, query2(1, 0, maxbound, l, r) + d[i]);
}
update2(1, 0, maxbound, mid, costright);
ans = min(ans, costleft + costright - d[i]);
}
if(ans == inf) cout << -1 << endl;
else cout << ans << endl;
}
int main(){
ios_base::sync_with_stdio(false);
cin.tie(NULL);
solve();
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
44 ms |
78584 KB |
Output is correct |
2 |
Correct |
45 ms |
78592 KB |
Output is correct |
3 |
Correct |
43 ms |
78636 KB |
Output is correct |
4 |
Correct |
43 ms |
78584 KB |
Output is correct |
5 |
Correct |
47 ms |
78560 KB |
Output is correct |
6 |
Correct |
42 ms |
78584 KB |
Output is correct |
7 |
Correct |
42 ms |
78592 KB |
Output is correct |
8 |
Correct |
42 ms |
78584 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
44 ms |
78584 KB |
Output is correct |
2 |
Correct |
45 ms |
78592 KB |
Output is correct |
3 |
Correct |
43 ms |
78636 KB |
Output is correct |
4 |
Correct |
43 ms |
78584 KB |
Output is correct |
5 |
Correct |
47 ms |
78560 KB |
Output is correct |
6 |
Correct |
42 ms |
78584 KB |
Output is correct |
7 |
Correct |
42 ms |
78592 KB |
Output is correct |
8 |
Correct |
42 ms |
78584 KB |
Output is correct |
9 |
Correct |
43 ms |
78584 KB |
Output is correct |
10 |
Correct |
44 ms |
78712 KB |
Output is correct |
11 |
Correct |
42 ms |
78584 KB |
Output is correct |
12 |
Correct |
47 ms |
78584 KB |
Output is correct |
13 |
Correct |
47 ms |
78584 KB |
Output is correct |
14 |
Correct |
42 ms |
78584 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
44 ms |
78584 KB |
Output is correct |
2 |
Correct |
45 ms |
78592 KB |
Output is correct |
3 |
Correct |
43 ms |
78636 KB |
Output is correct |
4 |
Correct |
43 ms |
78584 KB |
Output is correct |
5 |
Correct |
47 ms |
78560 KB |
Output is correct |
6 |
Correct |
42 ms |
78584 KB |
Output is correct |
7 |
Correct |
42 ms |
78592 KB |
Output is correct |
8 |
Correct |
42 ms |
78584 KB |
Output is correct |
9 |
Correct |
43 ms |
78584 KB |
Output is correct |
10 |
Correct |
44 ms |
78712 KB |
Output is correct |
11 |
Correct |
42 ms |
78584 KB |
Output is correct |
12 |
Correct |
47 ms |
78584 KB |
Output is correct |
13 |
Correct |
47 ms |
78584 KB |
Output is correct |
14 |
Correct |
42 ms |
78584 KB |
Output is correct |
15 |
Correct |
42 ms |
78592 KB |
Output is correct |
16 |
Correct |
44 ms |
78712 KB |
Output is correct |
17 |
Correct |
49 ms |
78944 KB |
Output is correct |
18 |
Correct |
46 ms |
78712 KB |
Output is correct |
19 |
Correct |
47 ms |
78712 KB |
Output is correct |
20 |
Correct |
49 ms |
78712 KB |
Output is correct |
21 |
Correct |
47 ms |
78712 KB |
Output is correct |
22 |
Correct |
44 ms |
78712 KB |
Output is correct |
23 |
Correct |
45 ms |
78712 KB |
Output is correct |
24 |
Correct |
45 ms |
78712 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
44 ms |
78584 KB |
Output is correct |
2 |
Correct |
45 ms |
78592 KB |
Output is correct |
3 |
Correct |
43 ms |
78636 KB |
Output is correct |
4 |
Correct |
43 ms |
78584 KB |
Output is correct |
5 |
Correct |
47 ms |
78560 KB |
Output is correct |
6 |
Correct |
42 ms |
78584 KB |
Output is correct |
7 |
Correct |
42 ms |
78592 KB |
Output is correct |
8 |
Correct |
42 ms |
78584 KB |
Output is correct |
9 |
Correct |
43 ms |
78584 KB |
Output is correct |
10 |
Correct |
44 ms |
78712 KB |
Output is correct |
11 |
Correct |
42 ms |
78584 KB |
Output is correct |
12 |
Correct |
47 ms |
78584 KB |
Output is correct |
13 |
Correct |
47 ms |
78584 KB |
Output is correct |
14 |
Correct |
42 ms |
78584 KB |
Output is correct |
15 |
Correct |
42 ms |
78592 KB |
Output is correct |
16 |
Correct |
44 ms |
78712 KB |
Output is correct |
17 |
Correct |
49 ms |
78944 KB |
Output is correct |
18 |
Correct |
46 ms |
78712 KB |
Output is correct |
19 |
Correct |
47 ms |
78712 KB |
Output is correct |
20 |
Correct |
49 ms |
78712 KB |
Output is correct |
21 |
Correct |
47 ms |
78712 KB |
Output is correct |
22 |
Correct |
44 ms |
78712 KB |
Output is correct |
23 |
Correct |
45 ms |
78712 KB |
Output is correct |
24 |
Correct |
45 ms |
78712 KB |
Output is correct |
25 |
Correct |
67 ms |
79732 KB |
Output is correct |
26 |
Correct |
91 ms |
81520 KB |
Output is correct |
27 |
Correct |
199 ms |
84840 KB |
Output is correct |
28 |
Correct |
204 ms |
89828 KB |
Output is correct |
29 |
Correct |
154 ms |
84388 KB |
Output is correct |
30 |
Correct |
233 ms |
89828 KB |
Output is correct |
31 |
Correct |
301 ms |
89956 KB |
Output is correct |
32 |
Correct |
283 ms |
89828 KB |
Output is correct |
33 |
Correct |
76 ms |
80496 KB |
Output is correct |
34 |
Correct |
154 ms |
84588 KB |
Output is correct |
35 |
Correct |
214 ms |
89832 KB |
Output is correct |
36 |
Correct |
288 ms |
89832 KB |
Output is correct |
37 |
Correct |
261 ms |
89828 KB |
Output is correct |
38 |
Correct |
262 ms |
89956 KB |
Output is correct |