#include<bits/stdc++.h>
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef double db;
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define pbds tree<pair<int, int>, null_type, less<pair<int, int>>,rb_tree_tag, tree_order_statistics_node_update>
using namespace __gnu_pbds;
ll gcd(ll a , ll b) {return b ? gcd(b , a % b) : a ;} // greatest common divisor (PGCD)
ll lcm(ll a , ll b) {return (a * b) / gcd(a , b);} // least common multiple (PPCM)
int dx[8] = {1, 0, 0, -1, 1, 1, -1, -1};
int dy[8] = {0, 1, -1, 0, 1, -1, -1, 1};
#define endl "\n"
#define ss second
#define ff first
#define all(x) (x).begin() , (x).end()
#define pb push_back
#define vi vector<int>
#define vii vector<pair<int,int>>
#define vl vector<ll>
#define vll vector<pair<ll,ll>>
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pdd pair<double,double>
#define vdd vector<pdd>
#define speed ios::sync_with_stdio(false);cin.tie(0);cout.tie(0);
using namespace std;
////////////////////Only Clear Code//////////////////////////
void usaco_problem(){
freopen("milkvisits.in", "r", stdin);
freopen("milkvisits.out", "w", stdout);
}
void init(){
#ifndef ONLINE_JUDGE
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif // ONLINE_JUDGE
}
const int mx = 3e5+5;
const int LOG = 25;
const ll inf = 1e15;
const ll mod = 1e9+7;
ll dp1[mx], dp2[mx];
int a[mx], b[mx], c[mx], d[mx];
int n1;
ll segtree[mx*4], segtree1[mx*4];
map<int, int> mp;
ll query(int node, int qlow, int qhigh, int l, int r){
if(l >= qlow && r <= qhigh){
return segtree[node];
}
if(r < qlow || l > qhigh){
return inf;
}
int md = (l+r)/2;
return min(query(node*2, qlow, qhigh, l, md), query(node*2+1, qlow, qhigh, md+1, r));
}
ll query1(int node, int qlow, int qhigh, int l, int r){
if(l >= qlow && r <= qhigh){
return segtree1[node];
}
if(r < qlow || l > qhigh){
return inf;
}
int md = (l+r)/2;
return min(query1(node*2, qlow, qhigh, l, md), query1(node*2+1, qlow, qhigh, md+1, r));
}
void update(int ind, ll v, ll u){
segtree[ind+n1] = min(v, segtree[ind+n1]);
segtree1[ind+n1] = min(u, segtree1[ind+n1]);
for(int j = (ind+n1)/2; j >= 1;j/=2){
segtree[j] = min(segtree[j*2], segtree[j*2+1]);
segtree1[j] = min(segtree1[j*2], segtree1[j*2+1]);
}
}
int cnt(int n){
int c = 0;
while(n > 0){
if(n&1)c++;
n/=2;
}
return c;
}
void run_case(){
int n, m;cin >> n >> m;
for(int i = 0;i < n;i++){
dp1[i] = dp2[i] = inf;
}
set<int> s;
for(int i = 0;i < n;i++){
cin >> a[i] >> b[i] >> c[i] >> d[i];
s.insert(a[i]);
s.insert(b[i]);
s.insert(c[i]);
}
n1 = s.size();
while(cnt(n1) != 1){
n1++;
}
for(int i = 1;i < 2*n1;i++){
segtree[i] = segtree1[i] = inf;
}
int k = 0;
for(int x : s){
mp[x] = k++;
}
ll ans = inf;
for(int i = 0;i < n;i++){
if(a[i] == 1)dp1[i] = 0;
if(b[i] == m)dp2[i] = 0;
dp1[i] = min(dp1[i],query(1, mp[a[i]], mp[b[i]], 0, n1-1));
dp2[i] = min(dp2[i],query1(1, mp[a[i]], mp[b[i]], 0, n1-1));
dp1[i] += d[i];
dp2[i] += d[i];
update(mp[c[i]], dp1[i], dp2[i]);
ans = min(ans, dp1[i]+dp2[i]-d[i]);
}
cout << (ans >= inf ? -1 : ans) << endl;
}
int main(){
speed;
int t;
//cin >> t;
t = 1;
while(t--){
run_case();
}
}
/*
NEVER GIVE UP!
DOING SMTHNG IS BETTER THAN DOING NTHNG!!!
Your Guide when stuck:
- Continue keyword only after reading the whole input
- Don't use memset with testcases
- Check for corner cases(n=1, n=0)
- Check where you declare n(Be careful of declaring it globally and in main)
*/
Compilation message
pinball.cpp: In function 'void usaco_problem()':
pinball.cpp:33:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
33 | freopen("milkvisits.in", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
pinball.cpp:34:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
34 | freopen("milkvisits.out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
pinball.cpp: In function 'void init()':
pinball.cpp:40:8: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
40 | freopen("input.txt", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
pinball.cpp:42:8: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
42 | freopen("output.txt", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
264 KB |
Output is correct |
4 |
Correct |
0 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 |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
264 KB |
Output is correct |
4 |
Correct |
0 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 |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
264 KB |
Output is correct |
4 |
Correct |
0 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 |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
2 ms |
596 KB |
Output is correct |
18 |
Correct |
2 ms |
432 KB |
Output is correct |
19 |
Correct |
3 ms |
724 KB |
Output is correct |
20 |
Correct |
2 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
596 KB |
Output is correct |
22 |
Correct |
3 ms |
724 KB |
Output is correct |
23 |
Correct |
3 ms |
724 KB |
Output is correct |
24 |
Correct |
2 ms |
724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
264 KB |
Output is correct |
4 |
Correct |
0 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 |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
468 KB |
Output is correct |
13 |
Correct |
1 ms |
468 KB |
Output is correct |
14 |
Correct |
1 ms |
468 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
468 KB |
Output is correct |
17 |
Correct |
2 ms |
596 KB |
Output is correct |
18 |
Correct |
2 ms |
432 KB |
Output is correct |
19 |
Correct |
3 ms |
724 KB |
Output is correct |
20 |
Correct |
2 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
596 KB |
Output is correct |
22 |
Correct |
3 ms |
724 KB |
Output is correct |
23 |
Correct |
3 ms |
724 KB |
Output is correct |
24 |
Correct |
2 ms |
724 KB |
Output is correct |
25 |
Correct |
25 ms |
3284 KB |
Output is correct |
26 |
Correct |
80 ms |
8396 KB |
Output is correct |
27 |
Correct |
233 ms |
16080 KB |
Output is correct |
28 |
Correct |
120 ms |
3656 KB |
Output is correct |
29 |
Correct |
173 ms |
14600 KB |
Output is correct |
30 |
Correct |
201 ms |
6192 KB |
Output is correct |
31 |
Correct |
411 ms |
27492 KB |
Output is correct |
32 |
Correct |
345 ms |
18920 KB |
Output is correct |
33 |
Correct |
51 ms |
8564 KB |
Output is correct |
34 |
Correct |
230 ms |
24144 KB |
Output is correct |
35 |
Correct |
301 ms |
48016 KB |
Output is correct |
36 |
Correct |
574 ms |
48092 KB |
Output is correct |
37 |
Correct |
482 ms |
48176 KB |
Output is correct |
38 |
Correct |
437 ms |
48144 KB |
Output is correct |