#include<bits/stdc++.h>
#pragma GCC optimize("O3")
#pragma GCC target("avx2")
#define ll long long
#define pii pair<int,int>
#define pll pair<ll,ll>
#define plx pair<ll,int>
#define f first
#define s second
#define pb push_back
#define all(x) x.begin(),x.end()
#define vi vector<int>
using namespace std;
const int mxn=1e5+5;
int l[mxn],r[mxn],t[mxn],c[mxn],id[mxn],rid[mxn];
struct tree{
pii a[2*mxn];
void build(int sz,int x){
for(int i=0;i<sz;i++)a[i+sz]={l[rid[i+1]]+x*t[rid[i+1]],rid[i+1]};
for(int i=sz-1;i>0;i--)a[i]=min(a[2*i],a[2*i+1]);
}
void upd(int i,int amt,int sz){
i+=sz;a[i]={amt,amt};for(i>>=1;i;i>>=1)a[i]=min(a[2*i],a[2*i+1]);
}
pii qr(int l,int r,int sz,pii res={2e9,2e9}){
for(l+=sz,r+=sz;l<r;l>>=1,r>>=1){
if(l&1)res=min(res,a[l++]);
if(r&1)res=min(res,a[--r]);
}return res;
}
}tr[2];
ll dist[mxn]{0};
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);
int n,m;cin>>n>>m;vector<pii>vec;
for(int i=1;i<=m;i++)cin>>t[i]>>l[i]>>r[i]>>c[i],vec.pb({t[i],i});
sort(vec.begin(),vec.end());for(int i=0;i<m;i++)id[vec[i].s]=i+1,rid[i+1]=vec[i].s;
tr[0].build(m,-1);tr[1].build(m,1);priority_queue<plx,vector<plx>,greater<plx>>q;
for(int i=1;i<=m;i++)dist[i]=1e18;
for(int i=1;i<=m;i++){
if(l[i]==1){
dist[i]=c[i];q.push({dist[i],i});
tr[0].upd(id[i]-1,2e9,m);tr[1].upd(id[i]-1,2e9,m);
}
}
while(!q.empty()){
auto u=q.top();q.pop();
if(u.f>dist[u.s])continue;
pii x=tr[0].qr(0,id[u.s],m);
while(x.f<=r[u.s]-t[u.s]+1&&x.s!=2e9){
if(dist[x.s]>u.f+c[x.s])dist[x.s]=u.f+c[x.s],q.push({dist[x.s],x.s});
tr[0].upd(id[x.s]-1,2e9,m);
x=tr[0].qr(0,id[u.s],m);
if(x.s==2e9)break;
}x=tr[1].qr(id[u.s]-1,m,m);
while(x.f<=r[u.s]+t[u.s]+1&&x.s!=2e9){
if(dist[x.s]>u.f+c[x.s])dist[x.s]=u.f+c[x.s],q.push({dist[x.s],x.s});
tr[1].upd(id[x.s]-1,2e9,m);
x=tr[1].qr(id[u.s]-1,m,m);
}
}ll ans=1e18;
for(int i=1;i<=m;i++)if(r[i]==n)ans=min(ans,dist[i]);
cout<<(ans==1e18?-1:ans);
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
99 ms |
7640 KB |
Output is correct |
2 |
Correct |
58 ms |
7636 KB |
Output is correct |
3 |
Correct |
93 ms |
8912 KB |
Output is correct |
4 |
Correct |
115 ms |
8400 KB |
Output is correct |
5 |
Correct |
86 ms |
9552 KB |
Output is correct |
6 |
Correct |
84 ms |
7636 KB |
Output is correct |
7 |
Correct |
80 ms |
7628 KB |
Output is correct |
8 |
Correct |
32 ms |
7376 KB |
Output is correct |
9 |
Correct |
26 ms |
7640 KB |
Output is correct |
10 |
Correct |
26 ms |
7372 KB |
Output is correct |
11 |
Correct |
94 ms |
9556 KB |
Output is correct |
12 |
Correct |
94 ms |
9556 KB |
Output is correct |
13 |
Correct |
95 ms |
9632 KB |
Output is correct |
14 |
Correct |
97 ms |
9628 KB |
Output is correct |
15 |
Correct |
98 ms |
7564 KB |
Output is correct |
16 |
Correct |
101 ms |
7376 KB |
Output is correct |
17 |
Correct |
99 ms |
7640 KB |
Output is correct |
18 |
Correct |
81 ms |
9492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4440 KB |
Output is correct |
5 |
Correct |
1 ms |
4696 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
0 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
0 ms |
4444 KB |
Output is correct |
10 |
Correct |
0 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
1 ms |
4444 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
19 |
Correct |
1 ms |
4444 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
4444 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
1 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4440 KB |
Output is correct |
5 |
Correct |
1 ms |
4696 KB |
Output is correct |
6 |
Correct |
1 ms |
4444 KB |
Output is correct |
7 |
Correct |
0 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
0 ms |
4444 KB |
Output is correct |
10 |
Correct |
0 ms |
4444 KB |
Output is correct |
11 |
Correct |
1 ms |
4444 KB |
Output is correct |
12 |
Correct |
1 ms |
4444 KB |
Output is correct |
13 |
Correct |
1 ms |
4444 KB |
Output is correct |
14 |
Correct |
1 ms |
4444 KB |
Output is correct |
15 |
Correct |
1 ms |
4444 KB |
Output is correct |
16 |
Correct |
1 ms |
4444 KB |
Output is correct |
17 |
Correct |
1 ms |
4444 KB |
Output is correct |
18 |
Correct |
1 ms |
4444 KB |
Output is correct |
19 |
Correct |
1 ms |
4444 KB |
Output is correct |
20 |
Correct |
4 ms |
4700 KB |
Output is correct |
21 |
Correct |
4 ms |
4700 KB |
Output is correct |
22 |
Correct |
4 ms |
4700 KB |
Output is correct |
23 |
Correct |
4 ms |
4700 KB |
Output is correct |
24 |
Correct |
4 ms |
4700 KB |
Output is correct |
25 |
Correct |
4 ms |
4832 KB |
Output is correct |
26 |
Correct |
4 ms |
4588 KB |
Output is correct |
27 |
Correct |
4 ms |
4700 KB |
Output is correct |
28 |
Correct |
4 ms |
4700 KB |
Output is correct |
29 |
Correct |
4 ms |
4700 KB |
Output is correct |
30 |
Correct |
2 ms |
4700 KB |
Output is correct |
31 |
Correct |
4 ms |
4700 KB |
Output is correct |
32 |
Correct |
5 ms |
4956 KB |
Output is correct |
33 |
Correct |
4 ms |
4956 KB |
Output is correct |
34 |
Correct |
5 ms |
4752 KB |
Output is correct |
35 |
Correct |
4 ms |
4840 KB |
Output is correct |
36 |
Correct |
4 ms |
4956 KB |
Output is correct |
37 |
Correct |
5 ms |
4676 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
99 ms |
7640 KB |
Output is correct |
2 |
Correct |
58 ms |
7636 KB |
Output is correct |
3 |
Correct |
93 ms |
8912 KB |
Output is correct |
4 |
Correct |
115 ms |
8400 KB |
Output is correct |
5 |
Correct |
86 ms |
9552 KB |
Output is correct |
6 |
Correct |
84 ms |
7636 KB |
Output is correct |
7 |
Correct |
80 ms |
7628 KB |
Output is correct |
8 |
Correct |
32 ms |
7376 KB |
Output is correct |
9 |
Correct |
26 ms |
7640 KB |
Output is correct |
10 |
Correct |
26 ms |
7372 KB |
Output is correct |
11 |
Correct |
94 ms |
9556 KB |
Output is correct |
12 |
Correct |
94 ms |
9556 KB |
Output is correct |
13 |
Correct |
95 ms |
9632 KB |
Output is correct |
14 |
Correct |
97 ms |
9628 KB |
Output is correct |
15 |
Correct |
98 ms |
7564 KB |
Output is correct |
16 |
Correct |
101 ms |
7376 KB |
Output is correct |
17 |
Correct |
99 ms |
7640 KB |
Output is correct |
18 |
Correct |
81 ms |
9492 KB |
Output is correct |
19 |
Correct |
1 ms |
4444 KB |
Output is correct |
20 |
Correct |
1 ms |
4444 KB |
Output is correct |
21 |
Correct |
1 ms |
4444 KB |
Output is correct |
22 |
Correct |
1 ms |
4440 KB |
Output is correct |
23 |
Correct |
1 ms |
4696 KB |
Output is correct |
24 |
Correct |
1 ms |
4444 KB |
Output is correct |
25 |
Correct |
0 ms |
4444 KB |
Output is correct |
26 |
Correct |
1 ms |
4444 KB |
Output is correct |
27 |
Correct |
0 ms |
4444 KB |
Output is correct |
28 |
Correct |
0 ms |
4444 KB |
Output is correct |
29 |
Correct |
1 ms |
4444 KB |
Output is correct |
30 |
Correct |
1 ms |
4444 KB |
Output is correct |
31 |
Correct |
1 ms |
4444 KB |
Output is correct |
32 |
Correct |
1 ms |
4444 KB |
Output is correct |
33 |
Correct |
1 ms |
4444 KB |
Output is correct |
34 |
Correct |
1 ms |
4444 KB |
Output is correct |
35 |
Correct |
1 ms |
4444 KB |
Output is correct |
36 |
Correct |
1 ms |
4444 KB |
Output is correct |
37 |
Correct |
1 ms |
4444 KB |
Output is correct |
38 |
Correct |
4 ms |
4700 KB |
Output is correct |
39 |
Correct |
4 ms |
4700 KB |
Output is correct |
40 |
Correct |
4 ms |
4700 KB |
Output is correct |
41 |
Correct |
4 ms |
4700 KB |
Output is correct |
42 |
Correct |
4 ms |
4700 KB |
Output is correct |
43 |
Correct |
4 ms |
4832 KB |
Output is correct |
44 |
Correct |
4 ms |
4588 KB |
Output is correct |
45 |
Correct |
4 ms |
4700 KB |
Output is correct |
46 |
Correct |
4 ms |
4700 KB |
Output is correct |
47 |
Correct |
4 ms |
4700 KB |
Output is correct |
48 |
Correct |
2 ms |
4700 KB |
Output is correct |
49 |
Correct |
4 ms |
4700 KB |
Output is correct |
50 |
Correct |
5 ms |
4956 KB |
Output is correct |
51 |
Correct |
4 ms |
4956 KB |
Output is correct |
52 |
Correct |
5 ms |
4752 KB |
Output is correct |
53 |
Correct |
4 ms |
4840 KB |
Output is correct |
54 |
Correct |
4 ms |
4956 KB |
Output is correct |
55 |
Correct |
5 ms |
4676 KB |
Output is correct |
56 |
Correct |
78 ms |
8916 KB |
Output is correct |
57 |
Correct |
70 ms |
8932 KB |
Output is correct |
58 |
Correct |
65 ms |
7376 KB |
Output is correct |
59 |
Correct |
70 ms |
7372 KB |
Output is correct |
60 |
Correct |
73 ms |
7628 KB |
Output is correct |
61 |
Correct |
73 ms |
7376 KB |
Output is correct |
62 |
Correct |
72 ms |
8836 KB |
Output is correct |
63 |
Correct |
71 ms |
7376 KB |
Output is correct |
64 |
Correct |
69 ms |
7372 KB |
Output is correct |
65 |
Correct |
98 ms |
7452 KB |
Output is correct |
66 |
Correct |
57 ms |
7372 KB |
Output is correct |
67 |
Correct |
86 ms |
7556 KB |
Output is correct |
68 |
Correct |
99 ms |
7512 KB |
Output is correct |
69 |
Correct |
92 ms |
7376 KB |
Output is correct |
70 |
Correct |
97 ms |
7636 KB |
Output is correct |
71 |
Correct |
93 ms |
7376 KB |
Output is correct |
72 |
Correct |
91 ms |
7640 KB |
Output is correct |
73 |
Correct |
95 ms |
7456 KB |
Output is correct |
74 |
Correct |
28 ms |
7640 KB |
Output is correct |
75 |
Correct |
35 ms |
7544 KB |
Output is correct |
76 |
Correct |
101 ms |
9556 KB |
Output is correct |
77 |
Correct |
90 ms |
9552 KB |
Output is correct |
78 |
Correct |
103 ms |
9660 KB |
Output is correct |
79 |
Correct |
102 ms |
7500 KB |
Output is correct |
80 |
Correct |
104 ms |
7372 KB |
Output is correct |
81 |
Correct |
35 ms |
7544 KB |
Output is correct |
82 |
Correct |
93 ms |
7628 KB |
Output is correct |
83 |
Correct |
102 ms |
7628 KB |
Output is correct |
84 |
Correct |
94 ms |
7472 KB |
Output is correct |
85 |
Correct |
64 ms |
7464 KB |
Output is correct |
86 |
Correct |
69 ms |
7628 KB |
Output is correct |
87 |
Correct |
74 ms |
7376 KB |
Output is correct |
88 |
Correct |
95 ms |
7556 KB |
Output is correct |
89 |
Correct |
80 ms |
7376 KB |
Output is correct |
90 |
Correct |
100 ms |
9556 KB |
Output is correct |
91 |
Correct |
85 ms |
8908 KB |
Output is correct |
92 |
Correct |
69 ms |
7380 KB |
Output is correct |
93 |
Correct |
106 ms |
7372 KB |
Output is correct |
94 |
Correct |
95 ms |
7628 KB |
Output is correct |
95 |
Correct |
94 ms |
7372 KB |
Output is correct |
96 |
Correct |
94 ms |
9556 KB |
Output is correct |
97 |
Correct |
94 ms |
9588 KB |
Output is correct |
98 |
Correct |
95 ms |
9548 KB |
Output is correct |
99 |
Correct |
98 ms |
9552 KB |
Output is correct |
100 |
Correct |
92 ms |
9556 KB |
Output is correct |
101 |
Correct |
98 ms |
9488 KB |
Output is correct |
102 |
Correct |
103 ms |
9540 KB |
Output is correct |
103 |
Correct |
69 ms |
7888 KB |
Output is correct |