#include<bits/stdc++.h>
#include<fstream>
#pragma GCC optimize("Ofast,O3,unroll-loops")
#pragma GCC target("avx2")
using namespace std;
//ifstream fin("FEEDING.INP");
//ofstream fout("FEEDING.OUT");
#define sz(a) (int)a.size()
#define ll long long
#define pb push_back
#define forr(i, a, b) for(int i = a; i < b; i++)
#define dorr(i, a, b) for(int i = a; i >= b; i--)
#define ld long double
#define vt vector
#include<fstream>
#define fi first
#define se second
#define pll pair<ll, ll>
#define pii pair<int, int>
const int base = 74;
const ll mod = 1e9 + 7, inf = 1e9, mxv = 1005, mxn = 5e5 + 5;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
int m, n;
vt<ll>comp;
ll st[4 * mxn + 1], a[mxn + 1], b[mxn + 1], c[mxn + 1], d[mxn + 1], dpl[mxn + 1], dpr[mxn + 1];
int findl(ll x){
return(lower_bound(comp.begin(), comp.end(), x) - comp.begin());
}
int findr(int x){
return(upper_bound(comp.begin(), comp.end(), x) - comp.begin() - 1);
}
void upd(int nd, int l, int r, int id, ll v){
if(id < l || id > r)return;
if(l == r){
st[nd] = min(st[nd], v);
return;
}
int mid = (l + r) >> 1;
upd(nd << 1, l, mid, id, v); upd(nd << 1 | 1, mid + 1, r, id, v);
st[nd] = min(st[nd << 1], st[nd << 1 | 1]);
}
ll get(int nd, int l, int r, int ql, int qr){
if(ql > r | qr < l)return(inf);
if(ql <= l && qr >= r)return(st[nd]);
int mid = (l + r) >> 1;
return(min(get(nd << 1, l, mid, ql, qr), get(nd << 1 | 1, mid + 1, r, ql, qr)));
}
signed main()
{
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin >> m >> n;
for(int i = 1; i <= m; i++){
cin >> a[i] >> b[i] >> c[i] >> d[i];
comp.pb(c[i]);
}
sort(comp.begin(), comp.end());
for(int i = 1; i <= 4 * sz(comp); i++)st[i] = inf;
for(int i = 1; i <= m; i++){
if(a[i] == 1){
dpl[i] = d[i];
}else{
dpl[i] = min(inf, get(1, 0, sz(comp) - 1, findl(a[i]), findr(b[i]))) + d[i];
}
if(dpl[i] != inf){
upd(1, 0, sz(comp) - 1, findl(c[i]), dpl[i]);
}
}
for(int i = 1; i <= 4 * sz(comp); i++)st[i] = inf;
for(int i = 1; i <= m; i++){
if(b[i] == n){
dpr[i] = d[i];
}else{
dpr[i] = min(inf, get(1, 0, sz(comp) - 1, findl(a[i]), findr(b[i])) + d[i]);
}
if(dpr[i] != inf){
upd(1, 0, sz(comp) - 1, findl(c[i]), dpr[i]);
}
}
ll ans = inf;
for(int i = 1; i <= m; i++){
if(dpl[i] != inf && dpr[i] != inf){
ans = min(ans, dpl[i] + dpr[i] - d[i]);
}
}
cout << ((ans == inf) ? -1 : ans);
return(0);
}
Compilation message
pinball.cpp: In function 'long long int get(int, int, int, int, int)':
pinball.cpp:45:11: warning: suggest parentheses around comparison in operand of '|' [-Wparentheses]
45 | if(ql > r | qr < l)return(inf);
| ~~~^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
0 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Incorrect |
0 ms |
340 KB |
Output isn't correct |
6 |
Halted |
0 ms |
0 KB |
- |