#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=2e5+5;
int l[mxn],r[mxn],t[mxn],c[mxn],id[mxn],rid[mxn];
struct tree{
pii a[4*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 |
90 ms |
15832 KB |
Output is correct |
2 |
Correct |
53 ms |
15824 KB |
Output is correct |
3 |
Correct |
99 ms |
17220 KB |
Output is correct |
4 |
Correct |
105 ms |
16708 KB |
Output is correct |
5 |
Correct |
86 ms |
17748 KB |
Output is correct |
6 |
Correct |
74 ms |
15984 KB |
Output is correct |
7 |
Correct |
75 ms |
15820 KB |
Output is correct |
8 |
Correct |
27 ms |
15752 KB |
Output is correct |
9 |
Correct |
28 ms |
15884 KB |
Output is correct |
10 |
Correct |
34 ms |
15688 KB |
Output is correct |
11 |
Correct |
95 ms |
17984 KB |
Output is correct |
12 |
Correct |
83 ms |
17744 KB |
Output is correct |
13 |
Correct |
96 ms |
17740 KB |
Output is correct |
14 |
Correct |
92 ms |
17740 KB |
Output is correct |
15 |
Correct |
99 ms |
15824 KB |
Output is correct |
16 |
Correct |
100 ms |
15828 KB |
Output is correct |
17 |
Correct |
97 ms |
15828 KB |
Output is correct |
18 |
Correct |
76 ms |
17756 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
10588 KB |
Output is correct |
2 |
Correct |
1 ms |
10588 KB |
Output is correct |
3 |
Correct |
1 ms |
10588 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
1 ms |
10588 KB |
Output is correct |
7 |
Correct |
1 ms |
10588 KB |
Output is correct |
8 |
Correct |
1 ms |
10588 KB |
Output is correct |
9 |
Correct |
1 ms |
10584 KB |
Output is correct |
10 |
Correct |
1 ms |
10588 KB |
Output is correct |
11 |
Correct |
1 ms |
10588 KB |
Output is correct |
12 |
Correct |
1 ms |
10588 KB |
Output is correct |
13 |
Correct |
1 ms |
10704 KB |
Output is correct |
14 |
Correct |
1 ms |
10588 KB |
Output is correct |
15 |
Correct |
1 ms |
10588 KB |
Output is correct |
16 |
Correct |
1 ms |
10588 KB |
Output is correct |
17 |
Correct |
1 ms |
10588 KB |
Output is correct |
18 |
Correct |
1 ms |
10588 KB |
Output is correct |
19 |
Correct |
1 ms |
10588 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
10588 KB |
Output is correct |
2 |
Correct |
1 ms |
10588 KB |
Output is correct |
3 |
Correct |
1 ms |
10588 KB |
Output is correct |
4 |
Correct |
1 ms |
10588 KB |
Output is correct |
5 |
Correct |
1 ms |
10588 KB |
Output is correct |
6 |
Correct |
1 ms |
10588 KB |
Output is correct |
7 |
Correct |
1 ms |
10588 KB |
Output is correct |
8 |
Correct |
1 ms |
10588 KB |
Output is correct |
9 |
Correct |
1 ms |
10584 KB |
Output is correct |
10 |
Correct |
1 ms |
10588 KB |
Output is correct |
11 |
Correct |
1 ms |
10588 KB |
Output is correct |
12 |
Correct |
1 ms |
10588 KB |
Output is correct |
13 |
Correct |
1 ms |
10704 KB |
Output is correct |
14 |
Correct |
1 ms |
10588 KB |
Output is correct |
15 |
Correct |
1 ms |
10588 KB |
Output is correct |
16 |
Correct |
1 ms |
10588 KB |
Output is correct |
17 |
Correct |
1 ms |
10588 KB |
Output is correct |
18 |
Correct |
1 ms |
10588 KB |
Output is correct |
19 |
Correct |
1 ms |
10588 KB |
Output is correct |
20 |
Correct |
4 ms |
10840 KB |
Output is correct |
21 |
Correct |
4 ms |
10844 KB |
Output is correct |
22 |
Correct |
4 ms |
10844 KB |
Output is correct |
23 |
Correct |
4 ms |
10844 KB |
Output is correct |
24 |
Correct |
4 ms |
10844 KB |
Output is correct |
25 |
Correct |
5 ms |
10840 KB |
Output is correct |
26 |
Correct |
4 ms |
10844 KB |
Output is correct |
27 |
Correct |
4 ms |
10844 KB |
Output is correct |
28 |
Correct |
4 ms |
10844 KB |
Output is correct |
29 |
Correct |
4 ms |
10844 KB |
Output is correct |
30 |
Correct |
3 ms |
10844 KB |
Output is correct |
31 |
Correct |
3 ms |
10844 KB |
Output is correct |
32 |
Correct |
5 ms |
11096 KB |
Output is correct |
33 |
Correct |
5 ms |
10844 KB |
Output is correct |
34 |
Correct |
5 ms |
10840 KB |
Output is correct |
35 |
Correct |
5 ms |
10844 KB |
Output is correct |
36 |
Correct |
5 ms |
10844 KB |
Output is correct |
37 |
Correct |
5 ms |
10844 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
90 ms |
15832 KB |
Output is correct |
2 |
Correct |
53 ms |
15824 KB |
Output is correct |
3 |
Correct |
99 ms |
17220 KB |
Output is correct |
4 |
Correct |
105 ms |
16708 KB |
Output is correct |
5 |
Correct |
86 ms |
17748 KB |
Output is correct |
6 |
Correct |
74 ms |
15984 KB |
Output is correct |
7 |
Correct |
75 ms |
15820 KB |
Output is correct |
8 |
Correct |
27 ms |
15752 KB |
Output is correct |
9 |
Correct |
28 ms |
15884 KB |
Output is correct |
10 |
Correct |
34 ms |
15688 KB |
Output is correct |
11 |
Correct |
95 ms |
17984 KB |
Output is correct |
12 |
Correct |
83 ms |
17744 KB |
Output is correct |
13 |
Correct |
96 ms |
17740 KB |
Output is correct |
14 |
Correct |
92 ms |
17740 KB |
Output is correct |
15 |
Correct |
99 ms |
15824 KB |
Output is correct |
16 |
Correct |
100 ms |
15828 KB |
Output is correct |
17 |
Correct |
97 ms |
15828 KB |
Output is correct |
18 |
Correct |
76 ms |
17756 KB |
Output is correct |
19 |
Correct |
1 ms |
10588 KB |
Output is correct |
20 |
Correct |
1 ms |
10588 KB |
Output is correct |
21 |
Correct |
1 ms |
10588 KB |
Output is correct |
22 |
Correct |
1 ms |
10588 KB |
Output is correct |
23 |
Correct |
1 ms |
10588 KB |
Output is correct |
24 |
Correct |
1 ms |
10588 KB |
Output is correct |
25 |
Correct |
1 ms |
10588 KB |
Output is correct |
26 |
Correct |
1 ms |
10588 KB |
Output is correct |
27 |
Correct |
1 ms |
10584 KB |
Output is correct |
28 |
Correct |
1 ms |
10588 KB |
Output is correct |
29 |
Correct |
1 ms |
10588 KB |
Output is correct |
30 |
Correct |
1 ms |
10588 KB |
Output is correct |
31 |
Correct |
1 ms |
10704 KB |
Output is correct |
32 |
Correct |
1 ms |
10588 KB |
Output is correct |
33 |
Correct |
1 ms |
10588 KB |
Output is correct |
34 |
Correct |
1 ms |
10588 KB |
Output is correct |
35 |
Correct |
1 ms |
10588 KB |
Output is correct |
36 |
Correct |
1 ms |
10588 KB |
Output is correct |
37 |
Correct |
1 ms |
10588 KB |
Output is correct |
38 |
Correct |
4 ms |
10840 KB |
Output is correct |
39 |
Correct |
4 ms |
10844 KB |
Output is correct |
40 |
Correct |
4 ms |
10844 KB |
Output is correct |
41 |
Correct |
4 ms |
10844 KB |
Output is correct |
42 |
Correct |
4 ms |
10844 KB |
Output is correct |
43 |
Correct |
5 ms |
10840 KB |
Output is correct |
44 |
Correct |
4 ms |
10844 KB |
Output is correct |
45 |
Correct |
4 ms |
10844 KB |
Output is correct |
46 |
Correct |
4 ms |
10844 KB |
Output is correct |
47 |
Correct |
4 ms |
10844 KB |
Output is correct |
48 |
Correct |
3 ms |
10844 KB |
Output is correct |
49 |
Correct |
3 ms |
10844 KB |
Output is correct |
50 |
Correct |
5 ms |
11096 KB |
Output is correct |
51 |
Correct |
5 ms |
10844 KB |
Output is correct |
52 |
Correct |
5 ms |
10840 KB |
Output is correct |
53 |
Correct |
5 ms |
10844 KB |
Output is correct |
54 |
Correct |
5 ms |
10844 KB |
Output is correct |
55 |
Correct |
5 ms |
10844 KB |
Output is correct |
56 |
Correct |
81 ms |
17284 KB |
Output is correct |
57 |
Correct |
79 ms |
17100 KB |
Output is correct |
58 |
Correct |
78 ms |
18128 KB |
Output is correct |
59 |
Correct |
78 ms |
18384 KB |
Output is correct |
60 |
Correct |
75 ms |
19148 KB |
Output is correct |
61 |
Correct |
74 ms |
18120 KB |
Output is correct |
62 |
Correct |
79 ms |
19688 KB |
Output is correct |
63 |
Correct |
73 ms |
18892 KB |
Output is correct |
64 |
Correct |
72 ms |
18876 KB |
Output is correct |
65 |
Correct |
105 ms |
19072 KB |
Output is correct |
66 |
Correct |
65 ms |
19160 KB |
Output is correct |
67 |
Correct |
101 ms |
19664 KB |
Output is correct |
68 |
Correct |
100 ms |
19664 KB |
Output is correct |
69 |
Correct |
91 ms |
19668 KB |
Output is correct |
70 |
Correct |
107 ms |
19664 KB |
Output is correct |
71 |
Correct |
97 ms |
19664 KB |
Output is correct |
72 |
Correct |
90 ms |
19660 KB |
Output is correct |
73 |
Correct |
104 ms |
19660 KB |
Output is correct |
74 |
Correct |
34 ms |
19720 KB |
Output is correct |
75 |
Correct |
41 ms |
19712 KB |
Output is correct |
76 |
Correct |
99 ms |
21328 KB |
Output is correct |
77 |
Correct |
98 ms |
21840 KB |
Output is correct |
78 |
Correct |
101 ms |
21324 KB |
Output is correct |
79 |
Correct |
103 ms |
19724 KB |
Output is correct |
80 |
Correct |
106 ms |
19152 KB |
Output is correct |
81 |
Correct |
36 ms |
19672 KB |
Output is correct |
82 |
Correct |
108 ms |
18708 KB |
Output is correct |
83 |
Correct |
107 ms |
18636 KB |
Output is correct |
84 |
Correct |
114 ms |
18812 KB |
Output is correct |
85 |
Correct |
75 ms |
19656 KB |
Output is correct |
86 |
Correct |
75 ms |
19660 KB |
Output is correct |
87 |
Correct |
84 ms |
19664 KB |
Output is correct |
88 |
Correct |
90 ms |
19664 KB |
Output is correct |
89 |
Correct |
81 ms |
19664 KB |
Output is correct |
90 |
Correct |
94 ms |
21748 KB |
Output is correct |
91 |
Correct |
86 ms |
20940 KB |
Output is correct |
92 |
Correct |
77 ms |
19540 KB |
Output is correct |
93 |
Correct |
102 ms |
19680 KB |
Output is correct |
94 |
Correct |
98 ms |
19660 KB |
Output is correct |
95 |
Correct |
97 ms |
19660 KB |
Output is correct |
96 |
Correct |
96 ms |
21724 KB |
Output is correct |
97 |
Correct |
98 ms |
21780 KB |
Output is correct |
98 |
Correct |
99 ms |
21840 KB |
Output is correct |
99 |
Correct |
93 ms |
21840 KB |
Output is correct |
100 |
Correct |
120 ms |
21584 KB |
Output is correct |
101 |
Correct |
97 ms |
21584 KB |
Output is correct |
102 |
Correct |
116 ms |
21824 KB |
Output is correct |
103 |
Correct |
69 ms |
19920 KB |
Output is correct |