#include <bits/stdc++.h>
#include <ext/pb_ds/tree_policy.hpp>
#include <ext/pb_ds/assoc_container.hpp>
using namespace std;
using namespace __gnu_pbds;
typedef long long ll;
typedef long double ld;
typedef complex<ld> cd;
typedef pair<int, int> pi;
typedef pair<ll,ll> pl;
typedef pair<ld,ld> pd;
typedef vector<int> vi;
typedef vector<ld> vd;
typedef vector<ll> vl;
typedef vector<pi> vpi;
typedef vector<pl> vpl;
typedef vector<cd> vcd;
template <class T> using Tree = tree<T, null_type, less<T>, rb_tree_tag,tree_order_statistics_node_update>;
#define FOR(i, a, b) for (int i=a; i<(b); i++)
#define F0R(i, a) for (int i=0; i<(a); i++)
#define FORd(i,a,b) for (int i = (b)-1; i >= a; i--)
#define F0Rd(i,a) for (int i = (a)-1; i >= 0; i--)
#define sz(x) (int)(x).size()
#define mp make_pair
#define pb push_back
#define f first
#define s second
#define lb lower_bound
#define ub upper_bound
#define all(x) x.begin(), x.end()
const int MOD = 1000000007;
const ll INF = 1e18;
const int MX = 100001;
template<class T, int SZ> struct Seg {
T seg[2*SZ], MN = INF;
Seg() {
F0R(i,2*SZ) seg[i] = INF;
}
T comb(T a, T b) { return min(a,b); } // easily change this to min or max
void upd(int p, T value) { // set value at position p
for (seg[p += SZ] = value; p > 1; p >>= 1)
seg[p>>1] = comb(seg[(p|1)^1],seg[p|1]); // non-commutative operations
}
void build() {
F0Rd(i,SZ) seg[i] = comb(seg[2*i],seg[2*i+1]);
}
T query(int l, int r) { // sum on interval [l, r]
T res1 = MN, res2 = MN; r++;
for (l += SZ, r += SZ; l < r; l >>= 1, r >>= 1) {
if (l&1) res1 = comb(res1,seg[l++]);
if (r&1) res2 = comb(seg[--r],res2);
}
return comb(res1,res2);
}
};
Seg<ll,1<<19> L, R;
int N,M;
map<int,int> m;
vector<vi> v;
void input() {
ios_base::sync_with_stdio(0); cin.tie(0);
cin >> M >> N;
m[1] = 0;
m[N+1] = 0;
F0R(i,M) {
int A,B,C,D; cin >> A >> B >> C >> D;
v.pb({A,B,C,D});
m[A] = m[B+1] = m[C] = 0;
}
int co = 0;
for (auto& a: m) a.s = co++;
L.upd(0,0);
R.upd(m[N+1]-1,0);
}
int main() {
input();
ll ans = INF;
// cout << L.query(1,1) << "\n";
for (auto a: v) {
int l = m[a[0]], r = m[a[1]+1]-1;
int mid = m[a[2]];
// cout << l << " " << mid << " " << r << "\n";
ans = min(ans,a[3]+L.query(l,r)+R.query(l,r));
L.upd(mid,min(L.query(l,r)+a[3],L.query(mid,mid)));
R.upd(mid,min(R.query(l,r)+a[3],R.query(mid,mid)));
}
if (ans == INF) cout << -1;
else cout << ans;
}
/* Look for:
* the exact constraints (multiple sets are too slow for n=10^6 :( )
* special cases (n=1?)
* overflow (ll vs int?)
* array bounds
*/
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
16760 KB |
Output is correct |
2 |
Correct |
15 ms |
16760 KB |
Output is correct |
3 |
Correct |
18 ms |
16960 KB |
Output is correct |
4 |
Correct |
17 ms |
16960 KB |
Output is correct |
5 |
Correct |
18 ms |
16960 KB |
Output is correct |
6 |
Correct |
15 ms |
17020 KB |
Output is correct |
7 |
Correct |
15 ms |
17020 KB |
Output is correct |
8 |
Correct |
14 ms |
17020 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
16760 KB |
Output is correct |
2 |
Correct |
15 ms |
16760 KB |
Output is correct |
3 |
Correct |
18 ms |
16960 KB |
Output is correct |
4 |
Correct |
17 ms |
16960 KB |
Output is correct |
5 |
Correct |
18 ms |
16960 KB |
Output is correct |
6 |
Correct |
15 ms |
17020 KB |
Output is correct |
7 |
Correct |
15 ms |
17020 KB |
Output is correct |
8 |
Correct |
14 ms |
17020 KB |
Output is correct |
9 |
Correct |
19 ms |
17020 KB |
Output is correct |
10 |
Correct |
17 ms |
17020 KB |
Output is correct |
11 |
Correct |
19 ms |
17020 KB |
Output is correct |
12 |
Correct |
21 ms |
17056 KB |
Output is correct |
13 |
Correct |
19 ms |
17060 KB |
Output is correct |
14 |
Correct |
18 ms |
17068 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
16760 KB |
Output is correct |
2 |
Correct |
15 ms |
16760 KB |
Output is correct |
3 |
Correct |
18 ms |
16960 KB |
Output is correct |
4 |
Correct |
17 ms |
16960 KB |
Output is correct |
5 |
Correct |
18 ms |
16960 KB |
Output is correct |
6 |
Correct |
15 ms |
17020 KB |
Output is correct |
7 |
Correct |
15 ms |
17020 KB |
Output is correct |
8 |
Correct |
14 ms |
17020 KB |
Output is correct |
9 |
Correct |
19 ms |
17020 KB |
Output is correct |
10 |
Correct |
17 ms |
17020 KB |
Output is correct |
11 |
Correct |
19 ms |
17020 KB |
Output is correct |
12 |
Correct |
21 ms |
17056 KB |
Output is correct |
13 |
Correct |
19 ms |
17060 KB |
Output is correct |
14 |
Correct |
18 ms |
17068 KB |
Output is correct |
15 |
Correct |
17 ms |
17112 KB |
Output is correct |
16 |
Correct |
17 ms |
17128 KB |
Output is correct |
17 |
Correct |
18 ms |
17220 KB |
Output is correct |
18 |
Correct |
19 ms |
17260 KB |
Output is correct |
19 |
Correct |
22 ms |
17300 KB |
Output is correct |
20 |
Correct |
19 ms |
17344 KB |
Output is correct |
21 |
Correct |
19 ms |
17360 KB |
Output is correct |
22 |
Correct |
19 ms |
17524 KB |
Output is correct |
23 |
Correct |
19 ms |
17576 KB |
Output is correct |
24 |
Correct |
18 ms |
17592 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
16 ms |
16760 KB |
Output is correct |
2 |
Correct |
15 ms |
16760 KB |
Output is correct |
3 |
Correct |
18 ms |
16960 KB |
Output is correct |
4 |
Correct |
17 ms |
16960 KB |
Output is correct |
5 |
Correct |
18 ms |
16960 KB |
Output is correct |
6 |
Correct |
15 ms |
17020 KB |
Output is correct |
7 |
Correct |
15 ms |
17020 KB |
Output is correct |
8 |
Correct |
14 ms |
17020 KB |
Output is correct |
9 |
Correct |
19 ms |
17020 KB |
Output is correct |
10 |
Correct |
17 ms |
17020 KB |
Output is correct |
11 |
Correct |
19 ms |
17020 KB |
Output is correct |
12 |
Correct |
21 ms |
17056 KB |
Output is correct |
13 |
Correct |
19 ms |
17060 KB |
Output is correct |
14 |
Correct |
18 ms |
17068 KB |
Output is correct |
15 |
Correct |
17 ms |
17112 KB |
Output is correct |
16 |
Correct |
17 ms |
17128 KB |
Output is correct |
17 |
Correct |
18 ms |
17220 KB |
Output is correct |
18 |
Correct |
19 ms |
17260 KB |
Output is correct |
19 |
Correct |
22 ms |
17300 KB |
Output is correct |
20 |
Correct |
19 ms |
17344 KB |
Output is correct |
21 |
Correct |
19 ms |
17360 KB |
Output is correct |
22 |
Correct |
19 ms |
17524 KB |
Output is correct |
23 |
Correct |
19 ms |
17576 KB |
Output is correct |
24 |
Correct |
18 ms |
17592 KB |
Output is correct |
25 |
Correct |
59 ms |
18936 KB |
Output is correct |
26 |
Correct |
165 ms |
22228 KB |
Output is correct |
27 |
Correct |
522 ms |
29568 KB |
Output is correct |
28 |
Correct |
213 ms |
30484 KB |
Output is correct |
29 |
Correct |
377 ms |
32896 KB |
Output is correct |
30 |
Correct |
321 ms |
36604 KB |
Output is correct |
31 |
Correct |
722 ms |
45980 KB |
Output is correct |
32 |
Correct |
710 ms |
45980 KB |
Output is correct |
33 |
Correct |
91 ms |
45980 KB |
Output is correct |
34 |
Correct |
431 ms |
45980 KB |
Output is correct |
35 |
Correct |
367 ms |
55684 KB |
Output is correct |
36 |
Correct |
901 ms |
55736 KB |
Output is correct |
37 |
Correct |
864 ms |
55736 KB |
Output is correct |
38 |
Correct |
726 ms |
55736 KB |
Output is correct |