#include <bits/stdc++.h>
using namespace std;
//#pragma GCC optimize ("O3")
//#pragma GCC target ("sse4")
#define endl "\n"
typedef long long ll;
template<class T, class T2> inline ostream &operator <<(ostream &out, const pair<T, T2> &x) { out << x.first << " " << x.second; return out;}
template<class T, class T2> inline istream &operator >>(istream &in, pair<T, T2> &x) { in >> x.first >> x.second; return in;}
template<class T, class T2> inline bool chkmax(T &x, const T2 &y) { return x < y ? x = y, 1 : 0; }
template<class T, class T2> inline bool chkmin(T &x, const T2 &y) { return x > y ? x = y, 1 : 0; }
const ll mod = 2e15 + 7;
#define debug(...) cout << "Line: " << __LINE__ << endl; \
prllDebug(", "#__VA_ARGS__, __VA_ARGS__)
template <typename T>
void prllDebug(const char* name, T&& arg1) { cout << (name + 2) << " = " << arg1 << endl; }
template <typename T, typename... T2>
void prllDebug(const char* names, T&& arg1, T2&&... args) {
const char* end = strchr(names + 1, ',');
cout.write(names + 2, end - names - 2) << " = " << arg1 << endl;
prllDebug(end, args...);
}
const ll MAX_N = 5e5 + 10;
struct SegTree {
ll tree[4 * MAX_N];
SegTree() {
fill(tree, tree + MAX_N * 4, mod);
}
ll ans(ll curr, ll l, ll r, ll ql, ll qr) {
if(ql <= l && r <= qr) {
return tree[curr];
} else if(ql > r || l > qr) {
return mod;
}
ll m = (l + r) / 2ll;
return min(
ans(curr * 2, l, m, ql, qr),
ans(curr * 2 + 1, m + 1, r, ql, qr)
);
}
void upd(ll curr, ll l, ll r, ll ind, ll val) {
if(l == r && l == ind) {
chkmin(tree[curr], val);
return;
} else if(l > ind || r < ind) {return;}
ll m = (l + r) / 2ll;
upd(curr * 2, l, m, ind, val);
upd(curr * 2 + 1, m + 1, r, ind, val);
tree[curr] = min(tree[curr * 2], tree[curr * 2 + 1]);
}
};
SegTree lft, rght;
struct Comp {
vector<ll> start;
void push(ll x) {
start.push_back(x);
}
void compress() {
sort(start.begin(), start.end());
start.resize(unique(start.begin(), start.end()) - start.begin());
}
ll get(ll x) {
return lower_bound(start.begin(), start.end(), x) - start.begin();
}
};
Comp comp;
ll n, m;
ll a[MAX_N], b[MAX_N], c[MAX_N], d[MAX_N];
signed main() {
// ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL);
cin >> m >> n;
for(ll i = 0; i < m; i ++) {
cin >> a[i] >> b[i] >> c[i] >> d[i];
comp.push(a[i]);
comp.push(b[i]);
comp.push(c[i]);
}
comp.compress();
ll ans = mod;
for(ll i = 0; i < m; i ++) {
ll lftBest = lft.ans(1, 0, MAX_N - 1, comp.get(a[i]), comp.get(b[i]));
if(a[i] == 1) {
lftBest = 0;
}
lftBest += d[i];
lft.upd(1, 0, MAX_N - 1, comp.get(c[i]), lftBest);
ll rghtBest = rght.ans(1, 0, MAX_N - 1, comp.get(a[i]), comp.get(b[i]));
if(b[i] == n) {
rghtBest = 0;
}
rghtBest += d[i];
rght.upd(1, 0, MAX_N - 1, comp.get(c[i]), rghtBest);
chkmin(ans, lftBest + rghtBest - d[i]);
}
if(ans == mod) {
cout << -1 << endl;
} else {
cout << ans << endl;
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
31564 KB |
Output is correct |
2 |
Correct |
15 ms |
31588 KB |
Output is correct |
3 |
Correct |
16 ms |
31548 KB |
Output is correct |
4 |
Correct |
17 ms |
31552 KB |
Output is correct |
5 |
Correct |
15 ms |
31564 KB |
Output is correct |
6 |
Correct |
17 ms |
31564 KB |
Output is correct |
7 |
Correct |
17 ms |
31644 KB |
Output is correct |
8 |
Correct |
18 ms |
31564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
31564 KB |
Output is correct |
2 |
Correct |
15 ms |
31588 KB |
Output is correct |
3 |
Correct |
16 ms |
31548 KB |
Output is correct |
4 |
Correct |
17 ms |
31552 KB |
Output is correct |
5 |
Correct |
15 ms |
31564 KB |
Output is correct |
6 |
Correct |
17 ms |
31564 KB |
Output is correct |
7 |
Correct |
17 ms |
31644 KB |
Output is correct |
8 |
Correct |
18 ms |
31564 KB |
Output is correct |
9 |
Correct |
14 ms |
31564 KB |
Output is correct |
10 |
Correct |
15 ms |
31608 KB |
Output is correct |
11 |
Correct |
16 ms |
31568 KB |
Output is correct |
12 |
Correct |
16 ms |
31564 KB |
Output is correct |
13 |
Correct |
16 ms |
31612 KB |
Output is correct |
14 |
Correct |
16 ms |
31564 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
31564 KB |
Output is correct |
2 |
Correct |
15 ms |
31588 KB |
Output is correct |
3 |
Correct |
16 ms |
31548 KB |
Output is correct |
4 |
Correct |
17 ms |
31552 KB |
Output is correct |
5 |
Correct |
15 ms |
31564 KB |
Output is correct |
6 |
Correct |
17 ms |
31564 KB |
Output is correct |
7 |
Correct |
17 ms |
31644 KB |
Output is correct |
8 |
Correct |
18 ms |
31564 KB |
Output is correct |
9 |
Correct |
14 ms |
31564 KB |
Output is correct |
10 |
Correct |
15 ms |
31608 KB |
Output is correct |
11 |
Correct |
16 ms |
31568 KB |
Output is correct |
12 |
Correct |
16 ms |
31564 KB |
Output is correct |
13 |
Correct |
16 ms |
31612 KB |
Output is correct |
14 |
Correct |
16 ms |
31564 KB |
Output is correct |
15 |
Correct |
15 ms |
31596 KB |
Output is correct |
16 |
Correct |
15 ms |
31640 KB |
Output is correct |
17 |
Correct |
18 ms |
31692 KB |
Output is correct |
18 |
Correct |
20 ms |
31692 KB |
Output is correct |
19 |
Correct |
19 ms |
31692 KB |
Output is correct |
20 |
Correct |
19 ms |
31692 KB |
Output is correct |
21 |
Correct |
23 ms |
31628 KB |
Output is correct |
22 |
Correct |
18 ms |
31692 KB |
Output is correct |
23 |
Correct |
18 ms |
31680 KB |
Output is correct |
24 |
Correct |
19 ms |
31692 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
31564 KB |
Output is correct |
2 |
Correct |
15 ms |
31588 KB |
Output is correct |
3 |
Correct |
16 ms |
31548 KB |
Output is correct |
4 |
Correct |
17 ms |
31552 KB |
Output is correct |
5 |
Correct |
15 ms |
31564 KB |
Output is correct |
6 |
Correct |
17 ms |
31564 KB |
Output is correct |
7 |
Correct |
17 ms |
31644 KB |
Output is correct |
8 |
Correct |
18 ms |
31564 KB |
Output is correct |
9 |
Correct |
14 ms |
31564 KB |
Output is correct |
10 |
Correct |
15 ms |
31608 KB |
Output is correct |
11 |
Correct |
16 ms |
31568 KB |
Output is correct |
12 |
Correct |
16 ms |
31564 KB |
Output is correct |
13 |
Correct |
16 ms |
31612 KB |
Output is correct |
14 |
Correct |
16 ms |
31564 KB |
Output is correct |
15 |
Correct |
15 ms |
31596 KB |
Output is correct |
16 |
Correct |
15 ms |
31640 KB |
Output is correct |
17 |
Correct |
18 ms |
31692 KB |
Output is correct |
18 |
Correct |
20 ms |
31692 KB |
Output is correct |
19 |
Correct |
19 ms |
31692 KB |
Output is correct |
20 |
Correct |
19 ms |
31692 KB |
Output is correct |
21 |
Correct |
23 ms |
31628 KB |
Output is correct |
22 |
Correct |
18 ms |
31692 KB |
Output is correct |
23 |
Correct |
18 ms |
31680 KB |
Output is correct |
24 |
Correct |
19 ms |
31692 KB |
Output is correct |
25 |
Correct |
46 ms |
32516 KB |
Output is correct |
26 |
Correct |
105 ms |
34236 KB |
Output is correct |
27 |
Correct |
296 ms |
38188 KB |
Output is correct |
28 |
Correct |
315 ms |
42160 KB |
Output is correct |
29 |
Correct |
209 ms |
37068 KB |
Output is correct |
30 |
Correct |
349 ms |
42120 KB |
Output is correct |
31 |
Correct |
436 ms |
42116 KB |
Output is correct |
32 |
Correct |
406 ms |
42116 KB |
Output is correct |
33 |
Correct |
73 ms |
33440 KB |
Output is correct |
34 |
Correct |
221 ms |
37044 KB |
Output is correct |
35 |
Correct |
323 ms |
42160 KB |
Output is correct |
36 |
Correct |
452 ms |
42184 KB |
Output is correct |
37 |
Correct |
412 ms |
42068 KB |
Output is correct |
38 |
Correct |
404 ms |
42080 KB |
Output is correct |