#include<bits/stdc++.h>
#define ll long long
#define f first
#define sc second
#define pb push_back
using namespace std;
ll a,b,c,d,i,e,f,g,n,m,k,l,tree[3000006][4],minn,dp1[100005],dp2[100005];
pair < pair <ll,ll> , pair < ll , ll > > A[100005];
vector <ll> v;
map <ll,ll> mp;
void update(ll node,ll le,ll ri,ll idx,ll val,ll type) {
if(ri<idx || le>idx) return;
if(le==ri) { tree[node][type]=min(tree[node][type],val); return; }
update(2*node,le,(le+ri)/2,idx,val,type);
update(2*node+1,(le+ri)/2+1,ri,idx,val,type);
tree[node][type]=min(tree[2*node][type],tree[2*node+1][type]);
}
ll getmin(ll node,ll le,ll ri,ll start,ll end,ll type) {
if(ri<start || le>end) return 1e17;
if(start<=le && ri<=end) { return tree[node][type]; }
a=min(getmin(2*node,le,(le+ri)/2,start,end,type),getmin(2*node+1,(le+ri)/2+1,ri,start,end,type));
return a;
}
int main() {
cin>>n>>m;
for(ll i=1;i<=n;i++) {
cin>>A[i].f.f>>A[i].f.sc>>A[i].sc.f>>A[i].sc.sc;
v.pb(A[i].f.f);
v.pb(A[i].f.sc);
v.pb(A[i].sc.f);
dp1[i]=1e17; dp2[i]=1e17;
}
v.pb(1); v.pb(m);
sort(v.begin(),v.end());
k=1; mp[v[0]]=k;
for(ll i=1;i<v.size();i++) {
if(v[i]>v[i-1]) k++;
mp[v[i]]=k;
}
for(ll i=1;i<=n;i++) {
A[i].f.f=mp[A[i].f.f];
A[i].f.sc=mp[A[i].f.sc];
A[i].sc.f=mp[A[i].sc.f];
//cout<<A[i].f.f<<" "<<A[i].f.sc<<" "<<A[i].sc.f<<endl;
}
m=mp[m];
for(ll i=0;i<=4*m;i++) {
tree[i][1]=1e17;
tree[i][2]=1e17;
}
minn=1e17;
for(ll i=1;i<=n;i++) {
if(A[i].f.f==1) dp1[i]=A[i].sc.sc;
if(A[i].f.sc==m) dp2[i]=A[i].sc.sc;
if(dp1[i]==1e17) {
//cout<<"* "<<getmin(1,1,m,A[i].f.f,A[i].f.sc,1)<<" ";
dp1[i]=getmin(1,1,m,A[i].f.f,A[i].f.sc,1)+A[i].sc.sc;
}
if(dp2[i]==1e17) {
// cout<<"^";
dp2[i]=getmin(1,1,m,A[i].f.f,A[i].f.sc,2)+A[i].sc.sc;
}
//cout<<dp1[i]<<" "<<dp2[i]<<endl;
update(1,1,m,A[i].sc.f,dp1[i],1);
update(1,1,m,A[i].sc.f,dp2[i],2);
minn=min(minn,dp1[i]+dp2[i]-A[i].sc.sc);
}
if(minn==1e17) cout<<-1;
else cout<<minn;
}
Compilation message
pinball.cpp: In function 'int main()':
pinball.cpp:36:17: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<long long int>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
36 | for(ll i=1;i<v.size();i++) {
| ~^~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
2 ms |
460 KB |
Output is correct |
13 |
Correct |
2 ms |
460 KB |
Output is correct |
14 |
Correct |
2 ms |
460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
2 ms |
460 KB |
Output is correct |
13 |
Correct |
2 ms |
460 KB |
Output is correct |
14 |
Correct |
2 ms |
460 KB |
Output is correct |
15 |
Correct |
2 ms |
332 KB |
Output is correct |
16 |
Correct |
2 ms |
460 KB |
Output is correct |
17 |
Correct |
4 ms |
588 KB |
Output is correct |
18 |
Correct |
3 ms |
460 KB |
Output is correct |
19 |
Correct |
5 ms |
844 KB |
Output is correct |
20 |
Correct |
4 ms |
572 KB |
Output is correct |
21 |
Correct |
2 ms |
588 KB |
Output is correct |
22 |
Correct |
5 ms |
956 KB |
Output is correct |
23 |
Correct |
4 ms |
972 KB |
Output is correct |
24 |
Correct |
4 ms |
972 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
11 |
Correct |
1 ms |
332 KB |
Output is correct |
12 |
Correct |
2 ms |
460 KB |
Output is correct |
13 |
Correct |
2 ms |
460 KB |
Output is correct |
14 |
Correct |
2 ms |
460 KB |
Output is correct |
15 |
Correct |
2 ms |
332 KB |
Output is correct |
16 |
Correct |
2 ms |
460 KB |
Output is correct |
17 |
Correct |
4 ms |
588 KB |
Output is correct |
18 |
Correct |
3 ms |
460 KB |
Output is correct |
19 |
Correct |
5 ms |
844 KB |
Output is correct |
20 |
Correct |
4 ms |
572 KB |
Output is correct |
21 |
Correct |
2 ms |
588 KB |
Output is correct |
22 |
Correct |
5 ms |
956 KB |
Output is correct |
23 |
Correct |
4 ms |
972 KB |
Output is correct |
24 |
Correct |
4 ms |
972 KB |
Output is correct |
25 |
Correct |
41 ms |
4524 KB |
Output is correct |
26 |
Correct |
129 ms |
13420 KB |
Output is correct |
27 |
Correct |
372 ms |
26832 KB |
Output is correct |
28 |
Correct |
305 ms |
12096 KB |
Output is correct |
29 |
Correct |
262 ms |
22820 KB |
Output is correct |
30 |
Correct |
411 ms |
14552 KB |
Output is correct |
31 |
Correct |
636 ms |
42756 KB |
Output is correct |
32 |
Correct |
610 ms |
33788 KB |
Output is correct |
33 |
Correct |
84 ms |
13368 KB |
Output is correct |
34 |
Correct |
307 ms |
33924 KB |
Output is correct |
35 |
Correct |
480 ms |
67556 KB |
Output is correct |
36 |
Correct |
786 ms |
67544 KB |
Output is correct |
37 |
Correct |
646 ms |
67712 KB |
Output is correct |
38 |
Correct |
650 ms |
67532 KB |
Output is correct |