#include<bits/stdc++.h>
using namespace std;
int n,m;
long long inf=1e15,kaf=(1<<19);
struct segment{
long long seg[(1<<20)];
segment(){
for(int i=0;i<(1<<20);i++){
seg[i]=inf;
}
}
void upd(int i,long long w){
if(i==0){
return ;
}
seg[i]=min(seg[i],w);
return upd((i>>1),w);
}
long long pors(int i,int l,int r,int tl,int tr){
if(l>r||l>tr||r<tl){
return inf;
}
if(l>=tl&&r<=tr){
return seg[i];
}
int m=(l+r)>>1;
long long ret=min(pors((i<<1),l,m,tl,tr),pors((i<<1)^1,m+1,r,tl,tr));
return ret;
}
};
segment seg1,seg2;
int main(){
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin>>n>>m;
long long res=inf;
vector<pair<pair<int,int>,pair<int,int>>>allq(n);
vector<int>allind;
for(int i=0;i<n;i++){
cin>>allq[i].first.first>>allq[i].first.second>>allq[i].second.first>>allq[i].second.second;
allind.push_back(allq[i].first.first);
allind.push_back(allq[i].first.second);
allind.push_back(allq[i].second.first);
}
allind.push_back(1);
allind.push_back(m);
sort(allind.begin(),allind.end());
allind.resize(unique(allind.begin(),allind.end())-allind.begin());
int p=lower_bound(allind.begin(),allind.end(),1)-allind.begin();
seg1.upd(kaf+p,0);
p=lower_bound(allind.begin(),allind.end(),m)-allind.begin();
seg2.upd(kaf+p,0);
for(int i=0;i<n;i++){
long long l,r,c,w;
c=lower_bound(allind.begin(),allind.end(),allq[i].second.first)-allind.begin();
w=allq[i].second.second;
l=lower_bound(allind.begin(),allind.end(),allq[i].first.first)-allind.begin();
r=lower_bound(allind.begin(),allind.end(),allq[i].first.second)-allind.begin();
//cin>>l>>r>>c>>w;
long long fake=seg1.pors(1,0,kaf-1,l,r)+seg2.pors(1,0,kaf-1,l,r);
fake+=w;
res=min(fake,res);
fake=seg1.pors(1,0,kaf-1,l,r);
fake+=w;
seg1.upd(kaf+c,fake);
fake=seg2.pors(1,0,kaf-1,l,r);
fake+=w;
seg2.upd(kaf+c,fake);
}
if(res>=inf){
cout<<-1<<"\n";
}
else{
cout<<res<<'\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
16724 KB |
Output is correct |
2 |
Correct |
7 ms |
16724 KB |
Output is correct |
3 |
Correct |
7 ms |
16708 KB |
Output is correct |
4 |
Correct |
7 ms |
16656 KB |
Output is correct |
5 |
Correct |
7 ms |
16700 KB |
Output is correct |
6 |
Correct |
8 ms |
16724 KB |
Output is correct |
7 |
Correct |
8 ms |
16724 KB |
Output is correct |
8 |
Correct |
8 ms |
16724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
16724 KB |
Output is correct |
2 |
Correct |
7 ms |
16724 KB |
Output is correct |
3 |
Correct |
7 ms |
16708 KB |
Output is correct |
4 |
Correct |
7 ms |
16656 KB |
Output is correct |
5 |
Correct |
7 ms |
16700 KB |
Output is correct |
6 |
Correct |
8 ms |
16724 KB |
Output is correct |
7 |
Correct |
8 ms |
16724 KB |
Output is correct |
8 |
Correct |
8 ms |
16724 KB |
Output is correct |
9 |
Correct |
7 ms |
16724 KB |
Output is correct |
10 |
Correct |
8 ms |
16724 KB |
Output is correct |
11 |
Correct |
7 ms |
16724 KB |
Output is correct |
12 |
Correct |
7 ms |
16852 KB |
Output is correct |
13 |
Correct |
8 ms |
16684 KB |
Output is correct |
14 |
Correct |
8 ms |
16724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
16724 KB |
Output is correct |
2 |
Correct |
7 ms |
16724 KB |
Output is correct |
3 |
Correct |
7 ms |
16708 KB |
Output is correct |
4 |
Correct |
7 ms |
16656 KB |
Output is correct |
5 |
Correct |
7 ms |
16700 KB |
Output is correct |
6 |
Correct |
8 ms |
16724 KB |
Output is correct |
7 |
Correct |
8 ms |
16724 KB |
Output is correct |
8 |
Correct |
8 ms |
16724 KB |
Output is correct |
9 |
Correct |
7 ms |
16724 KB |
Output is correct |
10 |
Correct |
8 ms |
16724 KB |
Output is correct |
11 |
Correct |
7 ms |
16724 KB |
Output is correct |
12 |
Correct |
7 ms |
16852 KB |
Output is correct |
13 |
Correct |
8 ms |
16684 KB |
Output is correct |
14 |
Correct |
8 ms |
16724 KB |
Output is correct |
15 |
Correct |
7 ms |
16724 KB |
Output is correct |
16 |
Correct |
7 ms |
16724 KB |
Output is correct |
17 |
Correct |
9 ms |
16736 KB |
Output is correct |
18 |
Correct |
9 ms |
16684 KB |
Output is correct |
19 |
Correct |
9 ms |
16724 KB |
Output is correct |
20 |
Correct |
9 ms |
16724 KB |
Output is correct |
21 |
Correct |
8 ms |
16724 KB |
Output is correct |
22 |
Correct |
9 ms |
16732 KB |
Output is correct |
23 |
Correct |
8 ms |
16752 KB |
Output is correct |
24 |
Correct |
8 ms |
16724 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
7 ms |
16724 KB |
Output is correct |
2 |
Correct |
7 ms |
16724 KB |
Output is correct |
3 |
Correct |
7 ms |
16708 KB |
Output is correct |
4 |
Correct |
7 ms |
16656 KB |
Output is correct |
5 |
Correct |
7 ms |
16700 KB |
Output is correct |
6 |
Correct |
8 ms |
16724 KB |
Output is correct |
7 |
Correct |
8 ms |
16724 KB |
Output is correct |
8 |
Correct |
8 ms |
16724 KB |
Output is correct |
9 |
Correct |
7 ms |
16724 KB |
Output is correct |
10 |
Correct |
8 ms |
16724 KB |
Output is correct |
11 |
Correct |
7 ms |
16724 KB |
Output is correct |
12 |
Correct |
7 ms |
16852 KB |
Output is correct |
13 |
Correct |
8 ms |
16684 KB |
Output is correct |
14 |
Correct |
8 ms |
16724 KB |
Output is correct |
15 |
Correct |
7 ms |
16724 KB |
Output is correct |
16 |
Correct |
7 ms |
16724 KB |
Output is correct |
17 |
Correct |
9 ms |
16736 KB |
Output is correct |
18 |
Correct |
9 ms |
16684 KB |
Output is correct |
19 |
Correct |
9 ms |
16724 KB |
Output is correct |
20 |
Correct |
9 ms |
16724 KB |
Output is correct |
21 |
Correct |
8 ms |
16724 KB |
Output is correct |
22 |
Correct |
9 ms |
16732 KB |
Output is correct |
23 |
Correct |
8 ms |
16752 KB |
Output is correct |
24 |
Correct |
8 ms |
16724 KB |
Output is correct |
25 |
Correct |
25 ms |
17108 KB |
Output is correct |
26 |
Correct |
55 ms |
17748 KB |
Output is correct |
27 |
Correct |
155 ms |
19008 KB |
Output is correct |
28 |
Correct |
140 ms |
20424 KB |
Output is correct |
29 |
Correct |
109 ms |
18588 KB |
Output is correct |
30 |
Correct |
173 ms |
20452 KB |
Output is correct |
31 |
Correct |
226 ms |
20432 KB |
Output is correct |
32 |
Correct |
208 ms |
20416 KB |
Output is correct |
33 |
Correct |
33 ms |
17368 KB |
Output is correct |
34 |
Correct |
104 ms |
18616 KB |
Output is correct |
35 |
Correct |
158 ms |
20452 KB |
Output is correct |
36 |
Correct |
265 ms |
20496 KB |
Output is correct |
37 |
Correct |
204 ms |
20492 KB |
Output is correct |
38 |
Correct |
205 ms |
20368 KB |
Output is correct |