#include<bits/stdc++.h>
using namespace std;
#define all(fl) fl.begin(),fl.end()
#define pb push_back
#define fi first
#define se second
#define for1(i,j,k) for(int i=j;i<=k;i++)
#define for2(i,j,k) for(int i=j;i>=k;i--)
#define for3(i,j,k,l) for(int i=j;i<=k;i+=l)
#define lb lower_bound
#define ub upper_bound
#define sz(a) (int)a.size()
#define pii pair<int,int>
#define pli pair<long long,int>
#define gcd __gcd
#define lcm(x,y) x*y/__gcd(x,y)
#define pil pair<int,long long>
const int maxn=1e5+9;
struct edg{
int time;
int l,r;
long long c;
bool operator < (const edg &p)const {
return time<p.time;
}
}a[maxn];
vector<int>g[maxn];
/*bool check(int i, int j) {
if (a[i].r == n)return 0;
if (a[i].time <= a[j].time) {
if (a[i].r+1+a[i].time>=a[j].l+a[j].time)return 1;
return 0;
} else {
//will infected from a[j].l-1
if (a[i].r+1-a[i].time>=a[j].l-a[j].time)return 1;
return 0;
}
}*/
int n,m;
long long d[maxn];
bool vis[maxn];
const long long inf=1e18;
struct line{
int u;
long long val;
bool operator < (const line &p)const {
return val>p.val;
}
};
vector<int>stx[maxn*4],sty[maxn*4];//x for case 1 y for case 2
void build(int id,int l,int r){
for1(i,l,r)stx[id].pb(i);
for1(i,l,r)sty[id].pb(i);
sort(all(stx[id]),[](int i,int j){
return a[i].l+a[i].time>a[j].l+a[j].time;
});
sort(all(sty[id]),[](int i,int j){
return a[i].l-a[i].time>a[j].l-a[j].time;
});
if (l==r)return;
int mid=(l+r)/2;
build(id*2,l,mid);
build(id*2+1,mid+1,r);
}
priority_queue<line>t;
void get(int id,int l,int r,int u,int v,int root,bool fl){
if (l>v||r<u||u>v)return;
if (u<=l&&r<=v){
while (!stx[id].empty()){
if (fl)break;
int tmp=stx[id].back();
if (vis[tmp]){
stx[id].pop_back();
continue;
}
if (a[tmp].l+a[tmp].time<=a[root].r+1+a[root].time){
vis[tmp]=1;
d[tmp]=d[root]+a[tmp].c;
t.push({tmp,d[tmp]});
stx[id].pop_back();
}
else break;
}
while (!sty[id].empty()){
if (!fl)break;
int tmp=sty[id].back();
if (vis[tmp]){
sty[id].pop_back();
continue;
}
if (a[tmp].l-a[tmp].time<=a[root].r+1-a[root].time){
vis[tmp]=1;
d[tmp]=d[root]+a[tmp].c;
t.push({tmp,d[tmp]});
sty[id].pop_back();
}
else break;
}
return;
}
int mid=(l+r)/2;
get(id*2,l,mid,u,v,root,fl);
get(id*2+1,mid+1,r,u,v,root,fl);
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
//freopen("temp.INP","r",stdin);
//freopen("temp.OUT","w",stdout);
cin>>n>>m;
for1(i,1,m)cin>>a[i].time>>a[i].l>>a[i].r>>a[i].c;
sort(a+1,a+1+m);
build(1,1,m);
for1(i,1,m)d[i]=inf;
for1(i,1,m){
if (a[i].l==1){
t.push({i,a[i].c});
d[i]=a[i].c;
vis[i]=1;
}
}
while (!t.empty()){
auto u=t.top();
t.pop();
get(1,1,m,1,u.u-1,u.u,1);
get(1,1,m,u.u+1,m,u.u,0);
}
long long ans=inf;
for1(i,1,m)if (a[i].r==n)ans=min(ans,d[i]);
if (ans>=inf)ans=-1;
cout<<ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
403 ms |
54296 KB |
Output is correct |
2 |
Correct |
314 ms |
53936 KB |
Output is correct |
3 |
Correct |
317 ms |
54020 KB |
Output is correct |
4 |
Correct |
325 ms |
54044 KB |
Output is correct |
5 |
Correct |
271 ms |
56096 KB |
Output is correct |
6 |
Correct |
261 ms |
54096 KB |
Output is correct |
7 |
Correct |
227 ms |
53764 KB |
Output is correct |
8 |
Correct |
182 ms |
53744 KB |
Output is correct |
9 |
Correct |
175 ms |
53948 KB |
Output is correct |
10 |
Correct |
165 ms |
53844 KB |
Output is correct |
11 |
Correct |
349 ms |
56192 KB |
Output is correct |
12 |
Correct |
338 ms |
56340 KB |
Output is correct |
13 |
Correct |
352 ms |
56112 KB |
Output is correct |
14 |
Correct |
350 ms |
56212 KB |
Output is correct |
15 |
Correct |
350 ms |
54000 KB |
Output is correct |
16 |
Correct |
369 ms |
54008 KB |
Output is correct |
17 |
Correct |
349 ms |
53080 KB |
Output is correct |
18 |
Correct |
329 ms |
55476 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
23132 KB |
Output is correct |
2 |
Correct |
5 ms |
23132 KB |
Output is correct |
3 |
Correct |
5 ms |
23204 KB |
Output is correct |
4 |
Correct |
4 ms |
23128 KB |
Output is correct |
5 |
Correct |
5 ms |
23132 KB |
Output is correct |
6 |
Correct |
5 ms |
23132 KB |
Output is correct |
7 |
Correct |
6 ms |
23132 KB |
Output is correct |
8 |
Correct |
6 ms |
23132 KB |
Output is correct |
9 |
Correct |
4 ms |
23132 KB |
Output is correct |
10 |
Correct |
5 ms |
23132 KB |
Output is correct |
11 |
Correct |
5 ms |
23128 KB |
Output is correct |
12 |
Correct |
5 ms |
22980 KB |
Output is correct |
13 |
Correct |
5 ms |
22956 KB |
Output is correct |
14 |
Correct |
5 ms |
23132 KB |
Output is correct |
15 |
Correct |
6 ms |
23132 KB |
Output is correct |
16 |
Correct |
5 ms |
23132 KB |
Output is correct |
17 |
Correct |
6 ms |
23132 KB |
Output is correct |
18 |
Correct |
5 ms |
23164 KB |
Output is correct |
19 |
Correct |
6 ms |
23132 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
23132 KB |
Output is correct |
2 |
Correct |
5 ms |
23132 KB |
Output is correct |
3 |
Correct |
5 ms |
23204 KB |
Output is correct |
4 |
Correct |
4 ms |
23128 KB |
Output is correct |
5 |
Correct |
5 ms |
23132 KB |
Output is correct |
6 |
Correct |
5 ms |
23132 KB |
Output is correct |
7 |
Correct |
6 ms |
23132 KB |
Output is correct |
8 |
Correct |
6 ms |
23132 KB |
Output is correct |
9 |
Correct |
4 ms |
23132 KB |
Output is correct |
10 |
Correct |
5 ms |
23132 KB |
Output is correct |
11 |
Correct |
5 ms |
23128 KB |
Output is correct |
12 |
Correct |
5 ms |
22980 KB |
Output is correct |
13 |
Correct |
5 ms |
22956 KB |
Output is correct |
14 |
Correct |
5 ms |
23132 KB |
Output is correct |
15 |
Correct |
6 ms |
23132 KB |
Output is correct |
16 |
Correct |
5 ms |
23132 KB |
Output is correct |
17 |
Correct |
6 ms |
23132 KB |
Output is correct |
18 |
Correct |
5 ms |
23164 KB |
Output is correct |
19 |
Correct |
6 ms |
23132 KB |
Output is correct |
20 |
Correct |
13 ms |
24408 KB |
Output is correct |
21 |
Correct |
14 ms |
24668 KB |
Output is correct |
22 |
Correct |
15 ms |
24536 KB |
Output is correct |
23 |
Correct |
14 ms |
24488 KB |
Output is correct |
24 |
Correct |
15 ms |
24668 KB |
Output is correct |
25 |
Correct |
16 ms |
24492 KB |
Output is correct |
26 |
Correct |
15 ms |
24408 KB |
Output is correct |
27 |
Correct |
15 ms |
24412 KB |
Output is correct |
28 |
Correct |
16 ms |
24664 KB |
Output is correct |
29 |
Correct |
15 ms |
24412 KB |
Output is correct |
30 |
Correct |
13 ms |
24412 KB |
Output is correct |
31 |
Correct |
13 ms |
24412 KB |
Output is correct |
32 |
Correct |
15 ms |
24848 KB |
Output is correct |
33 |
Correct |
15 ms |
24768 KB |
Output is correct |
34 |
Correct |
16 ms |
24400 KB |
Output is correct |
35 |
Correct |
16 ms |
24668 KB |
Output is correct |
36 |
Correct |
16 ms |
24668 KB |
Output is correct |
37 |
Correct |
17 ms |
24412 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
403 ms |
54296 KB |
Output is correct |
2 |
Correct |
314 ms |
53936 KB |
Output is correct |
3 |
Correct |
317 ms |
54020 KB |
Output is correct |
4 |
Correct |
325 ms |
54044 KB |
Output is correct |
5 |
Correct |
271 ms |
56096 KB |
Output is correct |
6 |
Correct |
261 ms |
54096 KB |
Output is correct |
7 |
Correct |
227 ms |
53764 KB |
Output is correct |
8 |
Correct |
182 ms |
53744 KB |
Output is correct |
9 |
Correct |
175 ms |
53948 KB |
Output is correct |
10 |
Correct |
165 ms |
53844 KB |
Output is correct |
11 |
Correct |
349 ms |
56192 KB |
Output is correct |
12 |
Correct |
338 ms |
56340 KB |
Output is correct |
13 |
Correct |
352 ms |
56112 KB |
Output is correct |
14 |
Correct |
350 ms |
56212 KB |
Output is correct |
15 |
Correct |
350 ms |
54000 KB |
Output is correct |
16 |
Correct |
369 ms |
54008 KB |
Output is correct |
17 |
Correct |
349 ms |
53080 KB |
Output is correct |
18 |
Correct |
329 ms |
55476 KB |
Output is correct |
19 |
Correct |
5 ms |
23132 KB |
Output is correct |
20 |
Correct |
5 ms |
23132 KB |
Output is correct |
21 |
Correct |
5 ms |
23204 KB |
Output is correct |
22 |
Correct |
4 ms |
23128 KB |
Output is correct |
23 |
Correct |
5 ms |
23132 KB |
Output is correct |
24 |
Correct |
5 ms |
23132 KB |
Output is correct |
25 |
Correct |
6 ms |
23132 KB |
Output is correct |
26 |
Correct |
6 ms |
23132 KB |
Output is correct |
27 |
Correct |
4 ms |
23132 KB |
Output is correct |
28 |
Correct |
5 ms |
23132 KB |
Output is correct |
29 |
Correct |
5 ms |
23128 KB |
Output is correct |
30 |
Correct |
5 ms |
22980 KB |
Output is correct |
31 |
Correct |
5 ms |
22956 KB |
Output is correct |
32 |
Correct |
5 ms |
23132 KB |
Output is correct |
33 |
Correct |
6 ms |
23132 KB |
Output is correct |
34 |
Correct |
5 ms |
23132 KB |
Output is correct |
35 |
Correct |
6 ms |
23132 KB |
Output is correct |
36 |
Correct |
5 ms |
23164 KB |
Output is correct |
37 |
Correct |
6 ms |
23132 KB |
Output is correct |
38 |
Correct |
13 ms |
24408 KB |
Output is correct |
39 |
Correct |
14 ms |
24668 KB |
Output is correct |
40 |
Correct |
15 ms |
24536 KB |
Output is correct |
41 |
Correct |
14 ms |
24488 KB |
Output is correct |
42 |
Correct |
15 ms |
24668 KB |
Output is correct |
43 |
Correct |
16 ms |
24492 KB |
Output is correct |
44 |
Correct |
15 ms |
24408 KB |
Output is correct |
45 |
Correct |
15 ms |
24412 KB |
Output is correct |
46 |
Correct |
16 ms |
24664 KB |
Output is correct |
47 |
Correct |
15 ms |
24412 KB |
Output is correct |
48 |
Correct |
13 ms |
24412 KB |
Output is correct |
49 |
Correct |
13 ms |
24412 KB |
Output is correct |
50 |
Correct |
15 ms |
24848 KB |
Output is correct |
51 |
Correct |
15 ms |
24768 KB |
Output is correct |
52 |
Correct |
16 ms |
24400 KB |
Output is correct |
53 |
Correct |
16 ms |
24668 KB |
Output is correct |
54 |
Correct |
16 ms |
24668 KB |
Output is correct |
55 |
Correct |
17 ms |
24412 KB |
Output is correct |
56 |
Correct |
251 ms |
54304 KB |
Output is correct |
57 |
Correct |
263 ms |
54740 KB |
Output is correct |
58 |
Correct |
259 ms |
53064 KB |
Output is correct |
59 |
Correct |
251 ms |
53588 KB |
Output is correct |
60 |
Correct |
288 ms |
54364 KB |
Output is correct |
61 |
Correct |
257 ms |
53392 KB |
Output is correct |
62 |
Correct |
248 ms |
54376 KB |
Output is correct |
63 |
Correct |
174 ms |
54100 KB |
Output is correct |
64 |
Correct |
170 ms |
53840 KB |
Output is correct |
65 |
Correct |
393 ms |
54104 KB |
Output is correct |
66 |
Correct |
254 ms |
54100 KB |
Output is correct |
67 |
Correct |
343 ms |
54724 KB |
Output is correct |
68 |
Correct |
349 ms |
54716 KB |
Output is correct |
69 |
Correct |
319 ms |
54692 KB |
Output is correct |
70 |
Correct |
364 ms |
54940 KB |
Output is correct |
71 |
Correct |
345 ms |
54728 KB |
Output is correct |
72 |
Correct |
324 ms |
54868 KB |
Output is correct |
73 |
Correct |
380 ms |
54864 KB |
Output is correct |
74 |
Correct |
249 ms |
54544 KB |
Output is correct |
75 |
Correct |
240 ms |
54476 KB |
Output is correct |
76 |
Correct |
364 ms |
56524 KB |
Output is correct |
77 |
Correct |
336 ms |
56792 KB |
Output is correct |
78 |
Correct |
361 ms |
56280 KB |
Output is correct |
79 |
Correct |
440 ms |
54888 KB |
Output is correct |
80 |
Correct |
369 ms |
54392 KB |
Output is correct |
81 |
Correct |
259 ms |
54716 KB |
Output is correct |
82 |
Correct |
411 ms |
53888 KB |
Output is correct |
83 |
Correct |
376 ms |
53856 KB |
Output is correct |
84 |
Correct |
357 ms |
53972 KB |
Output is correct |
85 |
Correct |
314 ms |
54500 KB |
Output is correct |
86 |
Correct |
306 ms |
54888 KB |
Output is correct |
87 |
Correct |
338 ms |
54608 KB |
Output is correct |
88 |
Correct |
315 ms |
54604 KB |
Output is correct |
89 |
Correct |
332 ms |
54648 KB |
Output is correct |
90 |
Correct |
356 ms |
56844 KB |
Output is correct |
91 |
Correct |
330 ms |
55768 KB |
Output is correct |
92 |
Correct |
342 ms |
54776 KB |
Output is correct |
93 |
Correct |
355 ms |
54604 KB |
Output is correct |
94 |
Correct |
355 ms |
55068 KB |
Output is correct |
95 |
Correct |
340 ms |
54616 KB |
Output is correct |
96 |
Correct |
362 ms |
56764 KB |
Output is correct |
97 |
Correct |
330 ms |
56792 KB |
Output is correct |
98 |
Correct |
372 ms |
56872 KB |
Output is correct |
99 |
Correct |
339 ms |
56796 KB |
Output is correct |
100 |
Correct |
349 ms |
56824 KB |
Output is correct |
101 |
Correct |
333 ms |
56788 KB |
Output is correct |
102 |
Correct |
387 ms |
56932 KB |
Output is correct |
103 |
Correct |
330 ms |
55192 KB |
Output is correct |