#include <bits/stdc++.h>
using namespace std;
#define int long long
#define ld long double
#define show(x,y) cout << y << " " << #x << endl;
#define show2(x,y,i,j) cout << y << " " << #x << " " << j << " " << #i << endl;
#define show3(x,y,i,j,p,q) cout << y << " " << #x << " " << j << " " << #i << " " << q << " " << #p << endl;
#define show4(x,y) for(auto it:y) cout << it << " "; cout << #x << endl;
typedef pair<long long,int>pii;
//typedef pair<pii,pii>pi2;
typedef array<int,3>pi2;
mt19937 rng(chrono::system_clock::now().time_since_epoch().count());
inline int combine(int a, int b){
return min(a,b);
}
struct node{
int s,e,m;
node *l,*r;
int v;
node(int ss, int ee):s(ss),e(ee),m((s+e)>>1),v(1e15){
if(s!=e){
l=new node(s,m);
r=new node(m+1,e);
}
}
void upd(int x, int y){
if(s==e){
v=min(v,y);
return;
}
if(x<=m) l->upd(x,y);
else r->upd(x,y);
v=combine(l->v,r->v);
}
int query(int x, int y){
if(x<=s&&y>=e){
return v;
}
if(y<=m) return l->query(x,y);
if(x>m) return r->query(x,y);
return combine(l->query(x,m),r->query(m+1,y));
}
};
void solve(){
int n,m;
cin >> n >> m;
array<int,4>arr[n];
vector<int>v;
for(int x=0;x<n;x++){
cin >> arr[x][0] >> arr[x][1] >> arr[x][2] >> arr[x][3];
v.push_back(arr[x][0]);
v.push_back(arr[x][1]);
v.push_back(arr[x][2]);
}
v.push_back(1);
v.push_back(m);
sort(v.begin(),v.end());
v.resize(unique(v.begin(),v.end())-v.begin());
for(int x=0;x<n;x++){
arr[x][0]=lower_bound(v.begin(),v.end(),arr[x][0])-v.begin()+1;
arr[x][1]=lower_bound(v.begin(),v.end(),arr[x][1])-v.begin()+1;
arr[x][2]=lower_bound(v.begin(),v.end(),arr[x][2])-v.begin()+1;
}
int sz=lower_bound(v.begin(),v.end(),m)-v.begin()+1;
node st(0,sz+5);
int prefix[n+5];
st.upd(1,0);
for(int x=0;x<n;x++){
prefix[x]=1e15;
int hold=st.query(arr[x][0],arr[x][1])+arr[x][3];
st.upd(arr[x][2],hold);
prefix[x]=hold;
}
node st2(0,sz+5);
int suffix[n+5];
st2.upd(sz,0);
for(int x=0;x<n;x++){
suffix[x]=1e15;
int hold=st2.query(arr[x][0],arr[x][1])+arr[x][3];
st2.upd(arr[x][2],hold);
suffix[x]=hold;
}
int best=1e16;
for(int x=0;x<n;x++){
//show2(prefix[x],prefix[x],suffix[x],suffix[x]);
best=min(best,prefix[x]+suffix[x]-arr[x][3]);
}
if(best>=1e15) cout << -1;
else cout << best;
}
int32_t main(){
ios::sync_with_stdio(0);
cin.tie(0);
int t=1;
//cin >> t;
while(t--){
solve();
}
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
348 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 |
1 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 |
348 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 |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
348 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 |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
2 ms |
860 KB |
Output is correct |
18 |
Correct |
1 ms |
600 KB |
Output is correct |
19 |
Correct |
2 ms |
1112 KB |
Output is correct |
20 |
Correct |
2 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
860 KB |
Output is correct |
22 |
Correct |
2 ms |
1116 KB |
Output is correct |
23 |
Correct |
2 ms |
1116 KB |
Output is correct |
24 |
Correct |
2 ms |
1112 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 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 |
348 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 |
348 KB |
Output is correct |
10 |
Correct |
1 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
604 KB |
Output is correct |
13 |
Correct |
1 ms |
604 KB |
Output is correct |
14 |
Correct |
1 ms |
604 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
604 KB |
Output is correct |
17 |
Correct |
2 ms |
860 KB |
Output is correct |
18 |
Correct |
1 ms |
600 KB |
Output is correct |
19 |
Correct |
2 ms |
1112 KB |
Output is correct |
20 |
Correct |
2 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
860 KB |
Output is correct |
22 |
Correct |
2 ms |
1116 KB |
Output is correct |
23 |
Correct |
2 ms |
1116 KB |
Output is correct |
24 |
Correct |
2 ms |
1112 KB |
Output is correct |
25 |
Correct |
17 ms |
5596 KB |
Output is correct |
26 |
Correct |
60 ms |
16844 KB |
Output is correct |
27 |
Correct |
158 ms |
33136 KB |
Output is correct |
28 |
Correct |
112 ms |
12232 KB |
Output is correct |
29 |
Correct |
111 ms |
28612 KB |
Output is correct |
30 |
Correct |
151 ms |
16088 KB |
Output is correct |
31 |
Correct |
274 ms |
54812 KB |
Output is correct |
32 |
Correct |
237 ms |
41656 KB |
Output is correct |
33 |
Correct |
30 ms |
17164 KB |
Output is correct |
34 |
Correct |
129 ms |
43380 KB |
Output is correct |
35 |
Correct |
175 ms |
87124 KB |
Output is correct |
36 |
Correct |
378 ms |
86456 KB |
Output is correct |
37 |
Correct |
291 ms |
86228 KB |
Output is correct |
38 |
Correct |
267 ms |
86208 KB |
Output is correct |