#include<bits/stdc++.h>
using namespace std;
// define
#define execute cerr << " Time: " << fixed << setprecision(6) << (1.0 * clock() / CLOCKS_PER_SEC) << "s\n";
#define ll long long
#define ii pair <int , int>
#define iii pair <int , ii>
#define se second
#define fi first
#define all(v) (v).begin() , (v).end()
#define Unique(v) sort(all(v)) , v.resize(unique(all(v)) - v.begin())
#define bit(x,i) (((x) >> (i)) & 1LL)
#define flip(x,i) ((x) ^ (1LL << (i)))
#define ms(d,x) memset(d , x , sizeof(d))
#define exist __exist
#define ends __ends
#define visit visited
#define left __left
#define right __right
#define prev __prev
#define next __next
#define sitingfake 1
#define orz 1
//constant
const long long mod = 1e9 + 7;
const long long linf = 4557430888798830399LL;
const long long nlinf = -4485090715960753727LL;
const int LOG = 20;
const int inf = 1061109567;
const int ninf = -1044266559;
const int dx[] = {0 , -1 , 0 , 1};
const int dy[] = {-1 , 0 , 1 , 0};
template<typename T> bool maximize(T &a, const T &b)
{
if(a < b) {a = b; return 1;}
return 0;
}
template<typename T> bool minimize(T &a, const T &b)
{
if(a > b) {a = b; return 1;}
return 0;
}
void Plus(ll & a ,ll b)
{
b %= mod;
a += b;
if(a < 0) a += mod;
a %= mod;
return;
}
void Mul(ll & a, ll b)
{
(a *= (b % mod)) %= mod;
return;
}
const int maxn = 1e5 + 7;
int n , m;
struct Seg
{
int l , r , mid;
ll cost;
}a[maxn];
int lim = 0;
ll st[12 * maxn][2] , dp_left[maxn] , dp_right[maxn];
void up(int i , int l , int r , int pos , ll val , bool t)
{
if(pos < l || pos > r) return;
if(l == r)
{
st[i][t] = min(st[i][t] , val);
return;
}
int mid = (r + l) >> 1;
up(i * 2 , l , mid , pos , val , t);
up(i * 2 + 1 , mid + 1 , r , pos , val , t);
st[i][t] = min(st[i * 2][t] , st[i * 2 + 1][t]);
}
ll get(int i , int l , int r , int u , int v , bool t)
{
if(u > r || v < l) return linf;
if(u <= l && r <= v) return st[i][t];
int mid = (r + l) >> 1;
return min(get(i * 2 , l , mid ,u , v , t) , get(i * 2 + 1 , mid + 1 , r , u , v , t));
}
void Compress()
{
vector <int> p;
p.push_back(1);
p.push_back(m);
for(int i = 1; i <= n; i++)
{
p.push_back(a[i].l);
p.push_back(a[i].r);
p.push_back(a[i].mid);
}
Unique(p);
for(int i = 1; i <= n; i++)
{
a[i].l = lower_bound(all(p) , a[i].l) - p.begin() + 1;
a[i].r = lower_bound(all(p) , a[i].r) - p.begin() + 1;
a[i].mid = lower_bound(all(p) , a[i].mid) - p.begin() + 1;
}
lim = p.size();
}
void solve(void)
{
cin >> n >> m;
for(int i = 1; i <= n; i++)
{
cin >> a[i].l >> a[i].r >> a[i].mid >> a[i].cost;
}
Compress();
ms(st ,0x3f) , ms(dp_left , 0x3f) , ms(dp_right , 0x3f);
ll ans = linf;
//cout << lim << endl;
for(int i = 1; i <= n; i++)
{
if(a[i].l == 1)
{
dp_left[i] = a[i].cost;
//cout << i << endl;
}
else
{
//cout << i << ' ' << a[i].l << " " << a[i].mid << " " << get(1 , 1 , lim , a[i].l , a[i].mid , 0) << endl;
dp_left[i] = min(linf , get(1 , 1 , lim , a[i].l , a[i].r, 0) + a[i].cost);
}
if(a[i].r == lim)
{
dp_right[i] = a[i].cost;
}
else dp_right[i] = min(linf , get(1 , 1 , lim , a[i].l , a[i].r , 1) + a[i].cost);
if(dp_left[i] != linf && dp_right[i] != linf)
{
ans = min(ans , dp_left[i] + dp_right[i] - a[i].cost);
}
if(dp_left[i] != linf)
{
//cout << i << " " << a[i].mid << endl;
up(1 , 1 , lim , a[i].mid , dp_left[i] , 0);
}
if(dp_right[i] != linf)
{
up(1 , 1 , lim , a[i].mid , dp_right[i] , 1);
}
}
if(ans == linf) {
cout << -1;
}else{
cout << ans;
}
}
/**
**/
signed main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
#define task ""
if(fopen(task".inp","r"))
{
freopen(task".inp","r",stdin);
freopen(task".out","w",stdout);
}
int tc = 1;
// cin >> tc;
while(tc--) solve();
// execute;
}
Compilation message (stderr)
pinball.cpp: In function 'int main()':
pinball.cpp:179:15: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
179 | freopen(task".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
pinball.cpp:180:15: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
180 | freopen(task".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |
| # | Verdict | Execution time | Memory | Grader output |
|---|
| Fetching results... |