#include<bits/stdc++.h>
#pragma GCC optimize("trapv")
#define st first
#define nd second
using namespace std;
using ll = long long;
const int N=1<<17;
const ll INF=1e18;
ll dist[N];
bool vis[N];
vector<pair<int, int> > tab[N+N];
vector<int> co;
vector<pair<pair<int, int>, pair<int, int>> > V;
set<pair<ll, int> > S;
void insert(int v, int val, int id){
for(v+=N; v>1; v>>=1){
//cout<<val<<" "<<id<<" "<<v<<"\n";
tab[v].push_back({val, id});
}
}
void find(ll d, int v, int val){
for(v+=N; v>1; v>>=1){
//cout<<"b\n";
if(v&1){
v--;
//cout<<v<<"\n";
while(tab[v].size() && tab[v].back().st<=val){
int u=tab[v].back().nd;
//cout<<u<<"a\n";
//cout<<dist[u]<<" "<<d<<" "<<V[u].nd.nd<<"\n";
if(dist[u]>d+V[u].nd.nd){
//cout<<"c\n";
//cout<<u<<"\n";
//cout<<u<<" "<<d<<" "<<v<<"\n";
//cout<<u<<" "<<V[u].st.st+V[u].nd.st<<" "<<V[u].st.st-V[u].nd.st<<"b\n";
S.erase(S.find({dist[u], u}));
dist[u]=d+V[u].nd.nd;
S.insert({dist[u], u});
}
//cout<<u<<"a\n";
tab[v].pop_back();
}
}
}
}
int main(){
int l, n;
cin>>l>>n;
V.resize(n);
for(int i=0; i<n; ++i){
cin>>V[i].nd.st>>V[i].st.st>>V[i].st.nd>>V[i].nd.nd;
V[i].st.st--;
co.push_back(V[i].st.st+V[i].nd.st);
}
sort(co.begin(), co.end());
co.resize(unique(co.begin(), co.end())-co.begin());
for(int i=0; i<n; i++){
//cout<<i<<" "<<V[i].st.st+V[i].nd.st<<" "<<lower_bound(co.begin(), co.end(), V[i].st.st+V[i].nd.st)-co.begin()<<"\n";
insert(lower_bound(co.begin(), co.end(), V[i].st.st+V[i].nd.st)
-co.begin(), V[i].st.st-V[i].nd.st, i);
}
for(int i=0; i<N+N; i++){
if(tab[i].size()){
sort(tab[i].begin(), tab[i].end());
reverse(tab[i].begin(), tab[i].end());
}
}
for(int i=0; i<n; i++){
if(V[i].st.st)dist[i]=INF;
else dist[i]=V[i].nd.nd;
S.insert({dist[i], i});
}
for(int ii=0; ii<n; ii++){
int opt=S.begin()->nd;
//cout<<opt<<" "<<dist[opt]<<"\n";
//cout<<opt<<" "<<S.begin()->st<<"\n";
S.erase(S.begin());
if(V[opt].st.nd==l && dist[opt]!=INF){
cout<<dist[opt];
return 0;
}
//cout<<opt<<"\n";
//cout<<V[opt].st.nd+V[opt].nd.st <<" "<<V[opt].st.nd-V[opt].nd.st <<"a\n";
//cout<<opt<<" "<<upper_bound(co.begin(), co.end(), V[opt].st.nd+V[opt].nd.st)-co.begin()<<"\n";
find(dist[opt], upper_bound(co.begin(), co.end(), V[opt].st.nd+V[opt].nd.st)
-co.begin(), V[opt].st.nd-V[opt].nd.st);
/*for(int i=0; i<n; i++){
if(V[i].st.st+V[i].nd.st<=V[opt].st.nd+V[opt].nd.st
&& V[i].st.st-V[i].nd.st<=V[opt].st.nd-V[opt].nd.st){
if(dist[i]>V[i].nd.nd+dist[opt]){
dist[i]=V[i].nd.nd+dist[opt];
}
}
}*/
}
cout<<-1;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
442 ms |
33640 KB |
Output is correct |
2 |
Correct |
404 ms |
35896 KB |
Output is correct |
3 |
Correct |
352 ms |
33408 KB |
Output is correct |
4 |
Correct |
353 ms |
33636 KB |
Output is correct |
5 |
Correct |
355 ms |
32496 KB |
Output is correct |
6 |
Correct |
335 ms |
33748 KB |
Output is correct |
7 |
Correct |
309 ms |
36644 KB |
Output is correct |
8 |
Correct |
309 ms |
32444 KB |
Output is correct |
9 |
Correct |
297 ms |
33712 KB |
Output is correct |
10 |
Correct |
285 ms |
36560 KB |
Output is correct |
11 |
Correct |
397 ms |
36544 KB |
Output is correct |
12 |
Correct |
414 ms |
36384 KB |
Output is correct |
13 |
Correct |
462 ms |
36476 KB |
Output is correct |
14 |
Correct |
449 ms |
36100 KB |
Output is correct |
15 |
Correct |
450 ms |
36616 KB |
Output is correct |
16 |
Correct |
428 ms |
36516 KB |
Output is correct |
17 |
Correct |
419 ms |
35696 KB |
Output is correct |
18 |
Correct |
373 ms |
35900 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
6356 KB |
Output is correct |
2 |
Correct |
4 ms |
6356 KB |
Output is correct |
3 |
Correct |
4 ms |
6356 KB |
Output is correct |
4 |
Correct |
4 ms |
6464 KB |
Output is correct |
5 |
Correct |
4 ms |
6356 KB |
Output is correct |
6 |
Correct |
4 ms |
6356 KB |
Output is correct |
7 |
Correct |
5 ms |
6356 KB |
Output is correct |
8 |
Correct |
4 ms |
6356 KB |
Output is correct |
9 |
Correct |
4 ms |
6356 KB |
Output is correct |
10 |
Correct |
4 ms |
6356 KB |
Output is correct |
11 |
Correct |
4 ms |
6464 KB |
Output is correct |
12 |
Correct |
4 ms |
6356 KB |
Output is correct |
13 |
Correct |
4 ms |
6356 KB |
Output is correct |
14 |
Correct |
4 ms |
6356 KB |
Output is correct |
15 |
Correct |
4 ms |
6356 KB |
Output is correct |
16 |
Correct |
4 ms |
6464 KB |
Output is correct |
17 |
Correct |
4 ms |
6356 KB |
Output is correct |
18 |
Correct |
5 ms |
6484 KB |
Output is correct |
19 |
Correct |
4 ms |
6464 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
4 ms |
6356 KB |
Output is correct |
2 |
Correct |
4 ms |
6356 KB |
Output is correct |
3 |
Correct |
4 ms |
6356 KB |
Output is correct |
4 |
Correct |
4 ms |
6464 KB |
Output is correct |
5 |
Correct |
4 ms |
6356 KB |
Output is correct |
6 |
Correct |
4 ms |
6356 KB |
Output is correct |
7 |
Correct |
5 ms |
6356 KB |
Output is correct |
8 |
Correct |
4 ms |
6356 KB |
Output is correct |
9 |
Correct |
4 ms |
6356 KB |
Output is correct |
10 |
Correct |
4 ms |
6356 KB |
Output is correct |
11 |
Correct |
4 ms |
6464 KB |
Output is correct |
12 |
Correct |
4 ms |
6356 KB |
Output is correct |
13 |
Correct |
4 ms |
6356 KB |
Output is correct |
14 |
Correct |
4 ms |
6356 KB |
Output is correct |
15 |
Correct |
4 ms |
6356 KB |
Output is correct |
16 |
Correct |
4 ms |
6464 KB |
Output is correct |
17 |
Correct |
4 ms |
6356 KB |
Output is correct |
18 |
Correct |
5 ms |
6484 KB |
Output is correct |
19 |
Correct |
4 ms |
6464 KB |
Output is correct |
20 |
Correct |
18 ms |
7904 KB |
Output is correct |
21 |
Correct |
18 ms |
7892 KB |
Output is correct |
22 |
Correct |
22 ms |
7960 KB |
Output is correct |
23 |
Correct |
21 ms |
7892 KB |
Output is correct |
24 |
Correct |
21 ms |
7932 KB |
Output is correct |
25 |
Correct |
22 ms |
7892 KB |
Output is correct |
26 |
Correct |
18 ms |
7884 KB |
Output is correct |
27 |
Correct |
19 ms |
7892 KB |
Output is correct |
28 |
Correct |
21 ms |
7936 KB |
Output is correct |
29 |
Correct |
23 ms |
7920 KB |
Output is correct |
30 |
Correct |
19 ms |
7892 KB |
Output is correct |
31 |
Correct |
18 ms |
7984 KB |
Output is correct |
32 |
Correct |
20 ms |
7944 KB |
Output is correct |
33 |
Correct |
19 ms |
7872 KB |
Output is correct |
34 |
Correct |
21 ms |
8020 KB |
Output is correct |
35 |
Correct |
18 ms |
7880 KB |
Output is correct |
36 |
Correct |
18 ms |
7872 KB |
Output is correct |
37 |
Correct |
22 ms |
7892 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
442 ms |
33640 KB |
Output is correct |
2 |
Correct |
404 ms |
35896 KB |
Output is correct |
3 |
Correct |
352 ms |
33408 KB |
Output is correct |
4 |
Correct |
353 ms |
33636 KB |
Output is correct |
5 |
Correct |
355 ms |
32496 KB |
Output is correct |
6 |
Correct |
335 ms |
33748 KB |
Output is correct |
7 |
Correct |
309 ms |
36644 KB |
Output is correct |
8 |
Correct |
309 ms |
32444 KB |
Output is correct |
9 |
Correct |
297 ms |
33712 KB |
Output is correct |
10 |
Correct |
285 ms |
36560 KB |
Output is correct |
11 |
Correct |
397 ms |
36544 KB |
Output is correct |
12 |
Correct |
414 ms |
36384 KB |
Output is correct |
13 |
Correct |
462 ms |
36476 KB |
Output is correct |
14 |
Correct |
449 ms |
36100 KB |
Output is correct |
15 |
Correct |
450 ms |
36616 KB |
Output is correct |
16 |
Correct |
428 ms |
36516 KB |
Output is correct |
17 |
Correct |
419 ms |
35696 KB |
Output is correct |
18 |
Correct |
373 ms |
35900 KB |
Output is correct |
19 |
Correct |
4 ms |
6356 KB |
Output is correct |
20 |
Correct |
4 ms |
6356 KB |
Output is correct |
21 |
Correct |
4 ms |
6356 KB |
Output is correct |
22 |
Correct |
4 ms |
6464 KB |
Output is correct |
23 |
Correct |
4 ms |
6356 KB |
Output is correct |
24 |
Correct |
4 ms |
6356 KB |
Output is correct |
25 |
Correct |
5 ms |
6356 KB |
Output is correct |
26 |
Correct |
4 ms |
6356 KB |
Output is correct |
27 |
Correct |
4 ms |
6356 KB |
Output is correct |
28 |
Correct |
4 ms |
6356 KB |
Output is correct |
29 |
Correct |
4 ms |
6464 KB |
Output is correct |
30 |
Correct |
4 ms |
6356 KB |
Output is correct |
31 |
Correct |
4 ms |
6356 KB |
Output is correct |
32 |
Correct |
4 ms |
6356 KB |
Output is correct |
33 |
Correct |
4 ms |
6356 KB |
Output is correct |
34 |
Correct |
4 ms |
6464 KB |
Output is correct |
35 |
Correct |
4 ms |
6356 KB |
Output is correct |
36 |
Correct |
5 ms |
6484 KB |
Output is correct |
37 |
Correct |
4 ms |
6464 KB |
Output is correct |
38 |
Correct |
18 ms |
7904 KB |
Output is correct |
39 |
Correct |
18 ms |
7892 KB |
Output is correct |
40 |
Correct |
22 ms |
7960 KB |
Output is correct |
41 |
Correct |
21 ms |
7892 KB |
Output is correct |
42 |
Correct |
21 ms |
7932 KB |
Output is correct |
43 |
Correct |
22 ms |
7892 KB |
Output is correct |
44 |
Correct |
18 ms |
7884 KB |
Output is correct |
45 |
Correct |
19 ms |
7892 KB |
Output is correct |
46 |
Correct |
21 ms |
7936 KB |
Output is correct |
47 |
Correct |
23 ms |
7920 KB |
Output is correct |
48 |
Correct |
19 ms |
7892 KB |
Output is correct |
49 |
Correct |
18 ms |
7984 KB |
Output is correct |
50 |
Correct |
20 ms |
7944 KB |
Output is correct |
51 |
Correct |
19 ms |
7872 KB |
Output is correct |
52 |
Correct |
21 ms |
8020 KB |
Output is correct |
53 |
Correct |
18 ms |
7880 KB |
Output is correct |
54 |
Correct |
18 ms |
7872 KB |
Output is correct |
55 |
Correct |
22 ms |
7892 KB |
Output is correct |
56 |
Correct |
406 ms |
35264 KB |
Output is correct |
57 |
Correct |
407 ms |
35260 KB |
Output is correct |
58 |
Correct |
410 ms |
36928 KB |
Output is correct |
59 |
Correct |
418 ms |
37556 KB |
Output is correct |
60 |
Correct |
457 ms |
36084 KB |
Output is correct |
61 |
Correct |
402 ms |
36924 KB |
Output is correct |
62 |
Correct |
400 ms |
35256 KB |
Output is correct |
63 |
Correct |
422 ms |
36036 KB |
Output is correct |
64 |
Correct |
421 ms |
35924 KB |
Output is correct |
65 |
Correct |
435 ms |
36040 KB |
Output is correct |
66 |
Correct |
466 ms |
36088 KB |
Output is correct |
67 |
Correct |
475 ms |
37292 KB |
Output is correct |
68 |
Correct |
406 ms |
36928 KB |
Output is correct |
69 |
Correct |
357 ms |
36532 KB |
Output is correct |
70 |
Correct |
468 ms |
36784 KB |
Output is correct |
71 |
Correct |
420 ms |
36868 KB |
Output is correct |
72 |
Correct |
351 ms |
37016 KB |
Output is correct |
73 |
Correct |
480 ms |
36864 KB |
Output is correct |
74 |
Correct |
351 ms |
37440 KB |
Output is correct |
75 |
Correct |
317 ms |
36612 KB |
Output is correct |
76 |
Correct |
414 ms |
36620 KB |
Output is correct |
77 |
Correct |
413 ms |
36780 KB |
Output is correct |
78 |
Correct |
465 ms |
36660 KB |
Output is correct |
79 |
Correct |
513 ms |
36544 KB |
Output is correct |
80 |
Correct |
454 ms |
36580 KB |
Output is correct |
81 |
Correct |
415 ms |
36928 KB |
Output is correct |
82 |
Correct |
449 ms |
35912 KB |
Output is correct |
83 |
Correct |
461 ms |
36092 KB |
Output is correct |
84 |
Correct |
489 ms |
35712 KB |
Output is correct |
85 |
Correct |
442 ms |
37248 KB |
Output is correct |
86 |
Correct |
450 ms |
37136 KB |
Output is correct |
87 |
Correct |
468 ms |
37084 KB |
Output is correct |
88 |
Correct |
463 ms |
39572 KB |
Output is correct |
89 |
Correct |
452 ms |
36892 KB |
Output is correct |
90 |
Correct |
430 ms |
36912 KB |
Output is correct |
91 |
Correct |
373 ms |
36728 KB |
Output is correct |
92 |
Correct |
452 ms |
37440 KB |
Output is correct |
93 |
Correct |
491 ms |
36876 KB |
Output is correct |
94 |
Correct |
463 ms |
37196 KB |
Output is correct |
95 |
Correct |
476 ms |
36744 KB |
Output is correct |
96 |
Correct |
407 ms |
36832 KB |
Output is correct |
97 |
Correct |
453 ms |
36544 KB |
Output is correct |
98 |
Correct |
456 ms |
36992 KB |
Output is correct |
99 |
Correct |
442 ms |
37108 KB |
Output is correct |
100 |
Correct |
387 ms |
36544 KB |
Output is correct |
101 |
Correct |
473 ms |
36928 KB |
Output is correct |
102 |
Correct |
455 ms |
37556 KB |
Output is correct |
103 |
Correct |
424 ms |
36672 KB |
Output is correct |