#include <bits/stdc++.h>
#pragma GCC optimize("Ofast")
#define AquA cin.tie(0);ios_base::sync_with_stdio(0);
#define fs first
#define sc second
#define p_q priority_queue
#define int long long
using namespace std;
const int inf=1e18;
struct ST{
vector<set<pair<int,int> > > st;
void init(int x){
st.resize(4*x);
}
void update(int v,int L,int R,int p,pair<int,int> k,int t){
if(t>0){
st[v].insert(k);
}
else{
st[v].erase(st[v].find(k));
}
if(L==R){
return;
}
int m=(L+R)/2;
if(p<=m){
update(2*v+1,L,m,p,k,t);
}
else{
update(2*v+2,m+1,R,p,k,t);
}
}
pair<int,int> query(int v,int L,int R,int p){
if(L==R){
if(st[v].size()){
return *st[v].begin();
}
else{
return {inf,-1};
}
}
int m=(L+R)/2;
if(p<=m){
return query(2*v+1,L,m,p);
}
else{
pair<int,int> a,b=query(2*v+2,m+1,R,p);
if(st[2*v+1].size()){
a=*st[2*v+1].begin();
}
else{
a={inf,-1};
}
return min(a,b);
}
}
}st;
signed main(){
AquA;
int n,m;
cin >> m >> n;
vector<int> cx(n),px,py,a(n),b(n);
vector<pair<int,int> > vl(n),vr(n);
for(int i=0;i<n;i++){
int l,r,t,c;
cin >> t >> l >> r >> c;
a[i]=l;
b[i]=r;
vl[i]={l-t,l+t};
vr[i]={r-t+1,r+t+1};
px.push_back(vl[i].fs);
px.push_back(vr[i].fs);
py.push_back(vl[i].sc);
py.push_back(vr[i].sc);
// vl[l] to node <= vr[r]
cx[i]=c;
}
sort(px.begin(),px.end());
sort(py.begin(),py.end());
px.erase(unique(px.begin(),px.end()),px.end());
py.erase(unique(py.begin(),py.end()),py.end());
int s=px.size();
st.init(s);
for(int i=0;i<n;i++){
vl[i].fs=lower_bound(px.begin(),px.end(),vl[i].fs)-px.begin();
vr[i].fs=lower_bound(px.begin(),px.end(),vr[i].fs)-px.begin();
vl[i].sc=lower_bound(py.begin(),py.end(),vl[i].sc)-py.begin();
vr[i].sc=lower_bound(py.begin(),py.end(),vr[i].sc)-py.begin();
st.update(0,0,s-1,vl[i].fs,{vl[i].sc,i},1);
}
int ans=1e18;
p_q<pair<int,int>,vector<pair<int,int> >,greater<pair<int,int> > > pq;
for(int i=0;i<n;i++){
if(a[i]==1){
pq.push({cx[i],i});
st.update(0,0,s-1,vl[i].fs,{vl[i].sc,i},-1);
}
}
while(pq.size()){
auto h=pq.top();
//cout << h.fs << " " << h.sc << "\n";
pq.pop();
if(b[h.sc]==m){
ans=min(ans,h.fs);
}
while(1){
auto y=st.query(0,0,s-1,vr[h.sc].fs);
if(y.sc==-1 || y.fs>vr[h.sc].sc){
break;
}
st.update(0,0,s-1,vl[y.sc].fs,{vl[y.sc].sc,y.sc},-1);
pq.push({h.fs+cx[y.sc],y.sc});
}
}
if(ans>5e17){
cout << -1 << "\n";
}
else{
cout << ans << "\n";
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
826 ms |
141720 KB |
Output is correct |
2 |
Correct |
897 ms |
141664 KB |
Output is correct |
3 |
Correct |
805 ms |
127772 KB |
Output is correct |
4 |
Correct |
754 ms |
127084 KB |
Output is correct |
5 |
Correct |
352 ms |
63536 KB |
Output is correct |
6 |
Correct |
512 ms |
81100 KB |
Output is correct |
7 |
Correct |
394 ms |
103868 KB |
Output is correct |
8 |
Correct |
298 ms |
66704 KB |
Output is correct |
9 |
Correct |
627 ms |
87456 KB |
Output is correct |
10 |
Correct |
456 ms |
112100 KB |
Output is correct |
11 |
Correct |
871 ms |
157716 KB |
Output is correct |
12 |
Correct |
911 ms |
157588 KB |
Output is correct |
13 |
Correct |
928 ms |
169612 KB |
Output is correct |
14 |
Correct |
1000 ms |
169532 KB |
Output is correct |
15 |
Correct |
938 ms |
166812 KB |
Output is correct |
16 |
Correct |
936 ms |
166604 KB |
Output is correct |
17 |
Correct |
909 ms |
165820 KB |
Output is correct |
18 |
Correct |
903 ms |
156720 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
324 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
316 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
320 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
320 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
324 KB |
Output is correct |
7 |
Correct |
0 ms |
212 KB |
Output is correct |
8 |
Correct |
0 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
316 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
1 ms |
340 KB |
Output is correct |
13 |
Correct |
0 ms |
340 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
320 KB |
Output is correct |
16 |
Correct |
1 ms |
212 KB |
Output is correct |
17 |
Correct |
0 ms |
212 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
22 ms |
6344 KB |
Output is correct |
21 |
Correct |
25 ms |
6424 KB |
Output is correct |
22 |
Correct |
27 ms |
6732 KB |
Output is correct |
23 |
Correct |
25 ms |
6728 KB |
Output is correct |
24 |
Correct |
31 ms |
6864 KB |
Output is correct |
25 |
Correct |
26 ms |
6740 KB |
Output is correct |
26 |
Correct |
21 ms |
6700 KB |
Output is correct |
27 |
Correct |
17 ms |
6784 KB |
Output is correct |
28 |
Correct |
27 ms |
7412 KB |
Output is correct |
29 |
Correct |
23 ms |
7380 KB |
Output is correct |
30 |
Correct |
16 ms |
7348 KB |
Output is correct |
31 |
Correct |
15 ms |
7284 KB |
Output is correct |
32 |
Correct |
28 ms |
7564 KB |
Output is correct |
33 |
Correct |
25 ms |
7516 KB |
Output is correct |
34 |
Correct |
22 ms |
7324 KB |
Output is correct |
35 |
Correct |
26 ms |
7508 KB |
Output is correct |
36 |
Correct |
22 ms |
7436 KB |
Output is correct |
37 |
Correct |
22 ms |
7292 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
826 ms |
141720 KB |
Output is correct |
2 |
Correct |
897 ms |
141664 KB |
Output is correct |
3 |
Correct |
805 ms |
127772 KB |
Output is correct |
4 |
Correct |
754 ms |
127084 KB |
Output is correct |
5 |
Correct |
352 ms |
63536 KB |
Output is correct |
6 |
Correct |
512 ms |
81100 KB |
Output is correct |
7 |
Correct |
394 ms |
103868 KB |
Output is correct |
8 |
Correct |
298 ms |
66704 KB |
Output is correct |
9 |
Correct |
627 ms |
87456 KB |
Output is correct |
10 |
Correct |
456 ms |
112100 KB |
Output is correct |
11 |
Correct |
871 ms |
157716 KB |
Output is correct |
12 |
Correct |
911 ms |
157588 KB |
Output is correct |
13 |
Correct |
928 ms |
169612 KB |
Output is correct |
14 |
Correct |
1000 ms |
169532 KB |
Output is correct |
15 |
Correct |
938 ms |
166812 KB |
Output is correct |
16 |
Correct |
936 ms |
166604 KB |
Output is correct |
17 |
Correct |
909 ms |
165820 KB |
Output is correct |
18 |
Correct |
903 ms |
156720 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
1 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
320 KB |
Output is correct |
22 |
Correct |
1 ms |
212 KB |
Output is correct |
23 |
Correct |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
1 ms |
324 KB |
Output is correct |
25 |
Correct |
0 ms |
212 KB |
Output is correct |
26 |
Correct |
0 ms |
212 KB |
Output is correct |
27 |
Correct |
1 ms |
212 KB |
Output is correct |
28 |
Correct |
1 ms |
316 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
1 ms |
340 KB |
Output is correct |
31 |
Correct |
0 ms |
340 KB |
Output is correct |
32 |
Correct |
1 ms |
212 KB |
Output is correct |
33 |
Correct |
0 ms |
320 KB |
Output is correct |
34 |
Correct |
1 ms |
212 KB |
Output is correct |
35 |
Correct |
0 ms |
212 KB |
Output is correct |
36 |
Correct |
1 ms |
212 KB |
Output is correct |
37 |
Correct |
1 ms |
212 KB |
Output is correct |
38 |
Correct |
22 ms |
6344 KB |
Output is correct |
39 |
Correct |
25 ms |
6424 KB |
Output is correct |
40 |
Correct |
27 ms |
6732 KB |
Output is correct |
41 |
Correct |
25 ms |
6728 KB |
Output is correct |
42 |
Correct |
31 ms |
6864 KB |
Output is correct |
43 |
Correct |
26 ms |
6740 KB |
Output is correct |
44 |
Correct |
21 ms |
6700 KB |
Output is correct |
45 |
Correct |
17 ms |
6784 KB |
Output is correct |
46 |
Correct |
27 ms |
7412 KB |
Output is correct |
47 |
Correct |
23 ms |
7380 KB |
Output is correct |
48 |
Correct |
16 ms |
7348 KB |
Output is correct |
49 |
Correct |
15 ms |
7284 KB |
Output is correct |
50 |
Correct |
28 ms |
7564 KB |
Output is correct |
51 |
Correct |
25 ms |
7516 KB |
Output is correct |
52 |
Correct |
22 ms |
7324 KB |
Output is correct |
53 |
Correct |
26 ms |
7508 KB |
Output is correct |
54 |
Correct |
22 ms |
7436 KB |
Output is correct |
55 |
Correct |
22 ms |
7292 KB |
Output is correct |
56 |
Correct |
908 ms |
149136 KB |
Output is correct |
57 |
Correct |
1051 ms |
149424 KB |
Output is correct |
58 |
Correct |
966 ms |
140820 KB |
Output is correct |
59 |
Correct |
901 ms |
141344 KB |
Output is correct |
60 |
Correct |
1064 ms |
154780 KB |
Output is correct |
61 |
Correct |
879 ms |
141040 KB |
Output is correct |
62 |
Correct |
986 ms |
148960 KB |
Output is correct |
63 |
Correct |
852 ms |
166828 KB |
Output is correct |
64 |
Correct |
819 ms |
141732 KB |
Output is correct |
65 |
Correct |
819 ms |
154796 KB |
Output is correct |
66 |
Correct |
1061 ms |
154844 KB |
Output is correct |
67 |
Correct |
1372 ms |
155632 KB |
Output is correct |
68 |
Correct |
1066 ms |
155384 KB |
Output is correct |
69 |
Correct |
707 ms |
155224 KB |
Output is correct |
70 |
Correct |
1354 ms |
155724 KB |
Output is correct |
71 |
Correct |
1094 ms |
155420 KB |
Output is correct |
72 |
Correct |
704 ms |
155404 KB |
Output is correct |
73 |
Correct |
1422 ms |
167540 KB |
Output is correct |
74 |
Correct |
839 ms |
167308 KB |
Output is correct |
75 |
Correct |
649 ms |
167480 KB |
Output is correct |
76 |
Correct |
998 ms |
167800 KB |
Output is correct |
77 |
Correct |
1466 ms |
170616 KB |
Output is correct |
78 |
Correct |
998 ms |
170088 KB |
Output is correct |
79 |
Correct |
1638 ms |
167456 KB |
Output is correct |
80 |
Correct |
982 ms |
167148 KB |
Output is correct |
81 |
Correct |
1306 ms |
167396 KB |
Output is correct |
82 |
Correct |
943 ms |
166548 KB |
Output is correct |
83 |
Correct |
1187 ms |
166692 KB |
Output is correct |
84 |
Correct |
1657 ms |
166692 KB |
Output is correct |
85 |
Correct |
1385 ms |
167472 KB |
Output is correct |
86 |
Correct |
1364 ms |
160356 KB |
Output is correct |
87 |
Correct |
1354 ms |
163892 KB |
Output is correct |
88 |
Correct |
970 ms |
155484 KB |
Output is correct |
89 |
Correct |
1259 ms |
163976 KB |
Output is correct |
90 |
Correct |
1446 ms |
167100 KB |
Output is correct |
91 |
Correct |
1556 ms |
169340 KB |
Output is correct |
92 |
Correct |
1407 ms |
167292 KB |
Output is correct |
93 |
Correct |
1617 ms |
167344 KB |
Output is correct |
94 |
Correct |
1142 ms |
155592 KB |
Output is correct |
95 |
Correct |
1459 ms |
167420 KB |
Output is correct |
96 |
Correct |
1521 ms |
170456 KB |
Output is correct |
97 |
Correct |
1533 ms |
170552 KB |
Output is correct |
98 |
Correct |
1506 ms |
170520 KB |
Output is correct |
99 |
Correct |
1494 ms |
170440 KB |
Output is correct |
100 |
Correct |
1136 ms |
159336 KB |
Output is correct |
101 |
Correct |
1513 ms |
170688 KB |
Output is correct |
102 |
Correct |
1474 ms |
170492 KB |
Output is correct |
103 |
Correct |
1441 ms |
167924 KB |
Output is correct |