#pragma GCC optimize("Ofast,O2,O3,unroll-loops")
#pragma GCC target("avx2")
#include <bits/stdc++.h>
#include <random>
using namespace std;
void debug_out() { cerr << endl; }
template<typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << "[" << H << "]";
debug_out(T...);
}
#ifdef dddxxz
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif
#define SZ(s) ((int)s.size())
#define all(x) (x).begin(), (x).end()
#define lla(x) (x).rbegin(), (x).rend()
#define bpc(x) __builtin_popcount(x)
#define bpcll(x) __builtin_popcountll(x)
clock_t startTime;
double getCurrentTime() {
return (double) (clock() - startTime) / CLOCKS_PER_SEC;
}
#define MP make_pair
typedef long long ll;
mt19937 rng(chrono::high_resolution_clock::now().time_since_epoch().count());
const double eps = 0.000001;
const int MOD = 1e9 + 7;
const int INF = 1000000101;
const long long LLINF = 1223372000000000555;
const int N = 2e6 + 3e2;
const int M = 1111;
struct SegTree{
int size = 1;
vector<ll> t;
SegTree(int n){
t.assign(4 * n + 5, LLINF);
size = n;
}
void update(int v, int tl, int tr, int pos, ll val){
if (tl == tr){
t[v] = min(t[v], val);
return;
}
int tm = (tl + tr) >> 1;
if (pos <= tm) update(v << 1, tl, tm, pos, val); else
update(v << 1 | 1, tm + 1, tr, pos, val);
t[v] = min(t[v << 1], t[v << 1 | 1]);
}
void update(int pos, ll val){
update(1, 1, size, pos, val);
}
ll get(int v, int tl, int tr, int l, int r){
if (l <= tl && tr <= r) return t[v];
if (tl > r || tr < l) return LLINF;
int tm = (tl + tr) >> 1;
return min(get(v << 1, tl, tm, l, r), get(v << 1 | 1, tm + 1, tr, l, r));
}
ll get(int l, int r){
return get(1, 1, size, l, r);
}
};
long long pinball(int m, int n, vector<int> a, vector<int> b, vector<int> c, vector<int> d) {
map<int, int> mp;
mp[1] = mp[n] = 0;
for (int i = 0; i < m; i++){
mp[a[i]] = mp[b[i]] = mp[c[i]] = 0;
}
n = 0;
for (auto &now : mp) now.second = ++n;
for (int i = 0; i < m; i++){
a[i] = mp[a[i]];
b[i] = mp[b[i]];
c[i] = mp[c[i]];
}
vector<ll> dp1(m, LLINF), dp2(m, LLINF);
SegTree st1(n), st2(n);
ll ans = LLINF;
for (int i = 0; i < m; i++){
if (a[i] == 1){
dp1[i] = d[i];
} else {
dp1[i] = st1.get(a[i], b[i]) + d[i];
}
if (b[i] == n){
dp2[i] = d[i];
} else {
dp2[i] = st2.get(a[i], b[i]) + d[i];
}
debug(dp1[i], dp2[i]);
ans = min(ans, dp1[i] + dp2[i] - d[i]);
st1.update(c[i], dp1[i]);
st2.update(c[i], dp2[i]);
}
if (ans > 1e16) ans = -1;
return ans;
}
void solve(int TC) {
int m, n;
cin >> m >> n;
vector<int> a(m), b(m), c(m), d(m);
for (int i = 0; i < m; i++) cin >> a[i] >> b[i] >> c[i] >> d[i];
cout << pinball(m, n, a, b, c, d) << endl;
}
int main() {
startTime = clock();
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
#ifdef dddxxz
freopen("input.txt", "r", stdin);
freopen("output.txt", "w", stdout);
#endif
int TC = 1;
// cin >> TC;
for (int test = 1; test <= TC; test++) {
//debug(test);
//cout << "Case #" << test << ": ";
solve(test);
}
#ifdef dddxxz
cerr << endl << "Time: " << int(getCurrentTime() * 1000) << " ms" << endl;
#endif
return 0;
}
Compilation message
pinball.cpp: In function 'long long int pinball(int, int, std::vector<int>, std::vector<int>, std::vector<int>, std::vector<int>)':
pinball.cpp:21:20: warning: statement has no effect [-Wunused-value]
21 | #define debug(...) 42
| ^~
pinball.cpp:116:9: note: in expansion of macro 'debug'
116 | debug(dp1[i], dp2[i]);
| ^~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 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 |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 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 |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
2 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
2 ms |
596 KB |
Output is correct |
20 |
Correct |
2 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
2 ms |
596 KB |
Output is correct |
23 |
Correct |
2 ms |
668 KB |
Output is correct |
24 |
Correct |
2 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 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 |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
2 ms |
468 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
2 ms |
596 KB |
Output is correct |
20 |
Correct |
2 ms |
468 KB |
Output is correct |
21 |
Correct |
1 ms |
468 KB |
Output is correct |
22 |
Correct |
2 ms |
596 KB |
Output is correct |
23 |
Correct |
2 ms |
668 KB |
Output is correct |
24 |
Correct |
2 ms |
596 KB |
Output is correct |
25 |
Correct |
21 ms |
2644 KB |
Output is correct |
26 |
Correct |
65 ms |
7696 KB |
Output is correct |
27 |
Correct |
220 ms |
15656 KB |
Output is correct |
28 |
Correct |
128 ms |
5996 KB |
Output is correct |
29 |
Correct |
150 ms |
13564 KB |
Output is correct |
30 |
Correct |
204 ms |
7988 KB |
Output is correct |
31 |
Correct |
396 ms |
24400 KB |
Output is correct |
32 |
Correct |
306 ms |
19104 KB |
Output is correct |
33 |
Correct |
48 ms |
8268 KB |
Output is correct |
34 |
Correct |
167 ms |
19904 KB |
Output is correct |
35 |
Correct |
191 ms |
38740 KB |
Output is correct |
36 |
Correct |
507 ms |
38824 KB |
Output is correct |
37 |
Correct |
376 ms |
38780 KB |
Output is correct |
38 |
Correct |
324 ms |
38672 KB |
Output is correct |