#include "bits/stdc++.h"
using namespace std;
#define pb push_back
#define endl "\n"
#define int long long
#define sz(x) ((int)(x).size())
#define all(x) (x).begin(),(x).end()
constexpr int INF = 1e18;
void update(int pos, int val, vector<int>& tree) {
int n = sz(tree)/2;
pos += n;
while(pos > 0) {
tree[pos] = min(tree[pos], val);
pos >>= 1;
}
}
int query(int l, int r, vector<int>& tree) {
int ans = INF;
int n = sz(tree)/2;
l += n,r += n;
for(; l <= r; l>>=1, r>>=1) {
if(l%2==1) ans = min(ans, tree[l++]);
if(r%2==0) ans = min(ans, tree[r--]);
}
return ans;
}
void solve(){
int m,n;
cin >> m >> n;
map<int,int> mp;
mp[1]=mp[n]=1;
array<int,4> ar[m+1];
for(int i=1;i<=m;i++){
int a,b,c,d;
cin >> a >> b >> c >> d;
ar[i]={a,b,c,d};
mp[a]=mp[b]=mp[c]=1;
}
int p=0;
for(auto x:mp) mp[x.first]=p++;
vector<int> treel(2*p,INF),treer(2*p,INF);
update(0,0,treel);
update(p-1,0,treer);
vector<int> dpl(m+1,INF);
vector<int> dpr(m+1,INF);
for(int i=1;i<=m;i++) {
int curl=query(mp[ar[i][0]],mp[ar[i][1]],treel);
dpl[i]=min(dpl[i],curl+ar[i][3]);
update(mp[ar[i][2]],dpl[i],treel);
int curr=query(mp[ar[i][0]],mp[ar[i][1]],treer);
dpr[i]=min(dpr[i],curr+ar[i][3]);
update(mp[ar[i][2]],dpr[i],treer);
}
int best=INF;
for(int i=1;i<=m;i++) best=min(best,dpl[i]+dpr[i]-ar[i][3]);
if(best==INF) cout << -1 << endl;
else cout << best << endl;
}
int32_t main(){
cin.tie(0); ios::sync_with_stdio(0);
int t=1;//cin >> t;
while(t--) solve();
return 0;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
600 KB |
Output is correct |
17 |
Correct |
2 ms |
600 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
2 ms |
600 KB |
Output is correct |
20 |
Correct |
2 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
600 KB |
Output is correct |
22 |
Correct |
3 ms |
600 KB |
Output is correct |
23 |
Correct |
2 ms |
600 KB |
Output is correct |
24 |
Correct |
2 ms |
600 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
344 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
1 ms |
344 KB |
Output is correct |
10 |
Correct |
1 ms |
344 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
344 KB |
Output is correct |
16 |
Correct |
1 ms |
600 KB |
Output is correct |
17 |
Correct |
2 ms |
600 KB |
Output is correct |
18 |
Correct |
1 ms |
344 KB |
Output is correct |
19 |
Correct |
2 ms |
600 KB |
Output is correct |
20 |
Correct |
2 ms |
604 KB |
Output is correct |
21 |
Correct |
1 ms |
600 KB |
Output is correct |
22 |
Correct |
3 ms |
600 KB |
Output is correct |
23 |
Correct |
2 ms |
600 KB |
Output is correct |
24 |
Correct |
2 ms |
600 KB |
Output is correct |
25 |
Correct |
18 ms |
2648 KB |
Output is correct |
26 |
Correct |
55 ms |
7736 KB |
Output is correct |
27 |
Correct |
156 ms |
15696 KB |
Output is correct |
28 |
Correct |
115 ms |
8888 KB |
Output is correct |
29 |
Correct |
139 ms |
13300 KB |
Output is correct |
30 |
Correct |
162 ms |
10576 KB |
Output is correct |
31 |
Correct |
354 ms |
24912 KB |
Output is correct |
32 |
Correct |
291 ms |
20360 KB |
Output is correct |
33 |
Correct |
35 ms |
7580 KB |
Output is correct |
34 |
Correct |
168 ms |
18908 KB |
Output is correct |
35 |
Correct |
177 ms |
37148 KB |
Output is correct |
36 |
Correct |
478 ms |
37192 KB |
Output is correct |
37 |
Correct |
386 ms |
36964 KB |
Output is correct |
38 |
Correct |
310 ms |
37160 KB |
Output is correct |