#include<bits/stdc++.h>
using namespace std;
typedef long double ld;
typedef long long ll;
#define rep(a, b) for(int a = 0; a < (b); ++a)
#define st first
#define nd second
#define pb push_back
#define all(a) a.begin(), a.end()
const int LIM=1e5+7, INF=1e9+7;
pair<pair<int,int>,pair<int,int>>T[LIM];
vector<pair<int,pair<int,int>>>P[2];
int odl[10*LIM][2], akt, N=1;
map<int,int>mp;
vector<int>skal;
set<pair<int,int>>tr[1<<21][2];
void upd(int l, int r, int k, pair<int,int>x) {
if(l>r) return;
l+=N; r+=N;
tr[l][k].insert(x);
tr[r][k].insert(x);
while(l/2!=r/2) {
if(l%2==0) tr[l+1][k].insert(x);
if(r%2==1) tr[r-1][k].insert(x);
l/=2; r/=2;
}
}
int main() {
ios_base::sync_with_stdio(0); cin.tie(0);
int x1, y1, x2, y2, n;
cin >> x1 >> y1 >> x2 >> y2 >> n;
rep(i, n) cin >> T[i].st.st >> T[i].nd.st >> T[i].st.nd >> T[i].nd.nd;
rep(xd, 2) {
vector<pair<pair<int,int>,int>>V;
rep(i, n) {
if(T[i].st.st<T[i].nd.st && T[i].st.nd<T[i].nd.nd) {
V.pb({{T[i].nd.nd, 0}, T[i].st.st});
V.pb({{T[i].nd.nd, 0}, T[i].nd.st-1});
V.pb({{T[i].st.nd+1, -1}, T[i].st.st});
V.pb({{T[i].st.nd+1, -1}, T[i].nd.st-1});
}
V.pb({{T[i].st.nd, 2}, T[i].st.st});
V.pb({{T[i].nd.nd, 2}, T[i].st.st});
}
V.pb({{y1, 2}, x1});
V.pb({{y2, 2}, x2});
sort(all(V));
set<int>S;
S.insert(-INF);
S.insert(INF);
for(auto i : V) {
if(i.st.nd==0) S.erase(i.nd);
else if(i.st.nd==-1) S.insert(i.nd);
else {
auto it=S.lower_bound(i.nd);
auto b=*it;
--it;
auto a=*it;
P[xd].pb({i.st.st, {a+1, b}});
}
}
rep(i, n) {
swap(T[i].st.st, T[i].st.nd);
swap(T[i].nd.st, T[i].nd.nd);
}
swap(x1, y1);
swap(x2, y2);
}
skal.pb(x1);
skal.pb(x2);
skal.pb(y1);
skal.pb(y2);
rep(i, 2) for(auto j : P[i]) {
skal.pb(j.st);
skal.pb(j.nd.st);
skal.pb(j.nd.nd);
}
sort(all(skal));
for(auto i : skal) if(mp.find(i)==mp.end()) mp[i]=akt++;
x1=mp[x1]; x2=mp[x2]; y1=mp[y1]; y2=mp[y2];
rep(i, 2) rep(j, P[i].size()) P[i][j]={mp[P[i][j].st], {mp[P[i][j].nd.st], mp[P[i][j].nd.nd]}};
while(N<akt) N*=2;
rep(i, 2) {
rep(j, P[i].size()) {
upd(P[i][j].nd.st, P[i][j].nd.nd, i, {P[i][j].st, j});
}
}
rep(i, 2) rep(j, P[i].size()) odl[j][i]=INF;
queue<pair<int,int>>q;
rep(i, P[0].size()) if(P[0][i].st==y1 && P[0][i].nd.st<=x1 && x1<=P[0][i].nd.nd) {
odl[i][0]=1;
q.push({i, 0});
}
rep(i, P[1].size()) if(P[1][i].st==x1 && P[1][i].nd.st<=y1 && y1<=P[1][i].nd.nd) {
odl[i][1]=1;
q.push({i, 1});
}
while(!q.empty()) {
int a=q.front().st, b=q.front().nd; q.pop();
int x=P[b][a].st+N;
while(x>0) {
while(!tr[x][b^1].empty()) {
auto it=tr[x][b^1].lower_bound({P[b][a].nd.st, -INF});
if(it==tr[x][b^1].end()) break;
auto c=*it;
if(c.st>P[b][a].nd.nd) break;
tr[x][b^1].erase(c);
if(odl[c.nd][b^1]==INF) {
odl[c.nd][b^1]=odl[a][b]+1;
q.push({c.nd, b^1});
}
}
x/=2;
}
}
int ans=INF;
rep(i, P[0].size()) if(P[0][i].st==y2 && P[0][i].nd.st<=x2 && x2<=P[0][i].nd.nd) ans=min(ans, odl[i][0]);
rep(i, P[1].size()) if(P[1][i].st==x2 && P[1][i].nd.st<=y2 && y2<=P[1][i].nd.nd) ans=min(ans, odl[i][1]);
cout << ans << '\n';
}
Compilation message
golf.cpp: In function 'int main()':
golf.cpp:5:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
5 | #define rep(a, b) for(int a = 0; a < (b); ++a)
| ^
golf.cpp:81:13: note: in expansion of macro 'rep'
81 | rep(i, 2) rep(j, P[i].size()) P[i][j]={mp[P[i][j].st], {mp[P[i][j].nd.st], mp[P[i][j].nd.nd]}};
| ^~~
golf.cpp:5:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
5 | #define rep(a, b) for(int a = 0; a < (b); ++a)
| ^
golf.cpp:84:5: note: in expansion of macro 'rep'
84 | rep(j, P[i].size()) {
| ^~~
golf.cpp:5:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
5 | #define rep(a, b) for(int a = 0; a < (b); ++a)
| ^
golf.cpp:88:13: note: in expansion of macro 'rep'
88 | rep(i, 2) rep(j, P[i].size()) odl[j][i]=INF;
| ^~~
golf.cpp:5:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
5 | #define rep(a, b) for(int a = 0; a < (b); ++a)
| ^
golf.cpp:90:3: note: in expansion of macro 'rep'
90 | rep(i, P[0].size()) if(P[0][i].st==y1 && P[0][i].nd.st<=x1 && x1<=P[0][i].nd.nd) {
| ^~~
golf.cpp:5:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
5 | #define rep(a, b) for(int a = 0; a < (b); ++a)
| ^
golf.cpp:94:3: note: in expansion of macro 'rep'
94 | rep(i, P[1].size()) if(P[1][i].st==x1 && P[1][i].nd.st<=y1 && y1<=P[1][i].nd.nd) {
| ^~~
golf.cpp:5:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
5 | #define rep(a, b) for(int a = 0; a < (b); ++a)
| ^
golf.cpp:117:3: note: in expansion of macro 'rep'
117 | rep(i, P[0].size()) if(P[0][i].st==y2 && P[0][i].nd.st<=x2 && x2<=P[0][i].nd.nd) ans=min(ans, odl[i][0]);
| ^~~
golf.cpp:5:36: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<int, std::pair<int, int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
5 | #define rep(a, b) for(int a = 0; a < (b); ++a)
| ^
golf.cpp:118:3: note: in expansion of macro 'rep'
118 | rep(i, P[1].size()) if(P[1][i].st==x2 && P[1][i].nd.st<=y2 && y2<=P[1][i].nd.nd) ans=min(ans, odl[i][1]);
| ^~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
94 ms |
197200 KB |
Output is correct |
2 |
Correct |
74 ms |
197460 KB |
Output is correct |
3 |
Correct |
73 ms |
197460 KB |
Output is correct |
4 |
Correct |
74 ms |
197608 KB |
Output is correct |
5 |
Correct |
84 ms |
198984 KB |
Output is correct |
6 |
Correct |
80 ms |
198984 KB |
Output is correct |
7 |
Correct |
89 ms |
198988 KB |
Output is correct |
8 |
Correct |
88 ms |
198984 KB |
Output is correct |
9 |
Correct |
87 ms |
199028 KB |
Output is correct |
10 |
Correct |
87 ms |
198988 KB |
Output is correct |
11 |
Correct |
83 ms |
198980 KB |
Output is correct |
12 |
Correct |
82 ms |
198952 KB |
Output is correct |
13 |
Correct |
79 ms |
198984 KB |
Output is correct |
14 |
Correct |
85 ms |
198984 KB |
Output is correct |
15 |
Correct |
72 ms |
198152 KB |
Output is correct |
16 |
Correct |
74 ms |
199240 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
94 ms |
197200 KB |
Output is correct |
2 |
Correct |
74 ms |
197460 KB |
Output is correct |
3 |
Correct |
73 ms |
197460 KB |
Output is correct |
4 |
Correct |
74 ms |
197608 KB |
Output is correct |
5 |
Correct |
84 ms |
198984 KB |
Output is correct |
6 |
Correct |
80 ms |
198984 KB |
Output is correct |
7 |
Correct |
89 ms |
198988 KB |
Output is correct |
8 |
Correct |
88 ms |
198984 KB |
Output is correct |
9 |
Correct |
87 ms |
199028 KB |
Output is correct |
10 |
Correct |
87 ms |
198988 KB |
Output is correct |
11 |
Correct |
83 ms |
198980 KB |
Output is correct |
12 |
Correct |
82 ms |
198952 KB |
Output is correct |
13 |
Correct |
79 ms |
198984 KB |
Output is correct |
14 |
Correct |
85 ms |
198984 KB |
Output is correct |
15 |
Correct |
72 ms |
198152 KB |
Output is correct |
16 |
Correct |
74 ms |
199240 KB |
Output is correct |
17 |
Correct |
86 ms |
199408 KB |
Output is correct |
18 |
Correct |
90 ms |
199752 KB |
Output is correct |
19 |
Correct |
95 ms |
199496 KB |
Output is correct |
20 |
Correct |
84 ms |
199504 KB |
Output is correct |
21 |
Correct |
109 ms |
199524 KB |
Output is correct |
22 |
Correct |
109 ms |
199592 KB |
Output is correct |
23 |
Correct |
106 ms |
199500 KB |
Output is correct |
24 |
Correct |
89 ms |
199496 KB |
Output is correct |
25 |
Correct |
111 ms |
199572 KB |
Output is correct |
26 |
Correct |
92 ms |
201544 KB |
Output is correct |
27 |
Correct |
81 ms |
198296 KB |
Output is correct |
28 |
Correct |
78 ms |
199256 KB |
Output is correct |
29 |
Correct |
81 ms |
199496 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
94 ms |
197200 KB |
Output is correct |
2 |
Correct |
74 ms |
197460 KB |
Output is correct |
3 |
Correct |
73 ms |
197460 KB |
Output is correct |
4 |
Correct |
74 ms |
197608 KB |
Output is correct |
5 |
Correct |
84 ms |
198984 KB |
Output is correct |
6 |
Correct |
80 ms |
198984 KB |
Output is correct |
7 |
Correct |
89 ms |
198988 KB |
Output is correct |
8 |
Correct |
88 ms |
198984 KB |
Output is correct |
9 |
Correct |
87 ms |
199028 KB |
Output is correct |
10 |
Correct |
87 ms |
198988 KB |
Output is correct |
11 |
Correct |
83 ms |
198980 KB |
Output is correct |
12 |
Correct |
82 ms |
198952 KB |
Output is correct |
13 |
Correct |
79 ms |
198984 KB |
Output is correct |
14 |
Correct |
85 ms |
198984 KB |
Output is correct |
15 |
Correct |
72 ms |
198152 KB |
Output is correct |
16 |
Correct |
74 ms |
199240 KB |
Output is correct |
17 |
Correct |
86 ms |
199408 KB |
Output is correct |
18 |
Correct |
90 ms |
199752 KB |
Output is correct |
19 |
Correct |
95 ms |
199496 KB |
Output is correct |
20 |
Correct |
84 ms |
199504 KB |
Output is correct |
21 |
Correct |
109 ms |
199524 KB |
Output is correct |
22 |
Correct |
109 ms |
199592 KB |
Output is correct |
23 |
Correct |
106 ms |
199500 KB |
Output is correct |
24 |
Correct |
89 ms |
199496 KB |
Output is correct |
25 |
Correct |
111 ms |
199572 KB |
Output is correct |
26 |
Correct |
92 ms |
201544 KB |
Output is correct |
27 |
Correct |
81 ms |
198296 KB |
Output is correct |
28 |
Correct |
78 ms |
199256 KB |
Output is correct |
29 |
Correct |
81 ms |
199496 KB |
Output is correct |
30 |
Correct |
3333 ms |
476572 KB |
Output is correct |
31 |
Correct |
3272 ms |
476572 KB |
Output is correct |
32 |
Correct |
3375 ms |
479672 KB |
Output is correct |
33 |
Correct |
3190 ms |
478540 KB |
Output is correct |
34 |
Correct |
3321 ms |
481944 KB |
Output is correct |
35 |
Correct |
3297 ms |
481352 KB |
Output is correct |
36 |
Correct |
3301 ms |
481176 KB |
Output is correct |
37 |
Correct |
3428 ms |
483848 KB |
Output is correct |
38 |
Correct |
3495 ms |
481496 KB |
Output is correct |
39 |
Correct |
3309 ms |
481808 KB |
Output is correct |
40 |
Correct |
3226 ms |
771976 KB |
Output is correct |
41 |
Correct |
3536 ms |
808852 KB |
Output is correct |
42 |
Correct |
3007 ms |
721992 KB |
Output is correct |
43 |
Correct |
2975 ms |
722048 KB |
Output is correct |
44 |
Correct |
2987 ms |
746076 KB |
Output is correct |
45 |
Correct |
3491 ms |
764808 KB |
Output is correct |
46 |
Correct |
2937 ms |
727404 KB |
Output is correct |
47 |
Correct |
3319 ms |
783808 KB |
Output is correct |
48 |
Correct |
3291 ms |
727304 KB |
Output is correct |
49 |
Correct |
3562 ms |
765024 KB |
Output is correct |
50 |
Correct |
86 ms |
199244 KB |
Output is correct |
51 |
Correct |
92 ms |
199252 KB |
Output is correct |
52 |
Correct |
95 ms |
199424 KB |
Output is correct |