#include <bits/stdc++.h>
#define tt cin.tie(0), cout.tie(0), ios_base::sync_with_stdio(0)
#define fo freopen((NAME+".INP").c_str(), "r", stdin), freopen((NAME+".OUT").c_str(), "w", stdout)
#define ll long long
#define ull unsigned long long
#define i128 __int128
#define db long double
#define sz(a) ((int)(a).size())
#define pb emplace_back
#define pf emplace_front
#define pob pop_back
#define pof pop_front
#define lb lower_bound
#define ub upper_bound
#define fi first
#define se second
#define ins emplace
using namespace std;
const int MOD = 1e9+7, MAXN = 3e5+5;
const string NAME = "";
struct Device{
int l,r,to,cost;
}a[MAXN];
int m,n;
ll dp[MAXN][3],rs=1e18;
void compress(){
vector<int> v;
for(int i = 1; i<=m; ++i)
v.pb(a[i].l), v.pb(a[i].r), v.pb(a[i].to);
sort(v.begin(),v.end()), v.resize(unique(v.begin(),v.end())-v.begin());
for(int i = 1; i<=m; ++i){
a[i].l=lb(v.begin(),v.end(),a[i].l)-v.begin()+1;
a[i].r=lb(v.begin(),v.end(),a[i].r)-v.begin()+1;
a[i].to=lb(v.begin(),v.end(),a[i].to)-v.begin()+1;
}
}
pair<ll,ll> sgt[MAXN<<2];
void update(int id, int l, int r, int pos, const pair<ll,ll>& val){
if(pos<l||r<pos) return;
if(l==r){
sgt[id].fi=min(sgt[id].fi,val.fi);
sgt[id].se=min(sgt[id].se,val.se);
return;
}
int mid=(l+r)>>1;
update(id<<1,l,mid,pos,val);
update(id<<1|1,mid+1,r,pos,val);
sgt[id].fi=min(sgt[id<<1].fi,sgt[id<<1|1].fi);
sgt[id].se=min(sgt[id<<1].se,sgt[id<<1|1].se);
}
pair<ll,ll> getmin(int id, int l, int r, int u, int v){
if(v<l||r<u) return {1e18,1e18};
if(u<=l&&r<=v) return sgt[id];
int mid=(l+r)>>1;
pair<ll,ll> Left=getmin(id<<1,l,mid,u,v),Right=getmin(id<<1|1,mid+1,r,u,v);
return {min(Left.fi,Right.fi),min(Left.se,Right.se)};
}
int main()
{
tt;
if(fopen((NAME + ".INP").c_str(), "r")) fo;
cin >> m >> n;
memset(dp,0x3f,sizeof(dp));
for(int i = 1; i<=m; ++i){
cin >> a[i].l >> a[i].r >> a[i].to >> a[i].cost;
if(a[i].l==1&&a[i].r==n) dp[i][2]=a[i].cost;
else if(a[i].l==1) dp[i][0]=a[i].cost;
else if(a[i].r==n) dp[i][1]=a[i].cost;
}
compress();
memset(sgt,0x3f,sizeof(sgt));
for(int i = 1; i<=m; ++i){
pair<ll,ll> MIN=getmin(1,1,3*m,a[i].l,a[i].r);
ll mincost[2]={MIN.fi,MIN.se};
dp[i][2]=min({dp[i][2],mincost[0]+mincost[1]+a[i].cost,dp[i][0]+mincost[1],dp[i][1]+mincost[0]});
dp[i][0]=min(dp[i][0],mincost[0]+a[i].cost);
dp[i][1]=min(dp[i][1],mincost[1]+a[i].cost);
update(1,1,3*m,a[i].to,{dp[i][0],dp[i][1]});
rs=min(rs,dp[i][2]);
}
if(rs>1e14) rs=-1;
cout << rs;
}
컴파일 시 표준 에러 (stderr) 메시지
pinball.cpp: In function 'int main()':
pinball.cpp:3:19: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
3 | #define fo freopen((NAME+".INP").c_str(), "r", stdin), freopen((NAME+".OUT").c_str(), "w", stdout)
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
pinball.cpp:61:45: note: in expansion of macro 'fo'
61 | if(fopen((NAME + ".INP").c_str(), "r")) fo;
| ^~
pinball.cpp:3:63: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
3 | #define fo freopen((NAME+".INP").c_str(), "r", stdin), freopen((NAME+".OUT").c_str(), "w", stdout)
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
pinball.cpp:61:45: note: in expansion of macro 'fo'
61 | if(fopen((NAME + ".INP").c_str(), "r")) fo;
| ^~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |