/*
IN THE NAME OF GOD
*/
#include <bits/stdc++.h>
// #pragma GCC target("avx2,bmi,bmi2,lzcnt,popcnt")
// #pragma GCC optimize("O3")
// #pragma GCC optimize("unroll-loops")
using namespace std;
typedef long long ll;
typedef pair<ll, ll> pll;
typedef pair<int, int> pii;
typedef long double ld;
#define F first
#define S second
#define Mp make_pair
#define pb push_back
#define pf push_front
#define size(x) ((ll)x.size())
#define all(x) (x).begin(),(x).end()
#define kill(x) cout << x << '\n', exit(0);
#define fuck(x) cout << "(" << #x << " , " << x << ")" << endl
#define endl '\n'
const int N = 3e5+23, lg = 20;
ll Mod = 1e9+7; //998244353;
inline ll MOD(ll a, ll mod=Mod) {a%=mod; (a<0)&&(a+=mod); return a;}
inline ll poww(ll a, ll b, ll mod=Mod) {
ll ans = 1;
a=MOD(a, mod);
while (b) {
if (b & 1) ans = MOD(ans*a, mod);
b >>= 1;
a = MOD(a*a, mod);
}
return ans;
}
ll n, m, a[N], b[N], c[N], d[N], dp[2][N], seg[2][8*N];
vector<ll> comp;
void update(int ind, ll val, int id) {
if(ind == 0) return;
if(ind >= (1<<lg)) seg[id][ind] = min(val, seg[id][ind]);
else seg[id][ind] = min(seg[id][2*ind], seg[id][2*ind+1]);
update(ind/2, val, id);
}
ll query(int l, int r, int id, int ind=1, int lc=1, int rc=(1<<lg)+1) {
if(l>=rc || lc>=r) return (ll)1e18;
if(lc>=l && rc<=r) return seg[id][ind];
int mid = (lc+rc)/2;
return min(query(l, r, id, 2*ind, lc, mid), query(l, r, id, 2*ind+1, mid, rc));
}
ll get(ll val) {
int ptr = lower_bound(all(comp), val) - comp.begin() + 1;
return ptr;
}
int main () {
ios_base::sync_with_stdio(false), cin.tie(0);
cin>>m>>n;
comp.pb(n); comp.pb(n+1);
comp.pb(1);
for(int i=1; i<=m; i++) {
cin>>a[i]>>b[i]>>c[i]>>d[i];
comp.pb(a[i]); comp.pb(b[i]+1); comp.pb(c[i]);
}
sort(all(comp));
comp.resize(unique(all(comp)) - comp.begin());
fill(seg[0], seg[0]+8*N, 1e18);
fill(seg[1], seg[1]+8*N, 1e18);
update((1<<lg), 0, 0);
update(size(comp)+(1<<lg)-2, 0, 1);
ll ans = 1e18;
for(int i=1; i<=m; i++) {
dp[0][i] = min(query(get(a[i]), get(b[i]+1), 0) + d[i], (ll)1e18);
dp[1][i] = min(query(get(a[i]), get(b[i]+1), 1) + d[i], (ll)1e18);
if(dp[0][i] != 1e18 && dp[1][i] != 1e18) {
ans = min(ans, dp[0][i] + dp[1][i] - d[i]);
}
update(get(c[i])+(1<<lg)-1, dp[0][i], 0);
update(get(c[i])+(1<<lg)-1, dp[1][i], 1);
}
cout<<(ans==1e18 ? -1 : ans)<<endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
49752 KB |
Output is correct |
2 |
Correct |
8 ms |
49752 KB |
Output is correct |
3 |
Correct |
8 ms |
49752 KB |
Output is correct |
4 |
Correct |
7 ms |
49752 KB |
Output is correct |
5 |
Correct |
8 ms |
49884 KB |
Output is correct |
6 |
Correct |
8 ms |
49832 KB |
Output is correct |
7 |
Correct |
7 ms |
49756 KB |
Output is correct |
8 |
Correct |
7 ms |
49756 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
49752 KB |
Output is correct |
2 |
Correct |
8 ms |
49752 KB |
Output is correct |
3 |
Correct |
8 ms |
49752 KB |
Output is correct |
4 |
Correct |
7 ms |
49752 KB |
Output is correct |
5 |
Correct |
8 ms |
49884 KB |
Output is correct |
6 |
Correct |
8 ms |
49832 KB |
Output is correct |
7 |
Correct |
7 ms |
49756 KB |
Output is correct |
8 |
Correct |
7 ms |
49756 KB |
Output is correct |
9 |
Correct |
7 ms |
49756 KB |
Output is correct |
10 |
Correct |
9 ms |
49884 KB |
Output is correct |
11 |
Correct |
7 ms |
49752 KB |
Output is correct |
12 |
Correct |
7 ms |
49876 KB |
Output is correct |
13 |
Correct |
7 ms |
49756 KB |
Output is correct |
14 |
Correct |
7 ms |
49900 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
49752 KB |
Output is correct |
2 |
Correct |
8 ms |
49752 KB |
Output is correct |
3 |
Correct |
8 ms |
49752 KB |
Output is correct |
4 |
Correct |
7 ms |
49752 KB |
Output is correct |
5 |
Correct |
8 ms |
49884 KB |
Output is correct |
6 |
Correct |
8 ms |
49832 KB |
Output is correct |
7 |
Correct |
7 ms |
49756 KB |
Output is correct |
8 |
Correct |
7 ms |
49756 KB |
Output is correct |
9 |
Correct |
7 ms |
49756 KB |
Output is correct |
10 |
Correct |
9 ms |
49884 KB |
Output is correct |
11 |
Correct |
7 ms |
49752 KB |
Output is correct |
12 |
Correct |
7 ms |
49876 KB |
Output is correct |
13 |
Correct |
7 ms |
49756 KB |
Output is correct |
14 |
Correct |
7 ms |
49900 KB |
Output is correct |
15 |
Correct |
8 ms |
49756 KB |
Output is correct |
16 |
Correct |
8 ms |
49888 KB |
Output is correct |
17 |
Correct |
9 ms |
49952 KB |
Output is correct |
18 |
Correct |
8 ms |
49756 KB |
Output is correct |
19 |
Correct |
8 ms |
49908 KB |
Output is correct |
20 |
Correct |
8 ms |
49756 KB |
Output is correct |
21 |
Correct |
8 ms |
49880 KB |
Output is correct |
22 |
Correct |
8 ms |
49752 KB |
Output is correct |
23 |
Correct |
8 ms |
49960 KB |
Output is correct |
24 |
Correct |
8 ms |
49988 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
15 ms |
49752 KB |
Output is correct |
2 |
Correct |
8 ms |
49752 KB |
Output is correct |
3 |
Correct |
8 ms |
49752 KB |
Output is correct |
4 |
Correct |
7 ms |
49752 KB |
Output is correct |
5 |
Correct |
8 ms |
49884 KB |
Output is correct |
6 |
Correct |
8 ms |
49832 KB |
Output is correct |
7 |
Correct |
7 ms |
49756 KB |
Output is correct |
8 |
Correct |
7 ms |
49756 KB |
Output is correct |
9 |
Correct |
7 ms |
49756 KB |
Output is correct |
10 |
Correct |
9 ms |
49884 KB |
Output is correct |
11 |
Correct |
7 ms |
49752 KB |
Output is correct |
12 |
Correct |
7 ms |
49876 KB |
Output is correct |
13 |
Correct |
7 ms |
49756 KB |
Output is correct |
14 |
Correct |
7 ms |
49900 KB |
Output is correct |
15 |
Correct |
8 ms |
49756 KB |
Output is correct |
16 |
Correct |
8 ms |
49888 KB |
Output is correct |
17 |
Correct |
9 ms |
49952 KB |
Output is correct |
18 |
Correct |
8 ms |
49756 KB |
Output is correct |
19 |
Correct |
8 ms |
49908 KB |
Output is correct |
20 |
Correct |
8 ms |
49756 KB |
Output is correct |
21 |
Correct |
8 ms |
49880 KB |
Output is correct |
22 |
Correct |
8 ms |
49752 KB |
Output is correct |
23 |
Correct |
8 ms |
49960 KB |
Output is correct |
24 |
Correct |
8 ms |
49988 KB |
Output is correct |
25 |
Correct |
23 ms |
50400 KB |
Output is correct |
26 |
Correct |
56 ms |
53496 KB |
Output is correct |
27 |
Correct |
144 ms |
56200 KB |
Output is correct |
28 |
Correct |
130 ms |
57952 KB |
Output is correct |
29 |
Correct |
99 ms |
54996 KB |
Output is correct |
30 |
Correct |
185 ms |
58056 KB |
Output is correct |
31 |
Correct |
201 ms |
59632 KB |
Output is correct |
32 |
Correct |
186 ms |
58812 KB |
Output is correct |
33 |
Correct |
34 ms |
53212 KB |
Output is correct |
34 |
Correct |
102 ms |
54916 KB |
Output is correct |
35 |
Correct |
137 ms |
58816 KB |
Output is correct |
36 |
Correct |
245 ms |
59312 KB |
Output is correct |
37 |
Correct |
201 ms |
58744 KB |
Output is correct |
38 |
Correct |
189 ms |
58556 KB |
Output is correct |