#include<bits/stdc++.h>
#define TASKNAME "codeforce"
#define pb push_back
#define pli pair<int,int>
#define fi first
#define se second
#define fastio ios_base::sync_with_stdio(false); cin.tie(NULL);
using namespace std;
using ll=long long;
const ll maxN=2e5;
const ll inf=1e18;
const ll mod=1e9+7;
ll st[4*maxN];
ll n;
void update(ll pos,ll val,ll id=1,ll l=1,ll r=n+2)
{
if(l==r)
{
st[id]=min(st[id],val);
return;
}
ll mid=l+r>>1;
if(pos<=mid) update(pos,val,id*2,l,mid);
else update(pos,val,id*2+1,mid+1,r);
st[id]=min(st[id*2],st[id*2+1]);
}
ll get(ll i,ll j,ll id=1,ll l=1,ll r=n+2)
{
if(j<l||r<i) return inf;
if(i<=l&&r<=j) return st[id];
ll mid=l+r>>1;
return min(get(i,j,id*2,l,mid),get(i,j,id*2+1,mid+1,r));
}
ll dp[2][maxN],a[maxN],c[maxN],d[maxN],e[maxN],b[maxN];
void cal(ll x,ll t)
{
dp[t][0]=0;
c[0]=x;
fill(st,st+4*maxN,inf);
for(int i=0;i<=n;i++)
{
if(i!=0)
{
dp[t][i]=inf;
ll l,r;
l=lower_bound(e+1,e+n+3,a[i])-e;
r=upper_bound(e+1,e+n+3,b[i])-e-1;
dp[t][i]=min(dp[t][i],get(l,r)+d[i]);
}
ll pos=lower_bound(e+1,e+n+3,c[i])-e;
update(pos,dp[t][i]);
}
}
ll m,sz=0;
void solve()
{
cin >> n >> m;
for(int i=1;i<=n;i++) cin >> a[i] >> b[i] >> c[i] >> d[i],e[++sz]=c[i];
e[n+1]=1;
e[n+2]=m;
sort(e+1,e+n+3);
cal(1,0);
cal(m,1);
ll ans=inf;
for(int i=1;i<=n;i++)
{
ans=min(ans,dp[0][i]+dp[1][i]-d[i]);
}
if(ans!=inf) cout << ans;
else cout << -1;
}
int main()
{
fastio
//freopen(TASKNAME".INP","r",stdin);
//freopen(TASKNAME".OUT","w",stdout);
solve();
}
Compilation message
pinball.cpp: In function 'void update(ll, ll, ll, ll, ll)':
pinball.cpp:22:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
22 | ll mid=l+r>>1;
| ~^~
pinball.cpp: In function 'll get(ll, ll, ll, ll, ll)':
pinball.cpp:31:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
31 | ll mid=l+r>>1;
| ~^~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6612 KB |
Output is correct |
2 |
Correct |
3 ms |
6612 KB |
Output is correct |
3 |
Correct |
3 ms |
6612 KB |
Output is correct |
4 |
Correct |
3 ms |
6612 KB |
Output is correct |
5 |
Correct |
3 ms |
6612 KB |
Output is correct |
6 |
Correct |
3 ms |
6612 KB |
Output is correct |
7 |
Correct |
3 ms |
6596 KB |
Output is correct |
8 |
Correct |
3 ms |
6600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6612 KB |
Output is correct |
2 |
Correct |
3 ms |
6612 KB |
Output is correct |
3 |
Correct |
3 ms |
6612 KB |
Output is correct |
4 |
Correct |
3 ms |
6612 KB |
Output is correct |
5 |
Correct |
3 ms |
6612 KB |
Output is correct |
6 |
Correct |
3 ms |
6612 KB |
Output is correct |
7 |
Correct |
3 ms |
6596 KB |
Output is correct |
8 |
Correct |
3 ms |
6600 KB |
Output is correct |
9 |
Correct |
3 ms |
6608 KB |
Output is correct |
10 |
Correct |
4 ms |
6612 KB |
Output is correct |
11 |
Correct |
3 ms |
6612 KB |
Output is correct |
12 |
Correct |
3 ms |
6612 KB |
Output is correct |
13 |
Correct |
3 ms |
6604 KB |
Output is correct |
14 |
Correct |
3 ms |
6612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6612 KB |
Output is correct |
2 |
Correct |
3 ms |
6612 KB |
Output is correct |
3 |
Correct |
3 ms |
6612 KB |
Output is correct |
4 |
Correct |
3 ms |
6612 KB |
Output is correct |
5 |
Correct |
3 ms |
6612 KB |
Output is correct |
6 |
Correct |
3 ms |
6612 KB |
Output is correct |
7 |
Correct |
3 ms |
6596 KB |
Output is correct |
8 |
Correct |
3 ms |
6600 KB |
Output is correct |
9 |
Correct |
3 ms |
6608 KB |
Output is correct |
10 |
Correct |
4 ms |
6612 KB |
Output is correct |
11 |
Correct |
3 ms |
6612 KB |
Output is correct |
12 |
Correct |
3 ms |
6612 KB |
Output is correct |
13 |
Correct |
3 ms |
6604 KB |
Output is correct |
14 |
Correct |
3 ms |
6612 KB |
Output is correct |
15 |
Correct |
3 ms |
6612 KB |
Output is correct |
16 |
Correct |
3 ms |
6612 KB |
Output is correct |
17 |
Correct |
5 ms |
6728 KB |
Output is correct |
18 |
Correct |
4 ms |
6612 KB |
Output is correct |
19 |
Correct |
4 ms |
6612 KB |
Output is correct |
20 |
Correct |
4 ms |
6612 KB |
Output is correct |
21 |
Correct |
4 ms |
6612 KB |
Output is correct |
22 |
Correct |
6 ms |
6612 KB |
Output is correct |
23 |
Correct |
4 ms |
6612 KB |
Output is correct |
24 |
Correct |
4 ms |
6696 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
3 ms |
6612 KB |
Output is correct |
2 |
Correct |
3 ms |
6612 KB |
Output is correct |
3 |
Correct |
3 ms |
6612 KB |
Output is correct |
4 |
Correct |
3 ms |
6612 KB |
Output is correct |
5 |
Correct |
3 ms |
6612 KB |
Output is correct |
6 |
Correct |
3 ms |
6612 KB |
Output is correct |
7 |
Correct |
3 ms |
6596 KB |
Output is correct |
8 |
Correct |
3 ms |
6600 KB |
Output is correct |
9 |
Correct |
3 ms |
6608 KB |
Output is correct |
10 |
Correct |
4 ms |
6612 KB |
Output is correct |
11 |
Correct |
3 ms |
6612 KB |
Output is correct |
12 |
Correct |
3 ms |
6612 KB |
Output is correct |
13 |
Correct |
3 ms |
6604 KB |
Output is correct |
14 |
Correct |
3 ms |
6612 KB |
Output is correct |
15 |
Correct |
3 ms |
6612 KB |
Output is correct |
16 |
Correct |
3 ms |
6612 KB |
Output is correct |
17 |
Correct |
5 ms |
6728 KB |
Output is correct |
18 |
Correct |
4 ms |
6612 KB |
Output is correct |
19 |
Correct |
4 ms |
6612 KB |
Output is correct |
20 |
Correct |
4 ms |
6612 KB |
Output is correct |
21 |
Correct |
4 ms |
6612 KB |
Output is correct |
22 |
Correct |
6 ms |
6612 KB |
Output is correct |
23 |
Correct |
4 ms |
6612 KB |
Output is correct |
24 |
Correct |
4 ms |
6696 KB |
Output is correct |
25 |
Correct |
17 ms |
7412 KB |
Output is correct |
26 |
Correct |
44 ms |
8816 KB |
Output is correct |
27 |
Correct |
129 ms |
12872 KB |
Output is correct |
28 |
Correct |
194 ms |
15764 KB |
Output is correct |
29 |
Correct |
110 ms |
11232 KB |
Output is correct |
30 |
Correct |
199 ms |
15952 KB |
Output is correct |
31 |
Correct |
197 ms |
15992 KB |
Output is correct |
32 |
Correct |
199 ms |
15948 KB |
Output is correct |
33 |
Correct |
25 ms |
8268 KB |
Output is correct |
34 |
Correct |
102 ms |
11212 KB |
Output is correct |
35 |
Correct |
122 ms |
15852 KB |
Output is correct |
36 |
Correct |
192 ms |
15868 KB |
Output is correct |
37 |
Correct |
165 ms |
15876 KB |
Output is correct |
38 |
Correct |
165 ms |
15820 KB |
Output is correct |