#include <bits/stdc++.h>
using namespace std;
#define pb push_back
#define sz(a) a.size()
#define fi first
#define se second
#define all(a) a.begin(),a.end()
typedef long long int ll;
typedef pair<ll,ll> pii;
const int MAXN = 2e5+1;
ll t[MAXN],l[MAXN],r[MAXN],c[MAXN],dist[MAXN];
int tc[MAXN];
bool ded[MAXN];
vector<pii>adj[MAXN];
vector<int>crd,add[MAXN];
set<pii>s0[4*MAXN],s1[4*MAXN];
void build(int v,int L,int R){
if(L==R){
for(int x:add[L]){
s0[v].insert({l[x] + t[x],x});
s1[v].insert({l[x] - t[x],x});
}
return;
}
int tm = (L+R)/2;
build(2*v,L,tm);
build(2*v+1,tm+1,R);
for(auto x:s0[2*v])s0[v].insert(x);
for(auto x:s0[2*v+1])s0[v].insert(x);
for(auto x:s1[2*v])s1[v].insert(x);
for(auto x:s1[2*v+1])s1[v].insert(x);
}
vector<int>upd;
void query0(int v,int l,int r,int tl,int tr,int val){ //when t[i] > t[j]
if(l>r)return;
if(l==tl && r==tr){
while(!s0[v].empty()){
auto it = s0[v].upper_bound(make_pair(val,2e9));
if(it==s0[v].begin())break;
it--;
if(ded[it->se]){
s0[v].erase(it);
continue;
}else{
ded[it->se] = 1;
upd.pb(it->se);
s0[v].erase(it);
}
}
return;
}
int tm = (tl+tr)/2;
query0(2*v,l,min(r,tm),tl,tm,val);
query0(2*v+1,max(l,tm+1),r,tm+1,tr,val);
}
void query1(int v,int l,int r,int tl,int tr,int val){ //when t[i] > t[j]
if(l>r)return;
if(l==tl && r==tr){
while(!s1[v].empty()){
auto it = s1[v].upper_bound(make_pair(val,2e9));
if(it==s1[v].begin())break;
it--;
if(ded[it->se]){
s1[v].erase(it);
continue;
}else{
ded[it->se] = 1;
upd.pb(it->se);
s1[v].erase(it);
}
}
return;
}
int tm = (tl+tr)/2;
query1(2*v,l,min(r,tm),tl,tm,val);
query1(2*v+1,max(l,tm+1),r,tm+1,tr,val);
}
//ri >= lj + abs(t[i]-t[j])
//if t[i] > t[j]
//ri - t[i] >= lj - t[j]
#define owo ios_base::sync_with_stdio(0);cin.tie(0);
int main()
{
owo
int n,m;
cin>>n>>m;
for(int i=0;i<m;i++){
cin>>t[i]>>l[i]>>r[i]>>c[i];
crd.pb(t[i]);
l[i]--;
}
sort(all(crd));
crd.resize(unique(all(crd)) - crd.begin());
int ss = sz(crd);
for(int i=0;i<m;i++){
tc[i] = lower_bound(all(crd),t[i]) - crd.begin();
add[tc[i]].pb(i);
}
build(1,0,ss-1);
//for(int i=0;i<m;i++)cout<<tc[i]<<" ";
//cout<<'\n';
priority_queue<pii,vector<pii>,greater<pii>>pq;
for(int i=0;i<m;i++){
dist[i] = 1e18;
if(l[i] == 0){
ded[i] = 1;
dist[i] = c[i];
pq.push({c[i],i});
}
}
while(!pq.empty()){
pii v = pq.top();
//cout<<v.se<<'\n';
pq.pop();
upd.clear();
query0(1,tc[v.se],ss-1,0,ss-1,r[v.se] + t[v.se]);
query1(1,0,tc[v.se],0,ss-1,r[v.se] - t[v.se]);
for(int x:upd){
//cout<<v.se<<" "<<x<<'\n';
if(dist[x] > dist[v.se] + c[x]){
dist[x] = dist[v.se] + c[x];
pq.push({dist[x],x});
}
}
}
ll ans = 1e18;
for(int i=0;i<m;i++){
if(r[i] == n)ans = min(ans,dist[i]);
}
if(ans==1e18)ans=-1;
cout<<ans;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
151 ms |
105776 KB |
Output is correct |
2 |
Correct |
159 ms |
105764 KB |
Output is correct |
3 |
Correct |
160 ms |
106060 KB |
Output is correct |
4 |
Correct |
189 ms |
105428 KB |
Output is correct |
5 |
Correct |
146 ms |
107772 KB |
Output is correct |
6 |
Correct |
126 ms |
105932 KB |
Output is correct |
7 |
Correct |
130 ms |
105716 KB |
Output is correct |
8 |
Correct |
128 ms |
105620 KB |
Output is correct |
9 |
Correct |
122 ms |
105708 KB |
Output is correct |
10 |
Correct |
123 ms |
105572 KB |
Output is correct |
11 |
Correct |
197 ms |
108188 KB |
Output is correct |
12 |
Correct |
186 ms |
108384 KB |
Output is correct |
13 |
Correct |
186 ms |
107876 KB |
Output is correct |
14 |
Correct |
202 ms |
107860 KB |
Output is correct |
15 |
Correct |
180 ms |
105756 KB |
Output is correct |
16 |
Correct |
178 ms |
105732 KB |
Output is correct |
17 |
Correct |
172 ms |
104924 KB |
Output is correct |
18 |
Correct |
188 ms |
107468 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
38 ms |
84856 KB |
Output is correct |
2 |
Correct |
40 ms |
84976 KB |
Output is correct |
3 |
Correct |
38 ms |
84892 KB |
Output is correct |
4 |
Correct |
40 ms |
84868 KB |
Output is correct |
5 |
Correct |
38 ms |
84812 KB |
Output is correct |
6 |
Correct |
39 ms |
84856 KB |
Output is correct |
7 |
Correct |
46 ms |
84984 KB |
Output is correct |
8 |
Correct |
39 ms |
84796 KB |
Output is correct |
9 |
Correct |
41 ms |
84816 KB |
Output is correct |
10 |
Correct |
38 ms |
84852 KB |
Output is correct |
11 |
Correct |
48 ms |
84780 KB |
Output is correct |
12 |
Correct |
40 ms |
84912 KB |
Output is correct |
13 |
Correct |
39 ms |
84876 KB |
Output is correct |
14 |
Correct |
40 ms |
84812 KB |
Output is correct |
15 |
Correct |
39 ms |
84792 KB |
Output is correct |
16 |
Correct |
39 ms |
84888 KB |
Output is correct |
17 |
Correct |
39 ms |
84820 KB |
Output is correct |
18 |
Correct |
42 ms |
84884 KB |
Output is correct |
19 |
Correct |
41 ms |
84880 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
38 ms |
84856 KB |
Output is correct |
2 |
Correct |
40 ms |
84976 KB |
Output is correct |
3 |
Correct |
38 ms |
84892 KB |
Output is correct |
4 |
Correct |
40 ms |
84868 KB |
Output is correct |
5 |
Correct |
38 ms |
84812 KB |
Output is correct |
6 |
Correct |
39 ms |
84856 KB |
Output is correct |
7 |
Correct |
46 ms |
84984 KB |
Output is correct |
8 |
Correct |
39 ms |
84796 KB |
Output is correct |
9 |
Correct |
41 ms |
84816 KB |
Output is correct |
10 |
Correct |
38 ms |
84852 KB |
Output is correct |
11 |
Correct |
48 ms |
84780 KB |
Output is correct |
12 |
Correct |
40 ms |
84912 KB |
Output is correct |
13 |
Correct |
39 ms |
84876 KB |
Output is correct |
14 |
Correct |
40 ms |
84812 KB |
Output is correct |
15 |
Correct |
39 ms |
84792 KB |
Output is correct |
16 |
Correct |
39 ms |
84888 KB |
Output is correct |
17 |
Correct |
39 ms |
84820 KB |
Output is correct |
18 |
Correct |
42 ms |
84884 KB |
Output is correct |
19 |
Correct |
41 ms |
84880 KB |
Output is correct |
20 |
Correct |
62 ms |
93024 KB |
Output is correct |
21 |
Correct |
76 ms |
93084 KB |
Output is correct |
22 |
Correct |
60 ms |
91484 KB |
Output is correct |
23 |
Correct |
59 ms |
91532 KB |
Output is correct |
24 |
Correct |
67 ms |
93972 KB |
Output is correct |
25 |
Correct |
68 ms |
93748 KB |
Output is correct |
26 |
Correct |
66 ms |
93772 KB |
Output is correct |
27 |
Correct |
65 ms |
93772 KB |
Output is correct |
28 |
Correct |
83 ms |
93976 KB |
Output is correct |
29 |
Correct |
66 ms |
93840 KB |
Output is correct |
30 |
Correct |
61 ms |
93772 KB |
Output is correct |
31 |
Correct |
58 ms |
93772 KB |
Output is correct |
32 |
Correct |
67 ms |
94208 KB |
Output is correct |
33 |
Correct |
64 ms |
93956 KB |
Output is correct |
34 |
Correct |
66 ms |
93304 KB |
Output is correct |
35 |
Correct |
72 ms |
93980 KB |
Output is correct |
36 |
Correct |
64 ms |
93768 KB |
Output is correct |
37 |
Correct |
70 ms |
93272 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
151 ms |
105776 KB |
Output is correct |
2 |
Correct |
159 ms |
105764 KB |
Output is correct |
3 |
Correct |
160 ms |
106060 KB |
Output is correct |
4 |
Correct |
189 ms |
105428 KB |
Output is correct |
5 |
Correct |
146 ms |
107772 KB |
Output is correct |
6 |
Correct |
126 ms |
105932 KB |
Output is correct |
7 |
Correct |
130 ms |
105716 KB |
Output is correct |
8 |
Correct |
128 ms |
105620 KB |
Output is correct |
9 |
Correct |
122 ms |
105708 KB |
Output is correct |
10 |
Correct |
123 ms |
105572 KB |
Output is correct |
11 |
Correct |
197 ms |
108188 KB |
Output is correct |
12 |
Correct |
186 ms |
108384 KB |
Output is correct |
13 |
Correct |
186 ms |
107876 KB |
Output is correct |
14 |
Correct |
202 ms |
107860 KB |
Output is correct |
15 |
Correct |
180 ms |
105756 KB |
Output is correct |
16 |
Correct |
178 ms |
105732 KB |
Output is correct |
17 |
Correct |
172 ms |
104924 KB |
Output is correct |
18 |
Correct |
188 ms |
107468 KB |
Output is correct |
19 |
Correct |
38 ms |
84856 KB |
Output is correct |
20 |
Correct |
40 ms |
84976 KB |
Output is correct |
21 |
Correct |
38 ms |
84892 KB |
Output is correct |
22 |
Correct |
40 ms |
84868 KB |
Output is correct |
23 |
Correct |
38 ms |
84812 KB |
Output is correct |
24 |
Correct |
39 ms |
84856 KB |
Output is correct |
25 |
Correct |
46 ms |
84984 KB |
Output is correct |
26 |
Correct |
39 ms |
84796 KB |
Output is correct |
27 |
Correct |
41 ms |
84816 KB |
Output is correct |
28 |
Correct |
38 ms |
84852 KB |
Output is correct |
29 |
Correct |
48 ms |
84780 KB |
Output is correct |
30 |
Correct |
40 ms |
84912 KB |
Output is correct |
31 |
Correct |
39 ms |
84876 KB |
Output is correct |
32 |
Correct |
40 ms |
84812 KB |
Output is correct |
33 |
Correct |
39 ms |
84792 KB |
Output is correct |
34 |
Correct |
39 ms |
84888 KB |
Output is correct |
35 |
Correct |
39 ms |
84820 KB |
Output is correct |
36 |
Correct |
42 ms |
84884 KB |
Output is correct |
37 |
Correct |
41 ms |
84880 KB |
Output is correct |
38 |
Correct |
62 ms |
93024 KB |
Output is correct |
39 |
Correct |
76 ms |
93084 KB |
Output is correct |
40 |
Correct |
60 ms |
91484 KB |
Output is correct |
41 |
Correct |
59 ms |
91532 KB |
Output is correct |
42 |
Correct |
67 ms |
93972 KB |
Output is correct |
43 |
Correct |
68 ms |
93748 KB |
Output is correct |
44 |
Correct |
66 ms |
93772 KB |
Output is correct |
45 |
Correct |
65 ms |
93772 KB |
Output is correct |
46 |
Correct |
83 ms |
93976 KB |
Output is correct |
47 |
Correct |
66 ms |
93840 KB |
Output is correct |
48 |
Correct |
61 ms |
93772 KB |
Output is correct |
49 |
Correct |
58 ms |
93772 KB |
Output is correct |
50 |
Correct |
67 ms |
94208 KB |
Output is correct |
51 |
Correct |
64 ms |
93956 KB |
Output is correct |
52 |
Correct |
66 ms |
93304 KB |
Output is correct |
53 |
Correct |
72 ms |
93980 KB |
Output is correct |
54 |
Correct |
64 ms |
93768 KB |
Output is correct |
55 |
Correct |
70 ms |
93272 KB |
Output is correct |
56 |
Correct |
852 ms |
303800 KB |
Output is correct |
57 |
Correct |
847 ms |
304280 KB |
Output is correct |
58 |
Correct |
758 ms |
302544 KB |
Output is correct |
59 |
Correct |
784 ms |
303000 KB |
Output is correct |
60 |
Correct |
725 ms |
273756 KB |
Output is correct |
61 |
Correct |
775 ms |
302652 KB |
Output is correct |
62 |
Correct |
841 ms |
303860 KB |
Output is correct |
63 |
Correct |
786 ms |
317520 KB |
Output is correct |
64 |
Correct |
853 ms |
317508 KB |
Output is correct |
65 |
Correct |
267 ms |
118488 KB |
Output is correct |
66 |
Correct |
694 ms |
273816 KB |
Output is correct |
67 |
Correct |
1172 ms |
318424 KB |
Output is correct |
68 |
Correct |
981 ms |
318172 KB |
Output is correct |
69 |
Correct |
885 ms |
316364 KB |
Output is correct |
70 |
Correct |
1156 ms |
318460 KB |
Output is correct |
71 |
Correct |
975 ms |
318112 KB |
Output is correct |
72 |
Correct |
940 ms |
318204 KB |
Output is correct |
73 |
Correct |
1181 ms |
318392 KB |
Output is correct |
74 |
Correct |
700 ms |
318148 KB |
Output is correct |
75 |
Correct |
690 ms |
318108 KB |
Output is correct |
76 |
Correct |
864 ms |
309208 KB |
Output is correct |
77 |
Correct |
940 ms |
320724 KB |
Output is correct |
78 |
Correct |
1043 ms |
309736 KB |
Output is correct |
79 |
Correct |
1189 ms |
318324 KB |
Output is correct |
80 |
Correct |
1151 ms |
307612 KB |
Output is correct |
81 |
Correct |
702 ms |
318144 KB |
Output is correct |
82 |
Correct |
1163 ms |
307052 KB |
Output is correct |
83 |
Correct |
1179 ms |
317496 KB |
Output is correct |
84 |
Correct |
1155 ms |
317512 KB |
Output is correct |
85 |
Correct |
873 ms |
318156 KB |
Output is correct |
86 |
Correct |
986 ms |
318236 KB |
Output is correct |
87 |
Correct |
1040 ms |
318260 KB |
Output is correct |
88 |
Correct |
1018 ms |
318200 KB |
Output is correct |
89 |
Correct |
906 ms |
318192 KB |
Output is correct |
90 |
Correct |
968 ms |
319784 KB |
Output is correct |
91 |
Correct |
1053 ms |
319072 KB |
Output is correct |
92 |
Correct |
962 ms |
318152 KB |
Output is correct |
93 |
Correct |
1398 ms |
318240 KB |
Output is correct |
94 |
Correct |
1164 ms |
318252 KB |
Output is correct |
95 |
Correct |
1235 ms |
318204 KB |
Output is correct |
96 |
Correct |
1086 ms |
320268 KB |
Output is correct |
97 |
Correct |
1088 ms |
320604 KB |
Output is correct |
98 |
Correct |
989 ms |
320636 KB |
Output is correct |
99 |
Correct |
1010 ms |
320400 KB |
Output is correct |
100 |
Correct |
987 ms |
320516 KB |
Output is correct |
101 |
Correct |
938 ms |
320560 KB |
Output is correct |
102 |
Correct |
928 ms |
320040 KB |
Output is correct |
103 |
Correct |
1080 ms |
318404 KB |
Output is correct |