#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define MAXN (1000005)
struct node{
ll s,e,m,val,lazy;
node *l = nullptr,*r = nullptr;
node(ll S,ll E){
s = S;
e = E;
m = (s + e) / 2;
val = 1e18;
lazy = 1e18;
}
void create(){
if(l == nullptr && s != e){
l = new node(s,m);
r = new node(m + 1,e);
}
}
void propagate(){
create();
if(lazy == 1e18) return;
val = min(val,lazy);
if(s != e){
l->lazy = min(l -> lazy,lazy);
r->lazy = min(r -> lazy,lazy);
}
}
void update(ll S,ll E,ll v){
if(s == S && e == E) lazy = min(lazy,v);
else{
create();
if(E <= m) l->update(S,E,v);
else if(S > m) r->update(S,E,v);
else l->update(S,m,v),r->update(m + 1,E,v);
l->propagate(),r->propagate();
val = min(l->val,r->val);
}
}
long long query(ll S,ll E){
create();
propagate();
if(s == S && e == E) return val;
else if(S > m) return r->query(S,E);
else if(E <= m) return l->query(S,E);
else return min(l->query(S,m),r-> query(m + 1,E));
}
} *root;
int main() {
ios_base::sync_with_stdio(false);cin.tie(0);
ll M,N;
cin>>M>>N;
root = new node(0,N + 5);
pair<ll,ll> range[M];
ll 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];
}
ll dp1[M]; //start from column 1
for(ll i = 0;i < M;i++){
if(range[i].first == 1) dp1[i] = D[i];
else dp1[i] = 1e18;
}
for(ll i = 0;i < M;i++){
ll prev = root -> query(range[i].first,range[i].second);
if(range[i].first == 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,N + 5);
ll dpN[M]; //start from column N
for(ll i = 0;i < M;i++){
if(range[i].second == N) dpN[i] = D[i];
else dpN[i] = 1e18;
}
for(ll i = 0;i < M;i++){
ll prev = root -> query(range[i].first,range[i].second);
if(range[i].second == 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 |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
320 KB |
Output is correct |
6 |
Correct |
1 ms |
316 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
320 KB |
Output is correct |
6 |
Correct |
1 ms |
316 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
2 ms |
1216 KB |
Output is correct |
11 |
Correct |
2 ms |
1748 KB |
Output is correct |
12 |
Correct |
3 ms |
3276 KB |
Output is correct |
13 |
Correct |
5 ms |
3540 KB |
Output is correct |
14 |
Correct |
4 ms |
4052 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
320 KB |
Output is correct |
6 |
Correct |
1 ms |
316 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
2 ms |
1216 KB |
Output is correct |
11 |
Correct |
2 ms |
1748 KB |
Output is correct |
12 |
Correct |
3 ms |
3276 KB |
Output is correct |
13 |
Correct |
5 ms |
3540 KB |
Output is correct |
14 |
Correct |
4 ms |
4052 KB |
Output is correct |
15 |
Correct |
1 ms |
596 KB |
Output is correct |
16 |
Correct |
2 ms |
2388 KB |
Output is correct |
17 |
Correct |
10 ms |
8644 KB |
Output is correct |
18 |
Correct |
3 ms |
720 KB |
Output is correct |
19 |
Correct |
15 ms |
15248 KB |
Output is correct |
20 |
Correct |
7 ms |
6100 KB |
Output is correct |
21 |
Correct |
4 ms |
3920 KB |
Output is correct |
22 |
Correct |
12 ms |
12884 KB |
Output is correct |
23 |
Correct |
15 ms |
16840 KB |
Output is correct |
24 |
Correct |
14 ms |
15600 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
324 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
320 KB |
Output is correct |
6 |
Correct |
1 ms |
316 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
596 KB |
Output is correct |
10 |
Correct |
2 ms |
1216 KB |
Output is correct |
11 |
Correct |
2 ms |
1748 KB |
Output is correct |
12 |
Correct |
3 ms |
3276 KB |
Output is correct |
13 |
Correct |
5 ms |
3540 KB |
Output is correct |
14 |
Correct |
4 ms |
4052 KB |
Output is correct |
15 |
Correct |
1 ms |
596 KB |
Output is correct |
16 |
Correct |
2 ms |
2388 KB |
Output is correct |
17 |
Correct |
10 ms |
8644 KB |
Output is correct |
18 |
Correct |
3 ms |
720 KB |
Output is correct |
19 |
Correct |
15 ms |
15248 KB |
Output is correct |
20 |
Correct |
7 ms |
6100 KB |
Output is correct |
21 |
Correct |
4 ms |
3920 KB |
Output is correct |
22 |
Correct |
12 ms |
12884 KB |
Output is correct |
23 |
Correct |
15 ms |
16840 KB |
Output is correct |
24 |
Correct |
14 ms |
15600 KB |
Output is correct |
25 |
Correct |
55 ms |
31208 KB |
Output is correct |
26 |
Correct |
254 ms |
171832 KB |
Output is correct |
27 |
Correct |
702 ms |
312360 KB |
Output is correct |
28 |
Correct |
308 ms |
15316 KB |
Output is correct |
29 |
Correct |
573 ms |
357188 KB |
Output is correct |
30 |
Correct |
725 ms |
81580 KB |
Output is correct |
31 |
Runtime error |
916 ms |
524288 KB |
Execution killed with signal 9 |
32 |
Halted |
0 ms |
0 KB |
- |