#include <bits/stdc++.h>
#define ll long long
#define F first
#define S second
#define MAXN 100005
#define ii pair<int,int>
#define bit(i,j) ((i>>j)&1)
#define sz(i) (int)i.size()
#define endl '\n'
using namespace std;
const ll INF = 1e18 + 7;
int n, m;
int a[MAXN], b[MAXN], c[MAXN], d[MAXN];
namespace sub1
{
int M;
ll left[MAXN], right[MAXN];
vector<int>v;
struct tree1
{
ll st[12 * MAXN];
void seg(int id, int l, int r)
{
if(l == r)
return st[id] = INF, void();
int mid = (l + r) / 2;
seg(id * 2, l, mid);
seg(id * 2 + 1, mid + 1, r);
st[id] = INF;
}
void update(int id, int l, int r, int u, ll v)
{
if(l > u || r < u)
return ;
if(l == r)
return st[id] = min(st[id], v), void();
int mid = (l + r) / 2;
if(u <= mid)
update(id * 2, l, mid, u, v);
else
update(id * 2 + 1, mid + 1, r, u, v);
st[id] = min(st[id * 2], st[id * 2 + 1]);
}
ll get(int id, int l, int r, int u, int v)
{
if(l > v || r < u)
return INF;
if(l >= u && r <= v)
return st[id];
int mid = (l + r) / 2;
return min(get(id * 2, l, mid, u, v), get(id * 2 + 1, mid + 1, r, u, v));
}
} ST1;
struct tree2
{
ll st[12 * MAXN];
void seg(int id, int l, int r)
{
if(l == r)
return st[id] = INF, void();
int mid = (l + r) / 2;
seg(id * 2, l, mid);
seg(id * 2 + 1, mid + 1, r);
st[id] = INF;
}
void update(int id, int l, int r, int u, ll v)
{
if(l > u || r < u)
return ;
if(l == r)
return st[id] = min(st[id], v), void();
int mid = (l + r) / 2;
if(u <= mid)
update(id * 2, l, mid, u, v);
else
update(id * 2 + 1, mid + 1, r, u, v);
st[id] = min(st[id * 2], st[id * 2 + 1]);
}
ll get(int id, int l, int r, int u, int v)
{
if(l > v || r < u)
return INF;
if(l >= u && r <= v)
return st[id];
int mid = (l + r) / 2;
return min(get(id * 2, l, mid, u, v), get(id * 2 + 1, mid + 1, r, u, v));
}
} ST2;
void compress()
{
for(int i = 1; i <= n; i++)
v.push_back(a[i]),
v.push_back(b[i]),
v.push_back(c[i]);
sort(v.begin(), v.end());
v.resize(unique(v.begin(), v.end()) - v.begin());
M = sz(v);
for(int i = 1; i <= n; i++)
a[i] = lower_bound(v.begin(), v.end(), a[i]) - v.begin() + 1,
b[i] = lower_bound(v.begin(), v.end(), b[i]) - v.begin() + 1,
c[i] = lower_bound(v.begin(), v.end(), c[i]) - v.begin() + 1;
}
void solve()
{
compress();
ST1.seg(1, 1, M);
ST2.seg(1, 1, M);
for(int i = 1; i <= n; i++)
{
if(v[a[i]-1] == 1)
{
left[i] = d[i];
ST1.update(1, 1, M, c[i], d[i]);
}
else
{
left[i] = d[i] + ST1.get(1, 1, M, a[i], b[i]);
if(left[i] < INF)
ST1.update(1, 1, M, c[i], left[i]);
}
// cout << i << " " << left[i] << endl;
}
for(int i = 1; i <= n; i++)
{
if(v[b[i]-1] == m)
{
right[i] = d[i];
ST2.update(1, 1, M, c[i], d[i]);
}
else
{
right[i] = ST2.get(1, 1, M, a[i], b[i]) + d[i];
if(right[i] < INF)
ST2.update(1, 1, M, c[i], right[i]);
}
// cout << i << " " << right[i] << endl;
}
ll res = INF;
for(int i = 1; i <= n; i++)
res = min(res, left[i] + right[i] - d[i]);
cout << (res == INF ? -1 : res);
}
}
main()
{
if(fopen("TEST.inp", "r"))
{
freopen("TEST.inp", "r", stdin);
freopen("TEST.out", "w", stdout);
}
ios_base::sync_with_stdio(0);
cin.tie(0);
cin >> n >> m;
for(int i = 1; i <= n; i++)
cin >> a[i] >> b[i] >> c[i] >> d[i];
sub1::solve();
}
Compilation message (stderr)
pinball.cpp:144:1: warning: ISO C++ forbids declaration of 'main' with no type [-Wreturn-type]
144 | main()
| ^~~~
pinball.cpp: In function 'int main()':
pinball.cpp:148:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
148 | freopen("TEST.inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
pinball.cpp:149:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
149 | freopen("TEST.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... |