This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//Self-control leads to consistency.
#include <bits/stdc++.h>
using namespace std;
#pragma GCC target ("avx2")
#pragma GCC optimization ("O3")
#pragma GCC optimization ("unroll-loops")
#define flash ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0)
#define debug(x) cerr << " - " << #x << ": " << x << endl;
#define debugs(x, y) cerr << " - " << #x << ": " << x << " " << #y << ": " << y << endl;
#define all(x) (x).begin(),(x).end()
#define sz(x) (ll)x.size()
#define ll long long
#define INF 100000000000000000
#define MOD 1000000007
#define pb push_back
#define ve vector<ll>
#define dos pair<ll,ll>
#define vedos vector<dos>
#define rand mt19937 rng(chrono::steady_clock::now().time_since_epoch().count())
#define EPS 0.000001
struct greateri
{
template<class T>
bool operator()(T const &a, T const &b) const { return a > b; }
};
void setIO(string s) {
ios_base::sync_with_stdio(0); cin.tie(0);
freopen((s+".in").c_str(),"r",stdin);
freopen((s+".out").c_str(),"w",stdout);
}
ll m,n,price[100001],seg[400001],dp[100001],pos[100001],dp1[100001];
dos ran[100001];
void upd(ll p,ll l,ll r,ll in,ll val){
if(l==r && l==in){
seg[p]=val;
return;
}
ll med=(l+r)>>1;
if(in<=med)
upd(p*2,l,med,in,val);
else upd(p*2+1,med+1,r,in,val);
seg[p]=min(seg[p*2],seg[p*2+1]);
}
ll query(ll p,ll l,ll r,ll l1,ll r1){
if(l>r || l>r1 || l1>r)return INF;
if(l>=l1 && r<=r1)return seg[p];
ll med=(l+r)>>1;
return min(query(p*2,l,med,l1,r1),query(p*2+1,med+1,r,l1,r1));
}
int main()
{
flash;
cin>>m>>n;
set<dos> in;
for (ll i = 0; i < m; ++i)
{
ll a,b,c,d;
cin>>a>>b>>c>>d;
a--,b--,c--;
ran[i]={a,b};
in.insert({c,i});
price[i]=d;
}
ll co=0;
for(auto it:in){
pos[it.second]=co++;
}
for (ll i = 0; i < m; ++i)
{
dp[i]=INF;
upd(1,0,m-1,pos[i],dp[i]);
}
for (ll i = 0; i < m; ++i)
{
if(ran[i].first==0)dp[i]=price[i];
else{
auto u=in.lower_bound({ran[i].first,-INF});
auto v=in.lower_bound({ran[i].second+1,-INF});
if(v!=in.begin() && u!=in.end()){
v--;
dp[i]=price[i]+query(1,0,m-1,pos[(*u).second],pos[(*v).second]);
}
}
upd(1,0,m-1,pos[i],dp[i]);
}
for (ll i = 0; i < m; ++i)
{
dp1[i]=INF;
upd(1,0,m-1,pos[i],dp1[i]);
}
for (ll i = 0; i < m; ++i)
{
if(ran[i].second==n-1)dp1[i]=price[i];
else{
auto u=in.lower_bound({ran[i].first,-INF});
auto v=in.lower_bound({ran[i].second+1,-INF});
if(v!=in.begin() && u!=in.end()){
v--;
dp1[i]=price[i]+query(1,0,m-1,pos[(*u).second],pos[(*v).second]);
}
}
upd(1,0,m-1,pos[i],dp1[i]);
}
ll ans=INF;
for (ll i = 0; i < m; ++i)
{
ans=min(ans,dp[i]+dp1[i]-price[i]);
}
if(ans==INF)cout<<-1;
else cout<<ans;
return 0;
}
Compilation message (stderr)
pinball.cpp:5:0: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
#pragma GCC optimization ("O3")
pinball.cpp:6:0: warning: ignoring #pragma GCC optimization [-Wunknown-pragmas]
#pragma GCC optimization ("unroll-loops")
pinball.cpp: In function 'void setIO(std::__cxx11::string)':
pinball.cpp:28:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
freopen((s+".in").c_str(),"r",stdin);
~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~
pinball.cpp:29:10: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)', declared with attribute warn_unused_result [-Wunused-result]
freopen((s+".out").c_str(),"w",stdout);
~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# | 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... |