#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define MAXN (300005)
struct node{
ll s,e,m,v,lazyadd;
node *l,*r;
node(ll _s,ll _e){
s = _s;
e = _e;
m = (s + e) / 2;
v = 1e18;
lazyadd = 1e18;
if(s != e){
l = new node(s,m);
r = new node(m + 1,e);
}
}
ll value(){
if(s == e){
v = min(v,lazyadd);
return v;
}
v = min(v,lazyadd);
r->lazyadd = min(r -> lazyadd,lazyadd);
l->lazyadd = min(l -> lazyadd,lazyadd);
return v;
}
void update(ll x,ll y,ll val){
if(s == x && e == y) lazyadd = min(lazyadd,val);
else{
if(x > m) r -> update(x,y,val);
else if(y <= m) l -> update(x,y,val);
else l -> update(x,m,val), r -> update(m + 1,y,val);
v = min(l->value(),r->value());
}
}
ll rmq(ll x,ll y){
value();
if(s == x && e == y) return value();
if(x > m) return r -> rmq(x,y);
if(y <= m) return l -> rmq(x,y);
return min(l -> rmq(x,m),r -> rmq(m + 1,y));
}
} *root;
int main() {
ios_base::sync_with_stdio(false);cin.tie(0);
ll M,N;
cin>>M>>N;
root = new node(0,MAXN);
pair<ll,ll> range[M];
ll A[M], B[M], C[M], D[M];
vector<ll> d;
for(ll i = 0;i < M;i++){
cin>>range[i].first>>range[i].second>>C[i]>>D[i];
d.push_back(range[i].first);
d.push_back(range[i].second);
d.push_back(C[i]);
A[i] = range[i].first;
B[i] = range[i].second;
}
sort(d.begin(),d.end());
d.resize(unique(d.begin(),d.end()) - d.begin());
for(ll i = 0;i < M;i++){
range[i].first = lower_bound(d.begin(),d.end(),range[i].first) - d.begin();
range[i].second = lower_bound(d.begin(),d.end(),range[i].second) - d.begin();
C[i] = lower_bound(d.begin(),d.end(),C[i]) - d.begin();
}
ll dp1[M]; //start from column 1
for(ll i = 0;i < M;i++){
if(A[i] == 1) dp1[i] = D[i];
else dp1[i] = 1e18;
}
for(ll i = 0;i < M;i++){
ll prev = root -> rmq(range[i].first,range[i].second);
if(A[i] == 1){
dp1[i] = D[i];
}else{
if(prev < 1e18) dp1[i] = prev + D[i];
}
if(dp1[i] < 1e18) root -> update(C[i],C[i],dp1[i]);
}
root = new node(0,MAXN);
ll dpN[M]; //start from column N
for(ll i = 0;i < M;i++){
if(B[i] == N) dpN[i] = D[i];
else dpN[i] = 1e18;
}
for(ll i = 0;i < M;i++){
ll prev = root -> rmq(range[i].first,range[i].second);
if(B[i] == N){
dpN[i] = D[i];
}else{
if(prev < 1e18) dpN[i] = prev + D[i];
}
if(dpN[i] < 1e18) root -> update(C[i],C[i],dpN[i]);
}
ll ans = 2e18 + 5;
for(ll i = 0;i < M;i++){
if(dp1[i] < 1e18 && dpN[i] < 1e18){
ans = min(ans,dp1[i] + dpN[i] - D[i]);
}
}
if(ans >= 1e18) cout<<-1<<'\n';
else cout<<ans<<'\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
51 ms |
75340 KB |
Output is correct |
2 |
Correct |
72 ms |
75416 KB |
Output is correct |
3 |
Correct |
57 ms |
75380 KB |
Output is correct |
4 |
Correct |
53 ms |
75340 KB |
Output is correct |
5 |
Correct |
54 ms |
75324 KB |
Output is correct |
6 |
Correct |
53 ms |
75392 KB |
Output is correct |
7 |
Correct |
52 ms |
75452 KB |
Output is correct |
8 |
Correct |
54 ms |
75340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
51 ms |
75340 KB |
Output is correct |
2 |
Correct |
72 ms |
75416 KB |
Output is correct |
3 |
Correct |
57 ms |
75380 KB |
Output is correct |
4 |
Correct |
53 ms |
75340 KB |
Output is correct |
5 |
Correct |
54 ms |
75324 KB |
Output is correct |
6 |
Correct |
53 ms |
75392 KB |
Output is correct |
7 |
Correct |
52 ms |
75452 KB |
Output is correct |
8 |
Correct |
54 ms |
75340 KB |
Output is correct |
9 |
Correct |
54 ms |
75472 KB |
Output is correct |
10 |
Correct |
55 ms |
75368 KB |
Output is correct |
11 |
Correct |
53 ms |
75396 KB |
Output is correct |
12 |
Correct |
56 ms |
75360 KB |
Output is correct |
13 |
Correct |
52 ms |
75416 KB |
Output is correct |
14 |
Correct |
61 ms |
75416 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
51 ms |
75340 KB |
Output is correct |
2 |
Correct |
72 ms |
75416 KB |
Output is correct |
3 |
Correct |
57 ms |
75380 KB |
Output is correct |
4 |
Correct |
53 ms |
75340 KB |
Output is correct |
5 |
Correct |
54 ms |
75324 KB |
Output is correct |
6 |
Correct |
53 ms |
75392 KB |
Output is correct |
7 |
Correct |
52 ms |
75452 KB |
Output is correct |
8 |
Correct |
54 ms |
75340 KB |
Output is correct |
9 |
Correct |
54 ms |
75472 KB |
Output is correct |
10 |
Correct |
55 ms |
75368 KB |
Output is correct |
11 |
Correct |
53 ms |
75396 KB |
Output is correct |
12 |
Correct |
56 ms |
75360 KB |
Output is correct |
13 |
Correct |
52 ms |
75416 KB |
Output is correct |
14 |
Correct |
61 ms |
75416 KB |
Output is correct |
15 |
Correct |
57 ms |
75420 KB |
Output is correct |
16 |
Correct |
65 ms |
75424 KB |
Output is correct |
17 |
Correct |
55 ms |
75580 KB |
Output is correct |
18 |
Correct |
60 ms |
75468 KB |
Output is correct |
19 |
Correct |
55 ms |
75496 KB |
Output is correct |
20 |
Correct |
59 ms |
75588 KB |
Output is correct |
21 |
Correct |
69 ms |
75400 KB |
Output is correct |
22 |
Correct |
55 ms |
75468 KB |
Output is correct |
23 |
Correct |
54 ms |
75488 KB |
Output is correct |
24 |
Correct |
54 ms |
75468 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
51 ms |
75340 KB |
Output is correct |
2 |
Correct |
72 ms |
75416 KB |
Output is correct |
3 |
Correct |
57 ms |
75380 KB |
Output is correct |
4 |
Correct |
53 ms |
75340 KB |
Output is correct |
5 |
Correct |
54 ms |
75324 KB |
Output is correct |
6 |
Correct |
53 ms |
75392 KB |
Output is correct |
7 |
Correct |
52 ms |
75452 KB |
Output is correct |
8 |
Correct |
54 ms |
75340 KB |
Output is correct |
9 |
Correct |
54 ms |
75472 KB |
Output is correct |
10 |
Correct |
55 ms |
75368 KB |
Output is correct |
11 |
Correct |
53 ms |
75396 KB |
Output is correct |
12 |
Correct |
56 ms |
75360 KB |
Output is correct |
13 |
Correct |
52 ms |
75416 KB |
Output is correct |
14 |
Correct |
61 ms |
75416 KB |
Output is correct |
15 |
Correct |
57 ms |
75420 KB |
Output is correct |
16 |
Correct |
65 ms |
75424 KB |
Output is correct |
17 |
Correct |
55 ms |
75580 KB |
Output is correct |
18 |
Correct |
60 ms |
75468 KB |
Output is correct |
19 |
Correct |
55 ms |
75496 KB |
Output is correct |
20 |
Correct |
59 ms |
75588 KB |
Output is correct |
21 |
Correct |
69 ms |
75400 KB |
Output is correct |
22 |
Correct |
55 ms |
75468 KB |
Output is correct |
23 |
Correct |
54 ms |
75488 KB |
Output is correct |
24 |
Correct |
54 ms |
75468 KB |
Output is correct |
25 |
Correct |
85 ms |
76496 KB |
Output is correct |
26 |
Correct |
154 ms |
77880 KB |
Output is correct |
27 |
Correct |
351 ms |
81656 KB |
Output is correct |
28 |
Correct |
296 ms |
84276 KB |
Output is correct |
29 |
Correct |
251 ms |
80024 KB |
Output is correct |
30 |
Correct |
354 ms |
84312 KB |
Output is correct |
31 |
Correct |
595 ms |
87888 KB |
Output is correct |
32 |
Correct |
551 ms |
87944 KB |
Output is correct |
33 |
Correct |
98 ms |
77804 KB |
Output is correct |
34 |
Correct |
223 ms |
81772 KB |
Output is correct |
35 |
Correct |
249 ms |
87984 KB |
Output is correct |
36 |
Correct |
587 ms |
87860 KB |
Output is correct |
37 |
Correct |
437 ms |
87948 KB |
Output is correct |
38 |
Correct |
364 ms |
87848 KB |
Output is correct |