#include <bits/stdc++.h>
#pragma GCC optimize("-Ofast")
//#pragma GCC optimize("trapv")
#pragma GCC target("sse,sse2,sse3,ssse3,sse4,sse4.2,popcnt,abm,mmx,avx2,tune=native")
#pragma GCC optimize("-ffast-math")
#pragma GCC optimize("-funroll-loops")
#define I inline void
#define S struct
#define vi vector<int>
#define vii vector<pair<int, int>>
#define pii pair<int, int>
#define pll pair<ll, ll>
using namespace std;
using ll = long long;
using ld = long double;
const int N = 2e5 + 7, mod = 1e9 + 7;
const ll inf = 2e18;
// How interesting!
int n, m;
map<int, int> mp;
ll cl[N], cr[N];
vector<int> ord;
struct dev
{
ll l, md, r, c;
dev() {}
dev(int _l, int _md, int _r, int _c)
{
l = _l;
md = _md;
r = _r;
c = _c;
}
};
vector<dev> v;
struct segtree
{
ll tree[4 * N];
segtree()
{
fill(tree, tree + 4 * N, inf);
}
void update(int node, int L, int R, int ix, ll val)
{
if (L == R)
{
tree[node] = min(tree[node], val);
return;
}
int mid = (L + R) >> 1;
if (ix <= mid)
update(node * 2 + 1, L, mid, ix, val);
else
update(node * 2 + 2, mid + 1, R, ix, val);
tree[node] = min(tree[node * 2 + 1], tree[node * 2 + 2]);
}
ll query(int node, int L, int R, int l, int r)
{
if (l > R || r < L)
return inf;
if (L >= l && R <= r)
return tree[node];
int mid = (L + R) >> 1;
ll s1 = query(node * 2 + 1, L, mid, l, r);
ll s2 = query(node * 2 + 2, mid + 1, R, l, r);
return min(s1, s2);
}
inline ll query(int l, int r) { return query(0, 1, N, l, r); }
inline void upd(int pos, ll val) { update(0, 1, N, pos, val); }
} s[2];
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
//freopen("in.in", "r", stdin);
cin >> n >> m;
for (int i = 1; i <= n; ++i)
{
int a, b, c, d;
cin >> a >> b >> c >> d;
v.push_back(dev(a, c, b, d));
ord.push_back(a);
ord.push_back(b);
ord.push_back(c);
}
sort(ord.begin(), ord.end());
ord.erase(unique(ord.begin(), ord.end()), ord.end());
for (int i = 0; i < (int)ord.size(); ++i)
{
mp[ord[i]] = i + 1;
}
for (int i = 0; i < n; ++i)
{
v[i].l = mp[v[i].l];
v[i].r = mp[v[i].r];
v[i].md = mp[v[i].md];
}
for (int i = 0; i < n; ++i)
{
cl[i] = (v[i].l == mp[1] ? 0 : s[0].query(v[i].l, v[i].r)) + v[i].c;
cr[i] = (v[i].r == mp[m] ? 0 : s[1].query(v[i].l, v[i].r)) + v[i].c;
s[0].upd(v[i].md, cl[i]);
s[1].upd(v[i].md, cr[i]);
}
ll ans = inf;
for (int i = 0; i < n; ++i)
{
ans = min(ans, cl[i] + cr[i] - v[i].c);
}
if (ans == inf)
cout << -1;
else
cout << ans;
return 0;
}
/*
- bounds sir (segtree = 4N, eulerTour = 2N, ...)
- a variable defined twice?
- will overflow?
- is it a good complexity?
- don't mess up indices (0-indexed vs 1-indexed)
- reset everything between testcases.
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
12908 KB |
Output is correct |
2 |
Correct |
7 ms |
13036 KB |
Output is correct |
3 |
Correct |
7 ms |
13036 KB |
Output is correct |
4 |
Correct |
7 ms |
12908 KB |
Output is correct |
5 |
Correct |
7 ms |
12908 KB |
Output is correct |
6 |
Correct |
7 ms |
12908 KB |
Output is correct |
7 |
Correct |
7 ms |
12908 KB |
Output is correct |
8 |
Correct |
7 ms |
12908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
12908 KB |
Output is correct |
2 |
Correct |
7 ms |
13036 KB |
Output is correct |
3 |
Correct |
7 ms |
13036 KB |
Output is correct |
4 |
Correct |
7 ms |
12908 KB |
Output is correct |
5 |
Correct |
7 ms |
12908 KB |
Output is correct |
6 |
Correct |
7 ms |
12908 KB |
Output is correct |
7 |
Correct |
7 ms |
12908 KB |
Output is correct |
8 |
Correct |
7 ms |
12908 KB |
Output is correct |
9 |
Correct |
8 ms |
12908 KB |
Output is correct |
10 |
Correct |
8 ms |
12908 KB |
Output is correct |
11 |
Correct |
8 ms |
13036 KB |
Output is correct |
12 |
Correct |
8 ms |
12908 KB |
Output is correct |
13 |
Correct |
7 ms |
12908 KB |
Output is correct |
14 |
Correct |
7 ms |
12908 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
12908 KB |
Output is correct |
2 |
Correct |
7 ms |
13036 KB |
Output is correct |
3 |
Correct |
7 ms |
13036 KB |
Output is correct |
4 |
Correct |
7 ms |
12908 KB |
Output is correct |
5 |
Correct |
7 ms |
12908 KB |
Output is correct |
6 |
Correct |
7 ms |
12908 KB |
Output is correct |
7 |
Correct |
7 ms |
12908 KB |
Output is correct |
8 |
Correct |
7 ms |
12908 KB |
Output is correct |
9 |
Correct |
8 ms |
12908 KB |
Output is correct |
10 |
Correct |
8 ms |
12908 KB |
Output is correct |
11 |
Correct |
8 ms |
13036 KB |
Output is correct |
12 |
Correct |
8 ms |
12908 KB |
Output is correct |
13 |
Correct |
7 ms |
12908 KB |
Output is correct |
14 |
Correct |
7 ms |
12908 KB |
Output is correct |
15 |
Correct |
7 ms |
12908 KB |
Output is correct |
16 |
Correct |
8 ms |
12908 KB |
Output is correct |
17 |
Correct |
9 ms |
13036 KB |
Output is correct |
18 |
Correct |
8 ms |
13036 KB |
Output is correct |
19 |
Correct |
9 ms |
13164 KB |
Output is correct |
20 |
Correct |
9 ms |
13036 KB |
Output is correct |
21 |
Correct |
8 ms |
13184 KB |
Output is correct |
22 |
Correct |
9 ms |
13164 KB |
Output is correct |
23 |
Correct |
9 ms |
13292 KB |
Output is correct |
24 |
Correct |
9 ms |
13164 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
12908 KB |
Output is correct |
2 |
Correct |
7 ms |
13036 KB |
Output is correct |
3 |
Correct |
7 ms |
13036 KB |
Output is correct |
4 |
Correct |
7 ms |
12908 KB |
Output is correct |
5 |
Correct |
7 ms |
12908 KB |
Output is correct |
6 |
Correct |
7 ms |
12908 KB |
Output is correct |
7 |
Correct |
7 ms |
12908 KB |
Output is correct |
8 |
Correct |
7 ms |
12908 KB |
Output is correct |
9 |
Correct |
8 ms |
12908 KB |
Output is correct |
10 |
Correct |
8 ms |
12908 KB |
Output is correct |
11 |
Correct |
8 ms |
13036 KB |
Output is correct |
12 |
Correct |
8 ms |
12908 KB |
Output is correct |
13 |
Correct |
7 ms |
12908 KB |
Output is correct |
14 |
Correct |
7 ms |
12908 KB |
Output is correct |
15 |
Correct |
7 ms |
12908 KB |
Output is correct |
16 |
Correct |
8 ms |
12908 KB |
Output is correct |
17 |
Correct |
9 ms |
13036 KB |
Output is correct |
18 |
Correct |
8 ms |
13036 KB |
Output is correct |
19 |
Correct |
9 ms |
13164 KB |
Output is correct |
20 |
Correct |
9 ms |
13036 KB |
Output is correct |
21 |
Correct |
8 ms |
13184 KB |
Output is correct |
22 |
Correct |
9 ms |
13164 KB |
Output is correct |
23 |
Correct |
9 ms |
13292 KB |
Output is correct |
24 |
Correct |
9 ms |
13164 KB |
Output is correct |
25 |
Correct |
33 ms |
14560 KB |
Output is correct |
26 |
Correct |
92 ms |
17880 KB |
Output is correct |
27 |
Correct |
289 ms |
24268 KB |
Output is correct |
28 |
Correct |
186 ms |
24908 KB |
Output is correct |
29 |
Correct |
198 ms |
22228 KB |
Output is correct |
30 |
Correct |
253 ms |
24780 KB |
Output is correct |
31 |
Correct |
457 ms |
30668 KB |
Output is correct |
32 |
Correct |
425 ms |
28492 KB |
Output is correct |
33 |
Correct |
66 ms |
17504 KB |
Output is correct |
34 |
Correct |
228 ms |
24788 KB |
Output is correct |
35 |
Incorrect |
298 ms |
36716 KB |
Output isn't correct |
36 |
Halted |
0 ms |
0 KB |
- |