#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){
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);
}x=tr[1].qr(id[u.s]-1,m,m);
while(x.f<=r[u.s]+t[u.s]+1){
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);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
95 ms |
10708 KB |
Output is correct |
2 |
Correct |
59 ms |
10700 KB |
Output is correct |
3 |
Correct |
90 ms |
11204 KB |
Output is correct |
4 |
Correct |
85 ms |
10448 KB |
Output is correct |
5 |
Correct |
81 ms |
12536 KB |
Output is correct |
6 |
Correct |
89 ms |
10700 KB |
Output is correct |
7 |
Correct |
82 ms |
10708 KB |
Output is correct |
8 |
Correct |
28 ms |
10568 KB |
Output is correct |
9 |
Correct |
28 ms |
10512 KB |
Output is correct |
10 |
Correct |
27 ms |
10608 KB |
Output is correct |
11 |
Correct |
101 ms |
12628 KB |
Output is correct |
12 |
Correct |
103 ms |
12780 KB |
Output is correct |
13 |
Correct |
89 ms |
12740 KB |
Output is correct |
14 |
Correct |
92 ms |
12724 KB |
Output is correct |
15 |
Correct |
93 ms |
10700 KB |
Output is correct |
16 |
Correct |
96 ms |
10704 KB |
Output is correct |
17 |
Correct |
97 ms |
9940 KB |
Output is correct |
18 |
Correct |
77 ms |
11852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
0 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4440 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4560 KB |
Output is correct |
10 |
Correct |
1 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 |
0 ms |
4444 KB |
Output is correct |
16 |
Correct |
0 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 |
4440 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
4440 KB |
Output is correct |
2 |
Correct |
1 ms |
4444 KB |
Output is correct |
3 |
Correct |
0 ms |
4444 KB |
Output is correct |
4 |
Correct |
1 ms |
4444 KB |
Output is correct |
5 |
Correct |
1 ms |
4444 KB |
Output is correct |
6 |
Correct |
1 ms |
4440 KB |
Output is correct |
7 |
Correct |
1 ms |
4444 KB |
Output is correct |
8 |
Correct |
1 ms |
4444 KB |
Output is correct |
9 |
Correct |
1 ms |
4560 KB |
Output is correct |
10 |
Correct |
1 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 |
0 ms |
4444 KB |
Output is correct |
16 |
Correct |
0 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 |
4440 KB |
Output is correct |
20 |
Correct |
4 ms |
4700 KB |
Output is correct |
21 |
Correct |
4 ms |
4992 KB |
Output is correct |
22 |
Correct |
5 ms |
4952 KB |
Output is correct |
23 |
Correct |
4 ms |
4952 KB |
Output is correct |
24 |
Correct |
5 ms |
4956 KB |
Output is correct |
25 |
Correct |
5 ms |
4964 KB |
Output is correct |
26 |
Correct |
6 ms |
4956 KB |
Output is correct |
27 |
Correct |
4 ms |
4956 KB |
Output is correct |
28 |
Correct |
4 ms |
4956 KB |
Output is correct |
29 |
Correct |
6 ms |
5208 KB |
Output is correct |
30 |
Correct |
2 ms |
4956 KB |
Output is correct |
31 |
Correct |
2 ms |
4952 KB |
Output is correct |
32 |
Correct |
4 ms |
4996 KB |
Output is correct |
33 |
Correct |
4 ms |
4956 KB |
Output is correct |
34 |
Correct |
5 ms |
4956 KB |
Output is correct |
35 |
Correct |
5 ms |
5208 KB |
Output is correct |
36 |
Correct |
5 ms |
4956 KB |
Output is correct |
37 |
Correct |
5 ms |
4956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
95 ms |
10708 KB |
Output is correct |
2 |
Correct |
59 ms |
10700 KB |
Output is correct |
3 |
Correct |
90 ms |
11204 KB |
Output is correct |
4 |
Correct |
85 ms |
10448 KB |
Output is correct |
5 |
Correct |
81 ms |
12536 KB |
Output is correct |
6 |
Correct |
89 ms |
10700 KB |
Output is correct |
7 |
Correct |
82 ms |
10708 KB |
Output is correct |
8 |
Correct |
28 ms |
10568 KB |
Output is correct |
9 |
Correct |
28 ms |
10512 KB |
Output is correct |
10 |
Correct |
27 ms |
10608 KB |
Output is correct |
11 |
Correct |
101 ms |
12628 KB |
Output is correct |
12 |
Correct |
103 ms |
12780 KB |
Output is correct |
13 |
Correct |
89 ms |
12740 KB |
Output is correct |
14 |
Correct |
92 ms |
12724 KB |
Output is correct |
15 |
Correct |
93 ms |
10700 KB |
Output is correct |
16 |
Correct |
96 ms |
10704 KB |
Output is correct |
17 |
Correct |
97 ms |
9940 KB |
Output is correct |
18 |
Correct |
77 ms |
11852 KB |
Output is correct |
19 |
Correct |
1 ms |
4440 KB |
Output is correct |
20 |
Correct |
1 ms |
4444 KB |
Output is correct |
21 |
Correct |
0 ms |
4444 KB |
Output is correct |
22 |
Correct |
1 ms |
4444 KB |
Output is correct |
23 |
Correct |
1 ms |
4444 KB |
Output is correct |
24 |
Correct |
1 ms |
4440 KB |
Output is correct |
25 |
Correct |
1 ms |
4444 KB |
Output is correct |
26 |
Correct |
1 ms |
4444 KB |
Output is correct |
27 |
Correct |
1 ms |
4560 KB |
Output is correct |
28 |
Correct |
1 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 |
0 ms |
4444 KB |
Output is correct |
34 |
Correct |
0 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 |
4440 KB |
Output is correct |
38 |
Correct |
4 ms |
4700 KB |
Output is correct |
39 |
Correct |
4 ms |
4992 KB |
Output is correct |
40 |
Correct |
5 ms |
4952 KB |
Output is correct |
41 |
Correct |
4 ms |
4952 KB |
Output is correct |
42 |
Correct |
5 ms |
4956 KB |
Output is correct |
43 |
Correct |
5 ms |
4964 KB |
Output is correct |
44 |
Correct |
6 ms |
4956 KB |
Output is correct |
45 |
Correct |
4 ms |
4956 KB |
Output is correct |
46 |
Correct |
4 ms |
4956 KB |
Output is correct |
47 |
Correct |
6 ms |
5208 KB |
Output is correct |
48 |
Correct |
2 ms |
4956 KB |
Output is correct |
49 |
Correct |
2 ms |
4952 KB |
Output is correct |
50 |
Correct |
4 ms |
4996 KB |
Output is correct |
51 |
Correct |
4 ms |
4956 KB |
Output is correct |
52 |
Correct |
5 ms |
4956 KB |
Output is correct |
53 |
Correct |
5 ms |
5208 KB |
Output is correct |
54 |
Correct |
5 ms |
4956 KB |
Output is correct |
55 |
Correct |
5 ms |
4956 KB |
Output is correct |
56 |
Correct |
90 ms |
11528 KB |
Output is correct |
57 |
Runtime error |
105 ms |
19372 KB |
Execution killed with signal 11 |
58 |
Halted |
0 ms |
0 KB |
- |