This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
//by szh
#include<bits/stdc++.h>
using namespace std;
#define fi first
#define se second
#define pii pair<int,int>
#define pll pair<long long,long long>
#define pb push_back
#define debug(x) cerr<<#x<<"="<<x<<endl
#define pq priority_queue
#define inf 0x3f
#define rep(i,a,b) for (int i=a;i<(b);i++)
#define MP make_pair
#define SZ(x) (int(x.size()))
#define ll long long
#define mod 1000000007
#define ALL(x) x.begin(),x.end()
void inc(int &a,int b) {a=(a+b)%mod;}
void dec(int &a,int b) {a=(a-b+mod)%mod;}
int lowbit(int x) {return x&(-x);}
ll p0w(ll base,ll p) {ll ret=1;while(p>0){if (p%2ll==1ll) ret=ret*base%mod;base=base*base%mod;p/=2ll;}return ret;}
const int maxn = 3e5+10;
int n,m;
map <int,int> mp;
int a[maxn],b[maxn],c[maxn],d[maxn];
pll tree[maxn*4];
ll big = 1e18;
void update(int c,int cl,int cr,int pos,pll val) {
if (cl==cr) tree[c] = val;
else {
int mid=cl+cr>>1;
if (pos<=mid) update(c<<1,cl,mid,pos,val);
else update(c<<1|1,mid+1,cr,pos,val);
tree[c].fi = min(tree[c<<1].fi,tree[c<<1|1].fi);
tree[c].se = min(tree[c<<1].se,tree[c<<1|1].se);
}
}
pll query(int c,int cl,int cr,int l,int r) {
if (l<=cl and cr<=r) return tree[c];
int mid = cl+cr>>1;
pll ret = {big,big};
if (l<=mid) ret = query(c<<1,cl,mid,l,r);
if (r>mid) {
pll tmp = query(c<<1|1,mid+1,cr,l,r);
ret.fi = min(ret.fi,tmp.fi);
ret.se = min(ret.se,tmp.se);
}
return ret;
}
int main() {
// freopen("input.txt","r",stdin);
std::ios::sync_with_stdio(false);cin.tie(0);
cin>>n>>m;
ll ans = big;
rep(i,0,maxn*4) tree[i] = {big,big};
bool check1=false,check2=false;
rep(i,0,n) {
cin>>a[i]>>b[i]>>c[i]>>d[i];
mp[a[i]]=1,mp[b[i]]=1,mp[c[i]]=1;
if (a[i]==1) check1 = true;
if (b[i]==m) check2 = true;
}
if (check1==false or check2==false) {
cout<<-1;
return 0;
}
int tot = 0;
for (auto &it:mp) it.se=++tot;
rep(i,0,n) a[i] = mp[a[i]], b[i] = mp[b[i]], c[i] = mp[c[i]];
rep(i,0,n) {
pll tmp = query(1,1,tot,a[i],b[i]);
pll cur = {big,big};
if (a[i]==1) cur.fi=d[i];
else cur.fi = tmp.fi + d[i];
if (b[i]==tot) cur.se = d[i];
else cur.se = tmp.se + d[i];
if (cur.fi < big or cur.se<big) {
update(1,1,tot,c[i],cur);
ans = min(ans,cur.fi+cur.se-d[i]);
}
// debug(c[i]),debug(cur.fi),debug(cur.se);
}
if (ans==big) cout<<-1;
else cout<<ans;
return 0;
}
Compilation message (stderr)
pinball.cpp: In function 'void update(int, int, int, int, std::pair<long long int, long long int>)':
pinball.cpp:35:13: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
35 | int mid=cl+cr>>1;
| ~~^~~
pinball.cpp: In function 'std::pair<long long int, long long int> query(int, int, int, int, int)':
pinball.cpp:45:14: warning: suggest parentheses around '+' inside '>>' [-Wparentheses]
45 | int mid = cl+cr>>1;
| ~~^~~
# | 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... |