#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){
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 |
96 ms |
15820 KB |
Output is correct |
2 |
Correct |
53 ms |
15824 KB |
Output is correct |
3 |
Correct |
107 ms |
17096 KB |
Output is correct |
4 |
Correct |
91 ms |
16688 KB |
Output is correct |
5 |
Correct |
85 ms |
17744 KB |
Output is correct |
6 |
Correct |
82 ms |
15824 KB |
Output is correct |
7 |
Correct |
74 ms |
15820 KB |
Output is correct |
8 |
Correct |
26 ms |
15824 KB |
Output is correct |
9 |
Correct |
25 ms |
15832 KB |
Output is correct |
10 |
Correct |
26 ms |
15832 KB |
Output is correct |
11 |
Correct |
77 ms |
17748 KB |
Output is correct |
12 |
Correct |
78 ms |
17744 KB |
Output is correct |
13 |
Correct |
88 ms |
17740 KB |
Output is correct |
14 |
Correct |
94 ms |
17744 KB |
Output is correct |
15 |
Correct |
92 ms |
15820 KB |
Output is correct |
16 |
Correct |
89 ms |
15832 KB |
Output is correct |
17 |
Correct |
90 ms |
15832 KB |
Output is correct |
18 |
Correct |
75 ms |
17744 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
10588 KB |
Output is correct |
2 |
Correct |
1 ms |
10588 KB |
Output is correct |
3 |
Correct |
1 ms |
10584 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 |
10588 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 |
10588 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 |
2 ms |
10588 KB |
Output is correct |
18 |
Correct |
1 ms |
10588 KB |
Output is correct |
19 |
Correct |
1 ms |
10588 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
10588 KB |
Output is correct |
2 |
Correct |
1 ms |
10588 KB |
Output is correct |
3 |
Correct |
1 ms |
10584 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 |
10588 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 |
10588 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 |
2 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 |
10844 KB |
Output is correct |
21 |
Correct |
4 ms |
10844 KB |
Output is correct |
22 |
Correct |
6 ms |
10844 KB |
Output is correct |
23 |
Correct |
4 ms |
10844 KB |
Output is correct |
24 |
Correct |
5 ms |
10920 KB |
Output is correct |
25 |
Correct |
5 ms |
10876 KB |
Output is correct |
26 |
Correct |
5 ms |
10872 KB |
Output is correct |
27 |
Correct |
5 ms |
10840 KB |
Output is correct |
28 |
Correct |
5 ms |
10928 KB |
Output is correct |
29 |
Correct |
4 ms |
10868 KB |
Output is correct |
30 |
Correct |
2 ms |
10844 KB |
Output is correct |
31 |
Correct |
2 ms |
10844 KB |
Output is correct |
32 |
Correct |
4 ms |
10844 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 |
10876 KB |
Output is correct |
37 |
Correct |
5 ms |
10844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
96 ms |
15820 KB |
Output is correct |
2 |
Correct |
53 ms |
15824 KB |
Output is correct |
3 |
Correct |
107 ms |
17096 KB |
Output is correct |
4 |
Correct |
91 ms |
16688 KB |
Output is correct |
5 |
Correct |
85 ms |
17744 KB |
Output is correct |
6 |
Correct |
82 ms |
15824 KB |
Output is correct |
7 |
Correct |
74 ms |
15820 KB |
Output is correct |
8 |
Correct |
26 ms |
15824 KB |
Output is correct |
9 |
Correct |
25 ms |
15832 KB |
Output is correct |
10 |
Correct |
26 ms |
15832 KB |
Output is correct |
11 |
Correct |
77 ms |
17748 KB |
Output is correct |
12 |
Correct |
78 ms |
17744 KB |
Output is correct |
13 |
Correct |
88 ms |
17740 KB |
Output is correct |
14 |
Correct |
94 ms |
17744 KB |
Output is correct |
15 |
Correct |
92 ms |
15820 KB |
Output is correct |
16 |
Correct |
89 ms |
15832 KB |
Output is correct |
17 |
Correct |
90 ms |
15832 KB |
Output is correct |
18 |
Correct |
75 ms |
17744 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 |
10584 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 |
10588 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 |
10588 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 |
2 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 |
10844 KB |
Output is correct |
39 |
Correct |
4 ms |
10844 KB |
Output is correct |
40 |
Correct |
6 ms |
10844 KB |
Output is correct |
41 |
Correct |
4 ms |
10844 KB |
Output is correct |
42 |
Correct |
5 ms |
10920 KB |
Output is correct |
43 |
Correct |
5 ms |
10876 KB |
Output is correct |
44 |
Correct |
5 ms |
10872 KB |
Output is correct |
45 |
Correct |
5 ms |
10840 KB |
Output is correct |
46 |
Correct |
5 ms |
10928 KB |
Output is correct |
47 |
Correct |
4 ms |
10868 KB |
Output is correct |
48 |
Correct |
2 ms |
10844 KB |
Output is correct |
49 |
Correct |
2 ms |
10844 KB |
Output is correct |
50 |
Correct |
4 ms |
10844 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 |
10876 KB |
Output is correct |
55 |
Correct |
5 ms |
10844 KB |
Output is correct |
56 |
Correct |
71 ms |
17100 KB |
Output is correct |
57 |
Runtime error |
87 ms |
33360 KB |
Execution killed with signal 11 |
58 |
Halted |
0 ms |
0 KB |
- |