/***Farhan132***/
//#pragma GCC optimize("Ofast")
//#pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,avx2,fma")
//#pragma GCC optimization ("unroll-loops")
//#pragma GCC optimize("Ofast,fast-math")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef double dd;
typedef vector<ll> vll;
typedef pair<ll , ll> ii;
typedef vector< ii > vii;
typedef pair < pair < ll , ll > , pair < ll , ll > > cm;
typedef tuple < ll, ll, ll > tp;
#define ff first
#define ss second
#define pb push_back
#define in insert
#define f0(b) for(int i=0;i<(b);i++)
#define f00(b) for(int j=0;j<(b);j++)
#define f1(b) for(int i=1;i<=(b);i++)
#define f11(b) for(int j=1;j<=(b);j++)
#define f2(a,b) for(int i=(a);i<=(b);i++)
#define f22(a,b) for(int j=(a);j<=(b);j++)
#define sf(a) scanf("%lld",&a)
#define sff(a,b) scanf("%lld %lld", &a , &b)
#define pf(a) printf("%lld\n",a)
#define pff(a,b) printf("%lld %lld\n", a , b)
#define bug printf("**!\n")
#define mem(a , b) memset(a, b ,sizeof(a))
#define front_zero(n) __builtin_clzll(n)
#define back_zero(n) __builtin_ctzll(n)
#define total_one(n) __builtin_popcount(n)
#define clean fflush(stdout)
const ll mod = (ll) 998244353;
//const ll mod = (ll) 1e9 + 7;
const ll maxn = (ll)1e8 + 5;
const int nnn = 1048590;
const int inf = numeric_limits<int>::max()-1;
//const ll INF = numeric_limits<ll>::max()-1;
const ll INF = (ll)1e18;
ll dx[]={0,1,0,-1};
ll dy[]={1,0,-1,0};
ll dxx[]={0,1,0,-1,1,1,-1,-1};
ll dyy[]={1,0,-1,0,1,-1,1,-1};
bool USACO = 0;
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
using namespace __gnu_pbds;
template <typename T> using ordered_set = tree<T, null_type, less<T>, rb_tree_tag, tree_order_statistics_node_update>;
const ll N = 4e5 + 5;
ll a[N] , b[N] , c[N] , d[N];
struct seg_tree{
struct typo{
ll mn;
void setup(ll v){
mn = v;
}
};
vector < typo > b;
ll n;
void init(ll l, ll r, ll node){
b[node].setup(INF);
return;
}
typo merge(typo a, typo b){
typo c;
c.mn = min(a.mn, b.mn);
return c;
}
void build(ll l, ll r, ll node){
if(l == r){
init(l,r,node);
return;
}
ll mid = (l + r)/2;
ll n1 = 2*node;
ll n2 = n1 + 1;
build(l, mid , n1);
build(mid+1, r, n2);
b[node] = merge(b[n1] , b[n2]);
return;
}
void build(ll _n){
n = _n;
b.resize(4*n);
build(1,n,1); // optional
}
void up(ll l, ll r, ll node, ll x, ll y, ll v){
if(y < l || x > r) return;
if(x <= l && y >= r){
b[node].mn = min(b[node].mn, v);
return;
}
ll mid = (l + r)/2;
ll n1 = 2*node;
ll n2 = n1 + 1;
up(l , mid , n1 , x , y , v);
up(mid + 1, r, n2, x, y , v);
b[node] = merge(b[n1] , b[n2]);
return;
}
void up(ll x, ll v){
up(1,n,1,x,x,v);
return;
}
typo get(ll l, ll r, ll node, ll x, ll y){
if(y < l || x > r) return {INF};
if(x <= l && y >= r){
return b[node];
}
ll mid = (l + r)/2;
ll n1 = 2*node;
ll n2 = n1 + 1;
return merge(get(l , mid , n1 , x , y) , get(mid + 1, r , n2 , x , y));
}
ll get(ll l, ll r){
return get(1,n,1,l,r).mn;
}
/* Final Check!!!
1. Typo, Okay?
2. Init Okay? (Build!!)
3. Merge, Okay?
4. Lazy Needed ? YES | NO (if NO, REMOVE anything related to lazy prop)
5. Dummy?
6. Check Update, Get, etc function's Merge, Lazy Prop , Init :D
7, Even after a WA, check for possible Out Of Bound/MLE/RE cases
*/
}pref, suf;
void solve(){
ll n , m;
cin >> m >> n;
vector < ll > idx;
idx.pb(1); idx.pb(n);
for(ll i = 1; i <= m; i++){
cin >> a[i] >> b[i] >> c[i] >> d[i];
idx.pb(a[i]) , idx.pb(b[i]) , idx.pb(c[i]);
}
sort(idx.begin(), idx.end());
idx.erase(unique(idx.begin(), idx.end()), idx.end());
n = idx.size();
pref.build(n);
suf.build(n);
ll ans = INF;
for(ll i = 1; i <= m; i++){
a[i] = lower_bound(idx.begin(), idx.end(), a[i]) - idx.begin() + 1;
b[i] = lower_bound(idx.begin(), idx.end(), b[i]) - idx.begin() + 1;
c[i] = lower_bound(idx.begin(), idx.end(), c[i]) - idx.begin() + 1;
ll x = INF, y = INF;
if(a[i] == 1) x = 0;
if(b[i] == n) y = 0;
x = min(x, pref.get(a[i], b[i]));
y = min(y, suf.get(a[i], b[i]));
ans = min(ans, x + y + d[i]);
pref.up(c[i], d[i] + x);
suf.up(c[i], d[i] + y);
}
if(ans == INF) ans = -1;
cout << ans << '\n';
return;
}
int main() {
#ifdef LOCAL
freopen("in", "r", stdin);
freopen("out", "w", stdout);
auto start_time = clock();
#else
ios_base::sync_with_stdio(0); cin.tie(NULL); cout.tie(NULL);
#endif
//pre(N);
ll T = 1, CT = 0; //cin >> T;
while(T--){
// cout << "Case #" << ++CT <<": " ;
solve();
}
#ifdef LOCAL
auto end_time = clock();
cerr<< "Execution time: "<<(end_time - start_time)*(int)1e3/CLOCKS_PER_SEC<<" ms\n";
#endif
return 0;
}
Compilation message
pinball.cpp: In function 'int main()':
pinball.cpp:214:15: warning: unused variable 'CT' [-Wunused-variable]
214 | ll T = 1, CT = 0; //cin >> T;
| ^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
376 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
376 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
432 KB |
Output is correct |
17 |
Correct |
2 ms |
468 KB |
Output is correct |
18 |
Correct |
2 ms |
468 KB |
Output is correct |
19 |
Correct |
2 ms |
600 KB |
Output is correct |
20 |
Correct |
2 ms |
468 KB |
Output is correct |
21 |
Correct |
2 ms |
468 KB |
Output is correct |
22 |
Correct |
3 ms |
596 KB |
Output is correct |
23 |
Correct |
2 ms |
596 KB |
Output is correct |
24 |
Correct |
2 ms |
596 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
0 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
344 KB |
Output is correct |
13 |
Correct |
1 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
376 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
432 KB |
Output is correct |
17 |
Correct |
2 ms |
468 KB |
Output is correct |
18 |
Correct |
2 ms |
468 KB |
Output is correct |
19 |
Correct |
2 ms |
600 KB |
Output is correct |
20 |
Correct |
2 ms |
468 KB |
Output is correct |
21 |
Correct |
2 ms |
468 KB |
Output is correct |
22 |
Correct |
3 ms |
596 KB |
Output is correct |
23 |
Correct |
2 ms |
596 KB |
Output is correct |
24 |
Correct |
2 ms |
596 KB |
Output is correct |
25 |
Correct |
21 ms |
2396 KB |
Output is correct |
26 |
Correct |
55 ms |
6252 KB |
Output is correct |
27 |
Correct |
164 ms |
13084 KB |
Output is correct |
28 |
Correct |
165 ms |
10896 KB |
Output is correct |
29 |
Correct |
121 ms |
10820 KB |
Output is correct |
30 |
Correct |
184 ms |
10892 KB |
Output is correct |
31 |
Correct |
289 ms |
20364 KB |
Output is correct |
32 |
Correct |
246 ms |
17408 KB |
Output is correct |
33 |
Correct |
38 ms |
5952 KB |
Output is correct |
34 |
Correct |
134 ms |
14484 KB |
Output is correct |
35 |
Correct |
171 ms |
28596 KB |
Output is correct |
36 |
Correct |
292 ms |
28604 KB |
Output is correct |
37 |
Correct |
257 ms |
28628 KB |
Output is correct |
38 |
Correct |
232 ms |
28596 KB |
Output is correct |