#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#define rc(x) return cout<<x<<endl,0
#define pb push_back
#define mkp make_pair
#define in insert
#define er erase
#define fd find
#define fr first
#define sc second
#define all(x) x.begin(),x.end()
#define lun(x) (int)x.size()
typedef long long ll;
typedef long double ld;
const ll INF=0x3f3f3f3f3f3f3f3f;
const ll llinf=(1LL<<60);
const int inf=(1<<30);
const int nmax=1e5+50;
const ll mod=1e9+7;
using namespace std;
int m,n,a[nmax],b[nmax],c[nmax],d[nmax],sz,i;
ll f[nmax],g[nmax],x,st[12*nmax],rs;
vector<int>vc;
void upd(int nod,int l,int r,int p,ll v)
{
if(l==r)
{
st[nod]=min(st[nod],v);
return;
}
int mid=(l+r)/2;
if(p<=mid)upd(2*nod,l,mid,p,v);
else upd(2*nod+1,mid+1,r,p,v);
st[nod]=min(st[2*nod],st[2*nod+1]);
}
ll qry(int nod,int l,int r,int tl,int tr)
{
if(tr<l || tl>r)return llinf;
if(tl<=l && r<=tr)return st[nod];
int mid=(l+r)/2;
return min(qry(2*nod,l,mid,tl,tr),qry(2*nod+1,mid+1,r,tl,tr));
}
int main()
{
//freopen("sol.in","r",stdin);
//freopen("sol.out","w",stdout);
//mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
ios_base::sync_with_stdio(false);cin.tie(0);cerr.tie(0);cout.tie(0);
cin>>m>>n;
for(i=1;i<=m;i++)
{
cin>>a[i]>>b[i]>>c[i]>>d[i];
vc.pb(a[i]);
vc.pb(b[i]);
vc.pb(c[i]);
}
vc.pb(1),vc.pb(n);
sort(all(vc));
vc.resize(unique(all(vc))-vc.begin());
sz=lun(vc);
for(i=1;i<=m;i++)
{
a[i]=lower_bound(all(vc),a[i])-vc.begin()+1;
b[i]=lower_bound(all(vc),b[i])-vc.begin()+1;
c[i]=lower_bound(all(vc),c[i])-vc.begin()+1;
}
for(i=1;i<=4*sz;i++)st[i]=llinf;
for(i=1;i<=m;i++)
{
f[i]=llinf;
x=qry(1,1,sz,a[i],b[i]);
if(a[i]==1)f[i]=d[i];
else if(x!=llinf)f[i]=x+d[i];
upd(1,1,sz,c[i],f[i]);
}
for(i=1;i<=4*sz;i++)st[i]=llinf;
rs=llinf;
for(i=1;i<=m;i++)
{
g[i]=llinf;
x=qry(1,1,sz,a[i],b[i]);
if(b[i]==sz)g[i]=d[i];
else if(x!=llinf)g[i]=x+d[i];
upd(1,1,sz,c[i],g[i]);
if(f[i]!=llinf && g[i]!=llinf)rs=min(rs,f[i]+g[i]-d[i]);
}
if(rs==llinf)rs=-1;
cout<<rs<<endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
6 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
640 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
6 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
640 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
512 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
6 ms |
384 KB |
Output is correct |
17 |
Correct |
6 ms |
512 KB |
Output is correct |
18 |
Correct |
6 ms |
512 KB |
Output is correct |
19 |
Correct |
7 ms |
512 KB |
Output is correct |
20 |
Correct |
6 ms |
512 KB |
Output is correct |
21 |
Correct |
5 ms |
384 KB |
Output is correct |
22 |
Correct |
6 ms |
512 KB |
Output is correct |
23 |
Correct |
6 ms |
512 KB |
Output is correct |
24 |
Correct |
7 ms |
512 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
4 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
4 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
6 ms |
384 KB |
Output is correct |
12 |
Correct |
5 ms |
640 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
512 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
6 ms |
384 KB |
Output is correct |
17 |
Correct |
6 ms |
512 KB |
Output is correct |
18 |
Correct |
6 ms |
512 KB |
Output is correct |
19 |
Correct |
7 ms |
512 KB |
Output is correct |
20 |
Correct |
6 ms |
512 KB |
Output is correct |
21 |
Correct |
5 ms |
384 KB |
Output is correct |
22 |
Correct |
6 ms |
512 KB |
Output is correct |
23 |
Correct |
6 ms |
512 KB |
Output is correct |
24 |
Correct |
7 ms |
512 KB |
Output is correct |
25 |
Correct |
23 ms |
1792 KB |
Output is correct |
26 |
Correct |
58 ms |
3956 KB |
Output is correct |
27 |
Correct |
162 ms |
7404 KB |
Output is correct |
28 |
Correct |
162 ms |
5480 KB |
Output is correct |
29 |
Correct |
119 ms |
6256 KB |
Output is correct |
30 |
Correct |
202 ms |
5996 KB |
Output is correct |
31 |
Correct |
249 ms |
10728 KB |
Output is correct |
32 |
Correct |
247 ms |
9320 KB |
Output is correct |
33 |
Correct |
37 ms |
3840 KB |
Output is correct |
34 |
Correct |
113 ms |
8044 KB |
Output is correct |
35 |
Correct |
176 ms |
14824 KB |
Output is correct |
36 |
Correct |
261 ms |
14824 KB |
Output is correct |
37 |
Correct |
251 ms |
14824 KB |
Output is correct |
38 |
Correct |
230 ms |
14840 KB |
Output is correct |