#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;
typedef pair<int,int> pii;
#define F first
#define S second
const int MN = 1e5+5;
const ll inf = 1e12;
ll N, M, i, j, x, y, c, t, dis[MN], vs[MN], ans=1LL<<60;
struct idk{ll a,b,c,d;}arr[MN],go[MN];
map<ll,ll> mpx, mpy; vector<ll> ed;
struct pq{bool operator()(const pll&i,const pll&j){return i.S>j.S;}};
priority_queue<pll,vector<pll>,pq> q;
struct segtree{
vector<pii> st[6*MN]; ll n, c;
void init(ll N){n=N;}
void prep(){
for(int i=0;i<6*MN;i++)
sort(st[i].begin(),st[i].end(),[](pii i,pii j){return i.F>j.F;});
}
void upd(ll i,ll s,ll e,ll ind,ll val,ll id){
st[i].push_back({val,id});
if(s^e){
if((s+e)/2<ind) upd(2*i+1,(s+e)/2+1,e,ind,val,id);
else upd(2*i,s,(s+e)/2,ind,val,id);
}
}
inline void update(ll x,ll y,ll id){
upd(1,1,n,x,y,id);
}
void qu(ll i,ll s,ll e,ll ss,ll se,ll y){
if(s>=ss&&e<=se){
while(st[i].size()&&st[i].back().F<=y){
int id = st[i].back().S;
if(!vs[id]){
vs[id]=1;
q.push({id,c+arr[id].c});
}
st[i].pop_back();
}
}
else if((s+e)/2<ss) qu(2*i+1,(s+e)/2+1,e,ss,se,y);
else if((s+e)/2>=se) qu(2*i,s,(s+e)/2,ss,se,y);
else qu(2*i,s,(s+e)/2,ss,se,y), qu(2*i+1,(s+e)/2+1,e,ss,se,y);
}
inline void query(ll s,ll e,ll y,ll cur){
c = cur;
qu(1,1,n,s,e,y);
}
}st;
int main(){
scanf("%lld%lld",&N,&M);
for(i=1;i<=M;i++){
scanf("%lld%lld%lld%lld",&t,&x,&y,&c);
arr[i]={x,y,c,t};
if(x==1) q.push({i,c}), x=-inf, vs[i]=1;
if(y==N) ed.push_back(i), y=inf;
y++;
ll sx = t-y, ex = t-x;
ll sy = t+x, ey = t+y;
go[i]={sx,ex,sy,ey};
mpx[sx]=mpx[ex]=mpy[sy]=mpy[ey]=0;
}
i=0; st.init(mpx.size());
for(auto it=mpx.begin();it!=mpx.end();it++)
it->second = ++i;
i=0;
for(auto it=mpy.begin();it!=mpy.end();it++)
it->second = ++i;
for(i=1;i<=M;i++){
go[i].a=mpx[go[i].a]; go[i].b=mpx[go[i].b];
go[i].c=mpy[go[i].c]; go[i].d=mpy[go[i].d];
st.update(go[i].b,go[i].c,i);
}
st.prep();
memset(dis,-1,sizeof(dis));
while(q.size()){
pll x=q.top(); q.pop();
dis[x.F]=x.S;
int id = x.F;
st.query(go[id].a,mpx.size(),go[id].d,x.S);
}
for(auto v : ed){
if(dis[v]!=-1) ans=min(ans,dis[v]);
}
printf("%lld\n",ans==1LL<<60?-1:ans);
return 0;
}
Compilation message
treatment.cpp: In function 'int main()':
treatment.cpp:57:10: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld%lld",&N,&M);
~~~~~^~~~~~~~~~~~~~~~~~
treatment.cpp:59:14: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%lld%lld%lld%lld",&t,&x,&y,&c);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
685 ms |
58768 KB |
Output is correct |
2 |
Correct |
627 ms |
58688 KB |
Output is correct |
3 |
Correct |
420 ms |
48876 KB |
Output is correct |
4 |
Correct |
446 ms |
48828 KB |
Output is correct |
5 |
Correct |
147 ms |
34440 KB |
Output is correct |
6 |
Correct |
185 ms |
36196 KB |
Output is correct |
7 |
Correct |
264 ms |
41188 KB |
Output is correct |
8 |
Correct |
113 ms |
32996 KB |
Output is correct |
9 |
Correct |
173 ms |
36700 KB |
Output is correct |
10 |
Correct |
243 ms |
43236 KB |
Output is correct |
11 |
Correct |
849 ms |
69352 KB |
Output is correct |
12 |
Correct |
838 ms |
69220 KB |
Output is correct |
13 |
Correct |
1043 ms |
75748 KB |
Output is correct |
14 |
Correct |
1219 ms |
75688 KB |
Output is correct |
15 |
Correct |
1024 ms |
73644 KB |
Output is correct |
16 |
Correct |
988 ms |
73480 KB |
Output is correct |
17 |
Correct |
1150 ms |
72848 KB |
Output is correct |
18 |
Correct |
934 ms |
68404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
15232 KB |
Output is correct |
2 |
Correct |
11 ms |
15232 KB |
Output is correct |
3 |
Correct |
13 ms |
15232 KB |
Output is correct |
4 |
Correct |
11 ms |
15232 KB |
Output is correct |
5 |
Correct |
12 ms |
15232 KB |
Output is correct |
6 |
Correct |
13 ms |
15232 KB |
Output is correct |
7 |
Correct |
11 ms |
15232 KB |
Output is correct |
8 |
Correct |
13 ms |
15232 KB |
Output is correct |
9 |
Correct |
13 ms |
15232 KB |
Output is correct |
10 |
Correct |
12 ms |
15232 KB |
Output is correct |
11 |
Correct |
13 ms |
15232 KB |
Output is correct |
12 |
Correct |
15 ms |
15232 KB |
Output is correct |
13 |
Correct |
11 ms |
15208 KB |
Output is correct |
14 |
Correct |
12 ms |
15232 KB |
Output is correct |
15 |
Correct |
12 ms |
15232 KB |
Output is correct |
16 |
Correct |
12 ms |
15232 KB |
Output is correct |
17 |
Correct |
12 ms |
15232 KB |
Output is correct |
18 |
Correct |
13 ms |
15232 KB |
Output is correct |
19 |
Correct |
12 ms |
15232 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
11 ms |
15232 KB |
Output is correct |
2 |
Correct |
11 ms |
15232 KB |
Output is correct |
3 |
Correct |
13 ms |
15232 KB |
Output is correct |
4 |
Correct |
11 ms |
15232 KB |
Output is correct |
5 |
Correct |
12 ms |
15232 KB |
Output is correct |
6 |
Correct |
13 ms |
15232 KB |
Output is correct |
7 |
Correct |
11 ms |
15232 KB |
Output is correct |
8 |
Correct |
13 ms |
15232 KB |
Output is correct |
9 |
Correct |
13 ms |
15232 KB |
Output is correct |
10 |
Correct |
12 ms |
15232 KB |
Output is correct |
11 |
Correct |
13 ms |
15232 KB |
Output is correct |
12 |
Correct |
15 ms |
15232 KB |
Output is correct |
13 |
Correct |
11 ms |
15208 KB |
Output is correct |
14 |
Correct |
12 ms |
15232 KB |
Output is correct |
15 |
Correct |
12 ms |
15232 KB |
Output is correct |
16 |
Correct |
12 ms |
15232 KB |
Output is correct |
17 |
Correct |
12 ms |
15232 KB |
Output is correct |
18 |
Correct |
13 ms |
15232 KB |
Output is correct |
19 |
Correct |
12 ms |
15232 KB |
Output is correct |
20 |
Correct |
32 ms |
17400 KB |
Output is correct |
21 |
Correct |
31 ms |
17408 KB |
Output is correct |
22 |
Correct |
32 ms |
17784 KB |
Output is correct |
23 |
Correct |
35 ms |
17656 KB |
Output is correct |
24 |
Correct |
37 ms |
17912 KB |
Output is correct |
25 |
Correct |
33 ms |
17656 KB |
Output is correct |
26 |
Correct |
30 ms |
17792 KB |
Output is correct |
27 |
Correct |
35 ms |
17656 KB |
Output is correct |
28 |
Correct |
34 ms |
18304 KB |
Output is correct |
29 |
Correct |
39 ms |
18168 KB |
Output is correct |
30 |
Correct |
27 ms |
18096 KB |
Output is correct |
31 |
Correct |
27 ms |
18048 KB |
Output is correct |
32 |
Correct |
39 ms |
18176 KB |
Output is correct |
33 |
Correct |
34 ms |
18168 KB |
Output is correct |
34 |
Correct |
39 ms |
18048 KB |
Output is correct |
35 |
Correct |
36 ms |
18176 KB |
Output is correct |
36 |
Correct |
34 ms |
18176 KB |
Output is correct |
37 |
Correct |
32 ms |
18040 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
685 ms |
58768 KB |
Output is correct |
2 |
Correct |
627 ms |
58688 KB |
Output is correct |
3 |
Correct |
420 ms |
48876 KB |
Output is correct |
4 |
Correct |
446 ms |
48828 KB |
Output is correct |
5 |
Correct |
147 ms |
34440 KB |
Output is correct |
6 |
Correct |
185 ms |
36196 KB |
Output is correct |
7 |
Correct |
264 ms |
41188 KB |
Output is correct |
8 |
Correct |
113 ms |
32996 KB |
Output is correct |
9 |
Correct |
173 ms |
36700 KB |
Output is correct |
10 |
Correct |
243 ms |
43236 KB |
Output is correct |
11 |
Correct |
849 ms |
69352 KB |
Output is correct |
12 |
Correct |
838 ms |
69220 KB |
Output is correct |
13 |
Correct |
1043 ms |
75748 KB |
Output is correct |
14 |
Correct |
1219 ms |
75688 KB |
Output is correct |
15 |
Correct |
1024 ms |
73644 KB |
Output is correct |
16 |
Correct |
988 ms |
73480 KB |
Output is correct |
17 |
Correct |
1150 ms |
72848 KB |
Output is correct |
18 |
Correct |
934 ms |
68404 KB |
Output is correct |
19 |
Correct |
11 ms |
15232 KB |
Output is correct |
20 |
Correct |
11 ms |
15232 KB |
Output is correct |
21 |
Correct |
13 ms |
15232 KB |
Output is correct |
22 |
Correct |
11 ms |
15232 KB |
Output is correct |
23 |
Correct |
12 ms |
15232 KB |
Output is correct |
24 |
Correct |
13 ms |
15232 KB |
Output is correct |
25 |
Correct |
11 ms |
15232 KB |
Output is correct |
26 |
Correct |
13 ms |
15232 KB |
Output is correct |
27 |
Correct |
13 ms |
15232 KB |
Output is correct |
28 |
Correct |
12 ms |
15232 KB |
Output is correct |
29 |
Correct |
13 ms |
15232 KB |
Output is correct |
30 |
Correct |
15 ms |
15232 KB |
Output is correct |
31 |
Correct |
11 ms |
15208 KB |
Output is correct |
32 |
Correct |
12 ms |
15232 KB |
Output is correct |
33 |
Correct |
12 ms |
15232 KB |
Output is correct |
34 |
Correct |
12 ms |
15232 KB |
Output is correct |
35 |
Correct |
12 ms |
15232 KB |
Output is correct |
36 |
Correct |
13 ms |
15232 KB |
Output is correct |
37 |
Correct |
12 ms |
15232 KB |
Output is correct |
38 |
Correct |
32 ms |
17400 KB |
Output is correct |
39 |
Correct |
31 ms |
17408 KB |
Output is correct |
40 |
Correct |
32 ms |
17784 KB |
Output is correct |
41 |
Correct |
35 ms |
17656 KB |
Output is correct |
42 |
Correct |
37 ms |
17912 KB |
Output is correct |
43 |
Correct |
33 ms |
17656 KB |
Output is correct |
44 |
Correct |
30 ms |
17792 KB |
Output is correct |
45 |
Correct |
35 ms |
17656 KB |
Output is correct |
46 |
Correct |
34 ms |
18304 KB |
Output is correct |
47 |
Correct |
39 ms |
18168 KB |
Output is correct |
48 |
Correct |
27 ms |
18096 KB |
Output is correct |
49 |
Correct |
27 ms |
18048 KB |
Output is correct |
50 |
Correct |
39 ms |
18176 KB |
Output is correct |
51 |
Correct |
34 ms |
18168 KB |
Output is correct |
52 |
Correct |
39 ms |
18048 KB |
Output is correct |
53 |
Correct |
36 ms |
18176 KB |
Output is correct |
54 |
Correct |
34 ms |
18176 KB |
Output is correct |
55 |
Correct |
32 ms |
18040 KB |
Output is correct |
56 |
Correct |
843 ms |
65232 KB |
Output is correct |
57 |
Correct |
945 ms |
68312 KB |
Output is correct |
58 |
Correct |
844 ms |
57744 KB |
Output is correct |
59 |
Correct |
966 ms |
58380 KB |
Output is correct |
60 |
Correct |
1135 ms |
66660 KB |
Output is correct |
61 |
Correct |
992 ms |
57672 KB |
Output is correct |
62 |
Correct |
866 ms |
65348 KB |
Output is correct |
63 |
Correct |
935 ms |
68196 KB |
Output is correct |
64 |
Correct |
755 ms |
65020 KB |
Output is correct |
65 |
Correct |
928 ms |
69612 KB |
Output is correct |
66 |
Correct |
952 ms |
66656 KB |
Output is correct |
67 |
Correct |
860 ms |
67160 KB |
Output is correct |
68 |
Correct |
708 ms |
67300 KB |
Output is correct |
69 |
Correct |
556 ms |
67044 KB |
Output is correct |
70 |
Correct |
1060 ms |
67268 KB |
Output is correct |
71 |
Correct |
772 ms |
67132 KB |
Output is correct |
72 |
Correct |
619 ms |
66788 KB |
Output is correct |
73 |
Correct |
1094 ms |
74808 KB |
Output is correct |
74 |
Correct |
765 ms |
73900 KB |
Output is correct |
75 |
Correct |
542 ms |
73576 KB |
Output is correct |
76 |
Correct |
1356 ms |
74848 KB |
Output is correct |
77 |
Correct |
1440 ms |
77800 KB |
Output is correct |
78 |
Correct |
1367 ms |
76104 KB |
Output is correct |
79 |
Correct |
1256 ms |
74612 KB |
Output is correct |
80 |
Correct |
1166 ms |
74088 KB |
Output is correct |
81 |
Correct |
1123 ms |
73756 KB |
Output is correct |
82 |
Correct |
1104 ms |
73384 KB |
Output is correct |
83 |
Correct |
1191 ms |
73524 KB |
Output is correct |
84 |
Correct |
1390 ms |
73700 KB |
Output is correct |
85 |
Correct |
1179 ms |
73444 KB |
Output is correct |
86 |
Correct |
986 ms |
72936 KB |
Output is correct |
87 |
Correct |
1130 ms |
72932 KB |
Output is correct |
88 |
Correct |
981 ms |
66268 KB |
Output is correct |
89 |
Correct |
1032 ms |
72932 KB |
Output is correct |
90 |
Correct |
1504 ms |
74084 KB |
Output is correct |
91 |
Correct |
1435 ms |
76388 KB |
Output is correct |
92 |
Correct |
1372 ms |
75748 KB |
Output is correct |
93 |
Correct |
1174 ms |
74976 KB |
Output is correct |
94 |
Correct |
966 ms |
66272 KB |
Output is correct |
95 |
Correct |
1184 ms |
75620 KB |
Output is correct |
96 |
Correct |
1201 ms |
76252 KB |
Output is correct |
97 |
Correct |
1425 ms |
76516 KB |
Output is correct |
98 |
Correct |
1446 ms |
76388 KB |
Output is correct |
99 |
Correct |
1389 ms |
76520 KB |
Output is correct |
100 |
Correct |
1041 ms |
69728 KB |
Output is correct |
101 |
Correct |
1196 ms |
76516 KB |
Output is correct |
102 |
Correct |
1157 ms |
77412 KB |
Output is correct |
103 |
Correct |
1045 ms |
76004 KB |
Output is correct |