#include <bits/stdc++.h>
#define mp make_pair
#define pb push_back
#define X first
#define Y second
#define y0 y12
#define y1 y22
#define INF 987654321987654321
#define PI 3.141592653589793238462643383279502884
#define fup(i,a,b,c) for(int (i)=(a);(i)<=(b);(i)+=(c))
#define fdn(i,a,b,c) for(int (i)=(a);(i)>=(b);(i)-=(c))
#define MEM0(a) memset((a),0,sizeof(a))
#define MEM_1(a) memset((a),-1,sizeof(a))
#define ALL(a) a.begin(),a.end()
#define COMPRESS(a) sort(ALL(a));a.resize(unique(ALL(a))-a.begin())
#define SYNC ios_base::sync_with_stdio(false);cin.tie(0)
using namespace std;
typedef long long ll;
typedef long double ld;
typedef double db;
typedef unsigned int uint;
typedef unsigned long long ull;
typedef pair<int, int> Pi;
typedef pair<ll, ll> Pll;
typedef pair<ld, ld> Pd;
typedef vector<int> Vi;
typedef vector<ll> Vll;
typedef vector<ld> Vd;
typedef vector<Pi> VPi;
typedef vector<Pll> VPll;
typedef vector<Pd> VPd;
typedef tuple<int, int, int> iii;
typedef tuple<int, int, int, int> iiii;
typedef tuple<ll, ll, ll> LLL;
typedef vector<iii> Viii;
typedef vector<LLL> VLLL;
typedef complex<double> base;
const int MOD = 998244353;
ll POW(ll a, ll b, ll MMM = MOD) { ll ret = 1; for (; b; b >>= 1, a = (a*a) % MMM)if (b & 1)ret = (ret*a) % MMM; return ret; }
int dx[] = { 0,1,0,-1,1,1,-1,-1 }, dy[] = { 1,0,-1,0,1,-1,1,-1 };
int ddx[] = { -1,-2,1,-2,2,-1,2,1 }, ddy[] = { -2,-1,-2,1,-1,2,1,2 };
ll tree1[1048576],tree2[1048576];
void init(ll *tree,int node,int S,int E){
tree[node]=INF;
if(S==E)return;
int L=node<<1,R=L|1,M=S+E>>1;
init(tree,L,S,M);
init(tree,R,M+1,E);
}
void upd(ll *tree,int node,int S,int E,int k,ll dif){
if(S==E){
tree[node]=min(tree[node],dif);
return;
}
int L=node<<1,R=L|1,M=S+E>>1;
if(k<=M)upd(tree,L,S,M,k,dif);
else upd(tree,R,M+1,E,k,dif);
tree[node]=min(tree[L],tree[R]);
}
ll find(ll *tree,int node,int S,int E,int i,int j){
if(i>E || j<S)return INF;
if(i<=S && j>=E)return tree[node];
int L=node<<1,R=L|1,M=S+E>>1;
return min(find(tree,L,S,M,i,j),find(tree,R,M+1,E,i,j));
}
int a[100000],b[100000],c[100000],d[100000];
int main(){
int n,m;
scanf("%d%d",&n,&m);
Vi vv={1,m};
fup(i,0,n-1,1){
scanf("%d%d%d%d",a+i,b+i,c+i,d+i);
vv.pb(a[i]);vv.pb(b[i]);vv.pb(c[i]);
}
COMPRESS(vv);
fup(i,0,n-1,1)a[i]=lower_bound(ALL(vv),a[i])-vv.begin(),b[i]=lower_bound(ALL(vv),b[i])-vv.begin(),c[i]=lower_bound(ALL(vv),c[i])-vv.begin();
int N=vv.size();
init(tree1,1,0,N-1);init(tree2,1,0,N-1);
upd(tree1,1,0,N-1,0,0);upd(tree2,1,0,N-1,N-1,0);
ll ans=INF;
fup(i,0,n-1,1){
ll t1=find(tree1,1,0,N-1,a[i],b[i]),t2=find(tree2,1,0,N-1,a[i],b[i]);
ans=min(ans,t1+t2+d[i]);
upd(tree1,1,0,N-1,c[i],t1+d[i]);
upd(tree2,1,0,N-1,c[i],t2+d[i]);
}
printf("%lld\n",ans==INF?-1:ans);
}
Compilation message
pinball.cpp: In function 'void init(ll*, int, int, int)':
pinball.cpp:47:25: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
47 | int L=node<<1,R=L|1,M=S+E>>1;
| ~^~
pinball.cpp: In function 'void upd(ll*, int, int, int, int, ll)':
pinball.cpp:56:25: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
56 | int L=node<<1,R=L|1,M=S+E>>1;
| ~^~
pinball.cpp: In function 'll find(ll*, int, int, int, int, int)':
pinball.cpp:65:25: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
65 | int L=node<<1,R=L|1,M=S+E>>1;
| ~^~
pinball.cpp: In function 'int main()':
pinball.cpp:10:30: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
10 | #define fup(i,a,b,c) for(int (i)=(a);(i)<=(b);(i)+=(c))
| ^
pinball.cpp:73:2: note: in expansion of macro 'fup'
73 | fup(i,0,n-1,1){
| ^~~
pinball.cpp:10:30: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
10 | #define fup(i,a,b,c) for(int (i)=(a);(i)<=(b);(i)+=(c))
| ^
pinball.cpp:78:2: note: in expansion of macro 'fup'
78 | fup(i,0,n-1,1)a[i]=lower_bound(ALL(vv),a[i])-vv.begin(),b[i]=lower_bound(ALL(vv),b[i])-vv.begin(),c[i]=lower_bound(ALL(vv),c[i])-vv.begin();
| ^~~
pinball.cpp:10:30: warning: unnecessary parentheses in declaration of 'i' [-Wparentheses]
10 | #define fup(i,a,b,c) for(int (i)=(a);(i)<=(b);(i)+=(c))
| ^
pinball.cpp:83:2: note: in expansion of macro 'fup'
83 | fup(i,0,n-1,1){
| ^~~
pinball.cpp:71:7: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
71 | scanf("%d%d",&n,&m);
| ~~~~~^~~~~~~~~~~~~~
pinball.cpp:74:8: warning: ignoring return value of 'int scanf(const char*, ...)' declared with attribute 'warn_unused_result' [-Wunused-result]
74 | scanf("%d%d%d%d",a+i,b+i,c+i,d+i);
| ~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 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 |
0 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 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 |
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 |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
316 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 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 |
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 |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
316 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
2 ms |
444 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
2 ms |
468 KB |
Output is correct |
20 |
Correct |
2 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
3 ms |
444 KB |
Output is correct |
23 |
Correct |
2 ms |
468 KB |
Output is correct |
24 |
Correct |
2 ms |
496 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 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 |
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 |
340 KB |
Output is correct |
11 |
Correct |
1 ms |
340 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
316 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
0 ms |
340 KB |
Output is correct |
16 |
Correct |
1 ms |
340 KB |
Output is correct |
17 |
Correct |
2 ms |
444 KB |
Output is correct |
18 |
Correct |
1 ms |
340 KB |
Output is correct |
19 |
Correct |
2 ms |
468 KB |
Output is correct |
20 |
Correct |
2 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
3 ms |
444 KB |
Output is correct |
23 |
Correct |
2 ms |
468 KB |
Output is correct |
24 |
Correct |
2 ms |
496 KB |
Output is correct |
25 |
Correct |
16 ms |
1928 KB |
Output is correct |
26 |
Correct |
45 ms |
3984 KB |
Output is correct |
27 |
Correct |
149 ms |
8964 KB |
Output is correct |
28 |
Correct |
139 ms |
7360 KB |
Output is correct |
29 |
Correct |
92 ms |
7736 KB |
Output is correct |
30 |
Correct |
160 ms |
7964 KB |
Output is correct |
31 |
Correct |
223 ms |
15336 KB |
Output is correct |
32 |
Correct |
183 ms |
11044 KB |
Output is correct |
33 |
Correct |
29 ms |
3648 KB |
Output is correct |
34 |
Correct |
109 ms |
11928 KB |
Output is correct |
35 |
Correct |
156 ms |
23456 KB |
Output is correct |
36 |
Correct |
288 ms |
23356 KB |
Output is correct |
37 |
Correct |
218 ms |
23368 KB |
Output is correct |
38 |
Correct |
200 ms |
23380 KB |
Output is correct |