#include <bits/stdc++.h>
using namespace std;
#define ll long long
#define endl "\n"
#define INF 1000000000
#define LINF 10000000000000000LL
#define pb push_back
#define all(x) x.begin(), x.end()
#define len(s) (int)s.size()
#define test_case { int t; cin>>t; while(t--)solve(); }
#define single_case solve();
#define line cerr<<"----------"<<endl;
#define ios { ios_base::sync_with_stdio(false); cin.tie(NULL); cout.tie(NULL); cerr.tie(NULL); }
#define mod 1000000007LL
const int N = 1e6 + 5;
ll a[N], b[N], c[N], d[N];
ll n, m;
ll seg[4*N];
ll l[N], r[N];
ll rev[N];
inline ll get_min(ll node, ll l, ll r, ll i, ll j)
{
if(i>r||j<l) return LINF;
if(i>=l&&j<=r) return seg[node];
ll mid = (i+j)/2;
return min(get_min(node*2+1, l, r, i, mid), get_min(node*2+2, l, r, mid+1, j));
}
inline void update(ll node, ll l, ll r, ll pos, ll x)
{
if(l==r)
{
seg[node] = min(seg[node], x);
return;
}
ll mid = (l+r)/2;
if(pos>mid) update(node*2+2, mid+1, r, pos, x);
else update(node*2+1, l, mid, pos, x);
seg[node] = min(seg[node*2+1], seg[node*2+2]);
}
map<ll, ll> mm;
vector<ll> s;
int main()
{
ios
cin>>m>>n;
for(int i = 0;i<m;i++)
{
cin>>a[i]>>b[i]>>c[i]>>d[i];
s.pb(a[i]);
s.pb(b[i]);
s.pb(c[i]);
}
sort(all(s));
ll val = 0;
for(int i = 0;i<len(s);i++)
{
if(!i||s[i]!=s[i-1]) mm[s[i]] = ++val;
}
for(int i = 0;i<4*N;i++) seg[i] = LINF;
for(int i = 0;i<m;i++)
{
if(a[i]==1)
{
l[i] = d[i];
update(0, 1, val, mm[c[i]], l[i]);
continue;
}
//cout<<mm[a[i]]<<" "<<mm[b[i]]<<endl;
ll mini = get_min(0, mm[a[i]], mm[b[i]], 1, val);
l[i] = min(LINF, mini+d[i]);
update(0, 1, val, mm[c[i]], l[i]);
}
for(int i = 0;i<4*N;i++) seg[i] = LINF;
for(int i = 0;i<m;i++)
{
if(b[i]==n)
{
r[i] = d[i];
update(0, 1, val, mm[c[i]], r[i]);
continue;
}
ll mini = get_min(0, mm[a[i]], mm[b[i]], 1, val);
r[i] = min(LINF, mini+d[i]);
update(0, 1, val, mm[c[i]], r[i]);
}
ll ans = LINF;
for(int i = 0;i<m;i++) ans = min(ans, l[i]+r[i]-d[i]);
//for(int i = 0;i<m;i++) cout<<l[i]<<" "<<r[i]<<endl;
if(ans==LINF) cout<<-1;
else cout<<ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
31724 KB |
Output is correct |
2 |
Correct |
23 ms |
31852 KB |
Output is correct |
3 |
Correct |
22 ms |
31724 KB |
Output is correct |
4 |
Correct |
23 ms |
31724 KB |
Output is correct |
5 |
Correct |
23 ms |
31724 KB |
Output is correct |
6 |
Correct |
23 ms |
31724 KB |
Output is correct |
7 |
Correct |
22 ms |
31724 KB |
Output is correct |
8 |
Correct |
23 ms |
31852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
31724 KB |
Output is correct |
2 |
Correct |
23 ms |
31852 KB |
Output is correct |
3 |
Correct |
22 ms |
31724 KB |
Output is correct |
4 |
Correct |
23 ms |
31724 KB |
Output is correct |
5 |
Correct |
23 ms |
31724 KB |
Output is correct |
6 |
Correct |
23 ms |
31724 KB |
Output is correct |
7 |
Correct |
22 ms |
31724 KB |
Output is correct |
8 |
Correct |
23 ms |
31852 KB |
Output is correct |
9 |
Correct |
25 ms |
31724 KB |
Output is correct |
10 |
Correct |
23 ms |
31800 KB |
Output is correct |
11 |
Correct |
23 ms |
31724 KB |
Output is correct |
12 |
Correct |
23 ms |
31724 KB |
Output is correct |
13 |
Correct |
23 ms |
31724 KB |
Output is correct |
14 |
Correct |
24 ms |
31724 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
31724 KB |
Output is correct |
2 |
Correct |
23 ms |
31852 KB |
Output is correct |
3 |
Correct |
22 ms |
31724 KB |
Output is correct |
4 |
Correct |
23 ms |
31724 KB |
Output is correct |
5 |
Correct |
23 ms |
31724 KB |
Output is correct |
6 |
Correct |
23 ms |
31724 KB |
Output is correct |
7 |
Correct |
22 ms |
31724 KB |
Output is correct |
8 |
Correct |
23 ms |
31852 KB |
Output is correct |
9 |
Correct |
25 ms |
31724 KB |
Output is correct |
10 |
Correct |
23 ms |
31800 KB |
Output is correct |
11 |
Correct |
23 ms |
31724 KB |
Output is correct |
12 |
Correct |
23 ms |
31724 KB |
Output is correct |
13 |
Correct |
23 ms |
31724 KB |
Output is correct |
14 |
Correct |
24 ms |
31724 KB |
Output is correct |
15 |
Correct |
23 ms |
31724 KB |
Output is correct |
16 |
Correct |
24 ms |
31724 KB |
Output is correct |
17 |
Correct |
25 ms |
31852 KB |
Output is correct |
18 |
Correct |
24 ms |
31852 KB |
Output is correct |
19 |
Correct |
26 ms |
31980 KB |
Output is correct |
20 |
Correct |
25 ms |
31852 KB |
Output is correct |
21 |
Correct |
24 ms |
31852 KB |
Output is correct |
22 |
Correct |
25 ms |
31980 KB |
Output is correct |
23 |
Correct |
27 ms |
31980 KB |
Output is correct |
24 |
Correct |
25 ms |
31980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
31724 KB |
Output is correct |
2 |
Correct |
23 ms |
31852 KB |
Output is correct |
3 |
Correct |
22 ms |
31724 KB |
Output is correct |
4 |
Correct |
23 ms |
31724 KB |
Output is correct |
5 |
Correct |
23 ms |
31724 KB |
Output is correct |
6 |
Correct |
23 ms |
31724 KB |
Output is correct |
7 |
Correct |
22 ms |
31724 KB |
Output is correct |
8 |
Correct |
23 ms |
31852 KB |
Output is correct |
9 |
Correct |
25 ms |
31724 KB |
Output is correct |
10 |
Correct |
23 ms |
31800 KB |
Output is correct |
11 |
Correct |
23 ms |
31724 KB |
Output is correct |
12 |
Correct |
23 ms |
31724 KB |
Output is correct |
13 |
Correct |
23 ms |
31724 KB |
Output is correct |
14 |
Correct |
24 ms |
31724 KB |
Output is correct |
15 |
Correct |
23 ms |
31724 KB |
Output is correct |
16 |
Correct |
24 ms |
31724 KB |
Output is correct |
17 |
Correct |
25 ms |
31852 KB |
Output is correct |
18 |
Correct |
24 ms |
31852 KB |
Output is correct |
19 |
Correct |
26 ms |
31980 KB |
Output is correct |
20 |
Correct |
25 ms |
31852 KB |
Output is correct |
21 |
Correct |
24 ms |
31852 KB |
Output is correct |
22 |
Correct |
25 ms |
31980 KB |
Output is correct |
23 |
Correct |
27 ms |
31980 KB |
Output is correct |
24 |
Correct |
25 ms |
31980 KB |
Output is correct |
25 |
Correct |
62 ms |
33520 KB |
Output is correct |
26 |
Correct |
161 ms |
36988 KB |
Output is correct |
27 |
Correct |
514 ms |
43140 KB |
Output is correct |
28 |
Correct |
212 ms |
38928 KB |
Output is correct |
29 |
Correct |
356 ms |
41060 KB |
Output is correct |
30 |
Correct |
333 ms |
39900 KB |
Output is correct |
31 |
Correct |
837 ms |
49372 KB |
Output is correct |
32 |
Correct |
718 ms |
46244 KB |
Output is correct |
33 |
Correct |
86 ms |
36844 KB |
Output is correct |
34 |
Correct |
387 ms |
44692 KB |
Output is correct |
35 |
Correct |
388 ms |
57564 KB |
Output is correct |
36 |
Correct |
997 ms |
57636 KB |
Output is correct |
37 |
Correct |
743 ms |
61576 KB |
Output is correct |
38 |
Correct |
679 ms |
61532 KB |
Output is correct |