#include<bits/stdc++.h>
using namespace std;
#define ll long long
#define ld long double
#define pii pair<int,int>
#define pll pair<ll,ll>
#define fi first
#define se second
const int maxN=1e5+5,LIMIT=1e6+5;
const ll inf=2e18;
int n,m,a[maxN+1],b[maxN+1],c[maxN+1];
ll d[maxN+1],dp[maxN+1],f[maxN+1];
vector<int> cprs;
bool intersect(ll l,ll r,ll mid)
{
return (l<=mid&&mid<=r);
}
struct segment
{
ll sum[4*LIMIT+1];
void Build(int x,int low,int high)
{
if(low==high)
{
sum[x]=inf;
return;
}
int mid=(low+high)/2;
Build(2*x,low,mid);
Build(2*x+1,mid+1,high);
sum[x]=min(sum[2*x],sum[2*x+1]);
}
int pos,l,r;
ll val;
void Update(int x,int low,int high)
{
if(low==high)
{
sum[x]=min(sum[x],val);
return;
}
int mid=(low+high)/2;
if(pos<=mid)
{
Update(2*x,low,mid);
}
else Update(2*x+1,mid+1,high);
sum[x]=min(sum[2*x],sum[2*x+1]);
}
void update_val(int pos,ll val)
{
this->pos=pos;
this->val=val;
Update(1,1,cprs.size());
}
ll get(int x,int low,int high)
{
if(low>r||high<l)
{
return inf;
}
if(low>=l&&high<=r)
{
return sum[x];
}
int mid=(low+high)/2;
ll get1=get(2*x,low,mid);
ll get2=get(2*x+1,mid+1,high);
return min(get1,get2);
}
ll query(int l,int r)
{
this->l=l;
this->r=r;
return get(1,1,cprs.size());
}
}st1,st2;
int nen(int x)
{
return lower_bound(cprs.begin(),cprs.end(),x)-cprs.begin()+1;
}
int main()
{
//freopen("","r",stdin);
//freopen("","w",stdout);
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>m>>n;
for(int i=1;i<=m;i++)
{
cin>>a[i]>>b[i]>>c[i]>>d[i];
cprs.push_back(a[i]);
cprs.push_back(b[i]);
cprs.push_back(c[i]);
dp[i]=inf;
f[i]=inf;
}
cprs.push_back(1);
cprs.push_back(n);
sort(cprs.begin(),cprs.end());
cprs.resize(unique(cprs.begin(),cprs.end())-cprs.begin());
for(int i=1;i<=m;i++)
{
a[i]=nen(a[i]);
b[i]=nen(b[i]);
c[i]=nen(c[i]);
}
st1.Build(1,1,cprs.size());
st2.Build(1,1,cprs.size());
st1.update_val(nen(1),0);
st2.update_val(nen(n),0);
dp[0]=0;
f[0]=0;
for(int i=1;i<=m;i++)
{
ll tmp1=st1.query(a[i],b[i]);
ll tmp2=st2.query(a[i],b[i]);
dp[i]=min(dp[i],tmp1+d[i]);
f[i]=min(f[i],tmp2+d[i]);
st1.update_val(c[i],dp[i]);
st2.update_val(c[i],f[i]);
//cout<<i<<" "<<dp[i]<<" "<<f[i]<<'\n';
}
ll ans=inf;
for(int i=1;i<=m;i++)
{
ans=min(ans,dp[i]+f[i]-d[i]);
}
if(ans==inf)
{
cout<<-1;
}
else cout<<ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
600 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
2 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
600 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
344 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
0 ms |
348 KB |
Output is correct |
17 |
Correct |
1 ms |
600 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
2 ms |
604 KB |
Output is correct |
20 |
Correct |
1 ms |
600 KB |
Output is correct |
21 |
Correct |
1 ms |
348 KB |
Output is correct |
22 |
Correct |
1 ms |
604 KB |
Output is correct |
23 |
Correct |
1 ms |
604 KB |
Output is correct |
24 |
Correct |
1 ms |
600 KB |
Output is correct |
25 |
Correct |
11 ms |
1884 KB |
Output is correct |
26 |
Correct |
30 ms |
3804 KB |
Output is correct |
27 |
Correct |
96 ms |
8000 KB |
Output is correct |
28 |
Correct |
84 ms |
5580 KB |
Output is correct |
29 |
Correct |
80 ms |
6816 KB |
Output is correct |
30 |
Correct |
108 ms |
6084 KB |
Output is correct |
31 |
Correct |
133 ms |
13484 KB |
Output is correct |
32 |
Correct |
144 ms |
9364 KB |
Output is correct |
33 |
Correct |
20 ms |
3548 KB |
Output is correct |
34 |
Correct |
69 ms |
10956 KB |
Output is correct |
35 |
Correct |
104 ms |
21704 KB |
Output is correct |
36 |
Correct |
155 ms |
21448 KB |
Output is correct |
37 |
Correct |
132 ms |
21704 KB |
Output is correct |
38 |
Correct |
159 ms |
21492 KB |
Output is correct |