/**11:37**/
#include<bits/stdc++.h>
#define maxn 100010
#define INF 100000000000000000
using namespace std;
int m,n;
int a[maxn],b[maxn],c[maxn],d[maxn];
map<int,bool> bio;
int len;
vector<int>poz;
long long dis[4*maxn][2];
void build(int v,int l,int r,int type)
{
if(l==r)
{
if((type==0 && l==0) || (type==1 && l==len-1))dis[v][type]=0;
else dis[v][type]=INF;
return;
}
int mid=(l+r)/2;
build(2*v,l,mid,type);
build(2*v+1,mid+1,r,type);
dis[v][type]=min(dis[2*v][type],dis[2*v+1][type]);
}
long long minn(int v,int l,int r,int lt,int rt,int type)
{
if(l>r || rt<l || lt>r)return INF;
if(lt<=l && r<=rt)return dis[v][type];
int mid=(l+r)/2;
return min(minn(2*v,l,mid,lt,rt,type),minn(2*v+1,mid+1,r,lt,rt,type));
}
void update(int v,int l,int r,int ind,long long vre,int type)
{
if(l>r || l>ind || ind>r)return;
if(l==r)
{
dis[v][type]=vre;
return;
}
int mid=(l+r)/2;
update(2*v,l,mid,ind,vre,type);
update(2*v+1,mid+1,r,ind,vre,type);
dis[v][type]=min(dis[2*v][type],dis[2*v+1][type]);
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
cout.tie(nullptr);
cin>>m>>n;
bio[1]=bio[n]=true;
poz.push_back(1);
poz.push_back(n);
for(int i=1;i<=m;i++)
{
cin>>a[i]>>b[i]>>c[i]>>d[i];
if(!bio[c[i]])poz.push_back(c[i]);
bio[c[i]]=true;
}
sort(poz.begin(),poz.end());
len=poz.size();
//for(int i=0;i<len;i++)cout<<poz[i]<<" ";cout<<endl;
for(int i=1;i<=m;i++)
{
a[i]=lower_bound(poz.begin(),poz.end(),a[i])-poz.begin();
b[i]=upper_bound(poz.begin(),poz.end(),b[i])-poz.begin()-1;
//cout<<i<<": "<<a[i]<<","<<b[i]<<endl;
}
long long res=INF;
for(int type=0;type<2;type++){build(1,0,len-1,type);}
for(int i=1;i<=m;i++)
{
long long cena=minn(1,0,len-1,a[i],b[i],0);
int pos=lower_bound(poz.begin(),poz.end(),c[i])-poz.begin();
long long tre=minn(1,0,len-1,pos,pos,0);
if(cena+d[i]<tre)update(1,0,len-1,pos,cena+d[i],0);
long long cena1=minn(1,0,len-1,a[i],b[i],1);
long long tre1=minn(1,0,len-1,pos,pos,1);
if(cena1+d[i]<tre1)update(1,0,len-1,pos,cena1+d[i],1);
if(cena+cena1+d[i]<res)res=cena+cena1+d[i];
if(cena+d[i]+tre1<res)res=cena+d[i]+tre1;
if(tre+cena1+d[i]<res)res=tre+cena1+d[i];
if(tre+tre1<res)res=tre+tre1;
}
if(res==INF)cout<<"-1"<<endl;
else cout<<res<<endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
3 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
4 ms |
384 KB |
Output is correct |
12 |
Correct |
3 ms |
384 KB |
Output is correct |
13 |
Correct |
3 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
3 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
4 ms |
384 KB |
Output is correct |
12 |
Correct |
3 ms |
384 KB |
Output is correct |
13 |
Correct |
3 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
512 KB |
Output is correct |
18 |
Correct |
4 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
512 KB |
Output is correct |
20 |
Correct |
4 ms |
384 KB |
Output is correct |
21 |
Correct |
4 ms |
512 KB |
Output is correct |
22 |
Correct |
5 ms |
512 KB |
Output is correct |
23 |
Correct |
4 ms |
640 KB |
Output is correct |
24 |
Correct |
5 ms |
640 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
384 KB |
Output is correct |
2 |
Correct |
2 ms |
384 KB |
Output is correct |
3 |
Correct |
2 ms |
384 KB |
Output is correct |
4 |
Correct |
3 ms |
384 KB |
Output is correct |
5 |
Correct |
2 ms |
384 KB |
Output is correct |
6 |
Correct |
2 ms |
384 KB |
Output is correct |
7 |
Correct |
2 ms |
384 KB |
Output is correct |
8 |
Correct |
3 ms |
384 KB |
Output is correct |
9 |
Correct |
3 ms |
384 KB |
Output is correct |
10 |
Correct |
2 ms |
384 KB |
Output is correct |
11 |
Correct |
4 ms |
384 KB |
Output is correct |
12 |
Correct |
3 ms |
384 KB |
Output is correct |
13 |
Correct |
3 ms |
384 KB |
Output is correct |
14 |
Correct |
2 ms |
384 KB |
Output is correct |
15 |
Correct |
2 ms |
384 KB |
Output is correct |
16 |
Correct |
2 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
512 KB |
Output is correct |
18 |
Correct |
4 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
512 KB |
Output is correct |
20 |
Correct |
4 ms |
384 KB |
Output is correct |
21 |
Correct |
4 ms |
512 KB |
Output is correct |
22 |
Correct |
5 ms |
512 KB |
Output is correct |
23 |
Correct |
4 ms |
640 KB |
Output is correct |
24 |
Correct |
5 ms |
640 KB |
Output is correct |
25 |
Correct |
27 ms |
1436 KB |
Output is correct |
26 |
Correct |
73 ms |
3792 KB |
Output is correct |
27 |
Correct |
236 ms |
8436 KB |
Output is correct |
28 |
Correct |
159 ms |
5924 KB |
Output is correct |
29 |
Correct |
168 ms |
7120 KB |
Output is correct |
30 |
Correct |
222 ms |
6800 KB |
Output is correct |
31 |
Correct |
327 ms |
13552 KB |
Output is correct |
32 |
Correct |
355 ms |
10896 KB |
Output is correct |
33 |
Correct |
52 ms |
3580 KB |
Output is correct |
34 |
Correct |
174 ms |
7796 KB |
Output is correct |
35 |
Correct |
257 ms |
15084 KB |
Output is correct |
36 |
Correct |
476 ms |
15088 KB |
Output is correct |
37 |
Correct |
428 ms |
15000 KB |
Output is correct |
38 |
Correct |
397 ms |
14956 KB |
Output is correct |