//thatsramen
#include <bits/stdc++.h>
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
#define eb emplace_back
#define pb push_back
#define ft first
#define sd second
#define pi pair<int, int>
#define pll pair<ll, ll>
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define dbg(...) dbg_out(__VA_ARGS__)
using ll = long long;
using ld = long double;
using namespace std;
using namespace __gnu_pbds;
//Constants
const ll INF = 5 * 1e18;
const int IINF = 2 * 1e9;
const ll MOD = 1e9 + 7;
// const ll MOD = 998244353;
const ll dx[4] = {1, 0, -1, 0}, dy[4] = {0, 1, 0, -1};
const ld PI = 3.14159265359;
//Templates
template<typename A, typename B> ostream& operator<<(ostream &os, const pair<A, B> &p) {return os << '(' << p.first << ", " << p.second << ')';}
template<typename T_container, typename T = typename enable_if<!is_same<T_container, string>::value, typename T_container::value_type>::type> ostream& operator<<(ostream &os, const T_container &v) {os << '['; string sep; for (const T &x : v) os << sep << x, sep = ", "; return os << ']';}
void dbg_out() {cerr << endl;}
template<typename Head, typename... Tail> void dbg_out(Head H, Tail... T) { cerr << H << ' '; dbg_out(T...); }
template<typename T> void mins(T& x, T y) {x = min(x, y);}
template<typename T> void maxs(T& x, T y) {x = max(x, y);}
template<typename T> using oset = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
template<typename T> using omset = tree<T, null_type, less_equal<T>, rb_tree_tag, tree_order_statistics_node_update>;
//order_of_key(k): number of elements strictly less than k
//find_by_order(k): k-th element in the set
void setPrec() {cout << fixed << setprecision(15);}
void unsyncIO() {cin.tie(0)->sync_with_stdio(0);}
void setIn(string s) {freopen(s.c_str(), "r", stdin);}
void setOut(string s) {freopen(s.c_str(), "w", stdout);}
void setIO(string s = "") {
unsyncIO(); setPrec();
if(s.size()) setIn(s + ".in"), setOut(s + ".out");
}
// #define TEST_CASES
const ll MAX = 2 * 1e18;
struct SegmentTree {
vector<ll> tre;
int sz;
SegmentTree(int n) {
sz = 1;
while (sz < n) sz *= 2;
tre.assign(2 * sz, MAX);
// build(a, 1, 0, sz);
}
void rcl(int x) {
tre[x] = min(tre[2 * x], tre[2 * x + 1]);
}
void build(vector<int> &a, int x, int lx, int rx) {
if (rx - lx == 1) {
if (lx < a.size()) {
tre[x] = a[lx];
}
return;
}
int m = (lx + rx) / 2;
build(a, 2 * x, lx, m);
build(a, 2 * x + 1, m, rx);
rcl(x);
}
void set(int i, ll v, int x, int lx, int rx) {
if (rx - lx == 1) {
mins(tre[x], v);
return;
}
int m = (lx + rx) / 2;
if (i < m) {
set(i, v, 2 * x, lx, m);
} else {
set(i, v, 2 * x + 1, m, rx);
}
rcl(x);
}
void set(int i, ll v) {
set(i, v, 1, 0, sz);
}
ll get(int l, int r, int x, int lx, int rx) {
if (rx <= l || lx >= r) {
return MAX;
}
if (l <= lx && r >= rx) {
return tre[x];
}
int m = (lx + rx) / 2;
ll s1 = get(l, r, 2 * x, lx, m);
ll s2 = get(l, r, 2 * x + 1, m, rx);
return min(s1, s2);
}
ll get(int l, int r) {
return get(l, r, 1, 0, sz);
}
};
void solve() {
int m, n;
cin >> m >> n;
vector<int> a(m), b(m), c(m), d(m);
map<int, int> al, comp;
for (int i = 0; i < m; i++) {
cin >> a[i] >> b[i] >> c[i] >> d[i];
al[a[i]]++;
al[b[i]]++;
al[c[i]]++;
}
int pp = 0;
for (auto &[x, y] : al) comp[x] = pp++;
SegmentTree st_left(pp + 5), st_right(pp + 5);
ll ans = INF;
for (int i = 0; i < m; i++) {
ll need_left = st_left.get(comp[a[i]], comp[b[i]] + 1);
ll need_right = st_right.get(comp[a[i]], comp[b[i]] + 1);
if (a[i] == 1) need_left = 0;
if (b[i] == n) need_right = 0;
st_left.set(comp[c[i]], need_left + d[i]);
st_right.set(comp[c[i]], need_right + d[i]);
if (need_left == MAX) continue;
if (need_right == MAX) continue;
mins(ans, need_left + need_right + d[i]);
}
if (ans == INF) ans = -1;
cout << ans;
}
int main() {
setIO();
int tt = 1;
#ifdef TEST_CASES
cin >> tt;
#endif
while (tt--)
solve();
return 0;
}
Compilation message
pinball.cpp:5: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
5 | #pragma GCC optimization ("O3")
|
pinball.cpp:6: warning: ignoring '#pragma GCC optimization' [-Wunknown-pragmas]
6 | #pragma GCC optimization ("unroll-loops")
|
pinball.cpp: In member function 'void SegmentTree::build(std::vector<int>&, int, int, int)':
pinball.cpp:77:20: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
77 | if (lx < a.size()) {
| ~~~^~~~~~~~~~
pinball.cpp: In function 'void setIn(std::string)':
pinball.cpp:48:30: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
48 | void setIn(string s) {freopen(s.c_str(), "r", stdin);}
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
pinball.cpp: In function 'void setOut(std::string)':
pinball.cpp:49:31: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
49 | void setOut(string s) {freopen(s.c_str(), "w", stdout);}
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
320 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
320 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
384 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 |
320 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
320 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
384 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 |
320 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
2 ms |
340 KB |
Output is correct |
17 |
Correct |
3 ms |
468 KB |
Output is correct |
18 |
Correct |
2 ms |
340 KB |
Output is correct |
19 |
Correct |
3 ms |
724 KB |
Output is correct |
20 |
Correct |
3 ms |
468 KB |
Output is correct |
21 |
Correct |
3 ms |
468 KB |
Output is correct |
22 |
Correct |
5 ms |
724 KB |
Output is correct |
23 |
Correct |
3 ms |
724 KB |
Output is correct |
24 |
Correct |
3 ms |
712 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
320 KB |
Output is correct |
3 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
320 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
384 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 |
320 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
2 ms |
340 KB |
Output is correct |
17 |
Correct |
3 ms |
468 KB |
Output is correct |
18 |
Correct |
2 ms |
340 KB |
Output is correct |
19 |
Correct |
3 ms |
724 KB |
Output is correct |
20 |
Correct |
3 ms |
468 KB |
Output is correct |
21 |
Correct |
3 ms |
468 KB |
Output is correct |
22 |
Correct |
5 ms |
724 KB |
Output is correct |
23 |
Correct |
3 ms |
724 KB |
Output is correct |
24 |
Correct |
3 ms |
712 KB |
Output is correct |
25 |
Correct |
32 ms |
3412 KB |
Output is correct |
26 |
Correct |
113 ms |
8880 KB |
Output is correct |
27 |
Correct |
359 ms |
17576 KB |
Output is correct |
28 |
Correct |
166 ms |
5872 KB |
Output is correct |
29 |
Correct |
240 ms |
15584 KB |
Output is correct |
30 |
Correct |
268 ms |
8476 KB |
Output is correct |
31 |
Correct |
588 ms |
29728 KB |
Output is correct |
32 |
Correct |
535 ms |
21140 KB |
Output is correct |
33 |
Correct |
67 ms |
8800 KB |
Output is correct |
34 |
Correct |
308 ms |
25220 KB |
Output is correct |
35 |
Correct |
368 ms |
50284 KB |
Output is correct |
36 |
Correct |
768 ms |
50324 KB |
Output is correct |
37 |
Correct |
526 ms |
50376 KB |
Output is correct |
38 |
Correct |
484 ms |
50316 KB |
Output is correct |