#include <bits/stdc++.h>
using namespace std;
typedef long long lint;
typedef long double llf;
typedef pair<int, int> pi;
const int MAXN = 200005;
const int pool = 500000;
int n, sx, sy, V;
pi s, e;
struct rect{ int sx, ex, sy, ey; }a[100005];
struct seg { int x, s, e, idx; };
vector<seg> segx, segy;
vector<int> gph0[pool];
vector<int> gph1[pool];
int dis[pool];
bool vis[pool];
vector<pi> addE[MAXN];
int val[MAXN];
void solve(vector<seg> l, vector<seg> r){
memset(val, -1, sizeof(val));
for(int i=0; i<MAXN; i++) addE[i].clear();
for(auto &i : l){
addE[i.s].push_back(pi(i.x, i.idx));
addE[i.e + 1].push_back(pi(i.x, -1));
}
int ptr = 0;
for(int i=0; i<MAXN; i++){
for(auto &j : addE[i]){
val[j.first] = j.second;
}
while(ptr < r.size() && r[ptr].x == i){
for(int j=r[ptr].s; j<=r[ptr].e; j++){
if(val[j] == -1) continue;
gph1[r[ptr].idx].push_back(val[j]);
}
ptr++;
}
}
}
vector<pi> ins[MAXN], del[MAXN];
int piv;
vector<seg> sweep(vector<pi> &v){
for(int i=0; i<MAXN; i++) ins[i].clear(), del[i].clear();
for(int i=0; i<n; i++){
if(a[i].sx + 1 < a[i].ex){
ins[a[i].sx + 1].push_back(pi(a[i].sy, a[i].ey));
del[a[i].ex].push_back(pi(a[i].sy, a[i].ey));
}
}
set<pi> intv;
int ptr = 0;
vector<seg> ret;
for(int i=0; i<MAXN; i++){
for(auto &j : ins[i]) intv.insert(j);
for(auto &j : del[i]) intv.erase(j);
while(ptr < v.size() && v[ptr].first == i){
auto l = intv.lower_bound(pi(v[ptr].second, -1));
int re = (l == intv.end() ? (2*n+2) : l->first);
int rs = (l == intv.begin() ? 0 : prev(l)->second);
ret.push_back({i, rs, re, piv++});
ptr++;
}
}
return ret;
}
int main(){
cin >> s.first >> s.second >> e.first >> e.second >> n;
vector<int> vx = {s.first, e.first}, vy = {s.second, e.second};
for(int i=0; i<n; i++){
scanf("%d %d %d %d",&a[i].sx,&a[i].ex,&a[i].sy,&a[i].ey);
vx.push_back(a[i].sx);
vx.push_back(a[i].ex);
vy.push_back(a[i].sy);
vy.push_back(a[i].ey);
}
sort(vx.begin(), vx.end());
vx.resize(unique(vx.begin(), vx.end()) - vx.begin());
sort(vy.begin(), vy.end());
vy.resize(unique(vy.begin(), vy.end()) - vy.begin());
auto getloc = [&](int x, vector<int> &v){
return lower_bound(v.begin(), v.end(), x) - v.begin();
};
s.first = getloc(s.first, vx);
s.second = getloc(s.second, vy);
e.first = getloc(e.first, vx);
e.second = getloc(e.second, vy);
for(int i=0; i<n; i++){
a[i].sx = getloc(a[i].sx, vx);
a[i].ex = getloc(a[i].ex, vx);
a[i].sy = getloc(a[i].sy, vy);
a[i].ey = getloc(a[i].ey, vy);
}
sx = vx.size();
sy = vy.size();
vector<pi> ptr;
ptr.push_back(s);
ptr.push_back(e);
for(int i=0; i<n; i++){
ptr.emplace_back(a[i].sx, a[i].sy);
ptr.emplace_back(a[i].ex, a[i].ey);
}
ptr.emplace_back(0, 0);
ptr.emplace_back(sx-1, sy-1);
sort(ptr.begin(), ptr.end());
ptr.resize(unique(ptr.begin(), ptr.end()) - ptr.begin());
segy = sweep(ptr);
for(int i=0; i<n; i++){
swap(a[i].sx, a[i].sy);
swap(a[i].ex, a[i].ey);
}
for(auto &i : ptr) swap(i.first, i.second);
sort(ptr.begin(), ptr.end());
segx = sweep(ptr);
sort(segx.begin(), segx.end(), [&](const seg &a, const seg &b){
return a.x < b.x;
});
sort(segy.begin(), segy.end(), [&](const seg &a, const seg &b){
return a.x < b.x;
});
solve(segx, segy);
solve(segy, segx);
deque<int> que;
memset(dis, 0x3f, sizeof(dis));
for(auto &i : segx){
if(i.x == s.second && i.s <= s.first && s.first <= i.e){
dis[i.idx] = 0;
que.push_back(i.idx);
}
}
for(auto &i : segy){
if(i.x == s.first && i.s <= s.second && s.second <= i.e){
dis[i.idx] = 0;
que.push_back(i.idx);
}
}
while(!que.empty()){
int x = que.front();
que.pop_front();
if(vis[x]) continue;
vis[x] = 1;
for(auto &i : gph0[x]){
if(dis[i] > dis[x]){
dis[i] = dis[x];
que.push_front(i);
}
}
for(auto &i : gph1[x]){
if(dis[i] > dis[x] + 1){
dis[i] = dis[x] + 1;
que.push_back(i);
}
}
}
int ret = 1e9;
for(auto &i : segx){
if(i.x == e.second && i.s <= e.first && e.first <= i.e){
ret = min(ret, dis[i.idx]);
}
}
for(auto &i : segy){
if(i.x == e.first && i.s <= e.second && e.second <= i.e){
ret = min(ret, dis[i.idx]);
}
}
cout << ret + 1 << endl;
}
Compilation message
golf.cpp: In function 'void solve(std::vector<seg>, std::vector<seg>)':
golf.cpp:34:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(ptr < r.size() && r[ptr].x == i){
~~~~^~~~~~~~~~
golf.cpp: In function 'std::vector<seg> sweep(std::vector<std::pair<int, int> >&)':
golf.cpp:61:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while(ptr < v.size() && v[ptr].first == i){
~~~~^~~~~~~~~~
golf.cpp: In function 'int main()':
golf.cpp:76:8: warning: ignoring return value of 'int scanf(const char*, ...)', declared with attribute warn_unused_result [-Wunused-result]
scanf("%d %d %d %d",&a[i].sx,&a[i].ex,&a[i].sy,&a[i].ey);
~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
40568 KB |
Output is correct |
2 |
Correct |
43 ms |
40696 KB |
Output is correct |
3 |
Correct |
43 ms |
40576 KB |
Output is correct |
4 |
Correct |
39 ms |
40680 KB |
Output is correct |
5 |
Correct |
43 ms |
41352 KB |
Output is correct |
6 |
Correct |
54 ms |
41336 KB |
Output is correct |
7 |
Correct |
43 ms |
41336 KB |
Output is correct |
8 |
Correct |
41 ms |
41344 KB |
Output is correct |
9 |
Correct |
57 ms |
41344 KB |
Output is correct |
10 |
Correct |
46 ms |
41336 KB |
Output is correct |
11 |
Correct |
42 ms |
41336 KB |
Output is correct |
12 |
Correct |
59 ms |
41404 KB |
Output is correct |
13 |
Correct |
46 ms |
41336 KB |
Output is correct |
14 |
Correct |
40 ms |
41344 KB |
Output is correct |
15 |
Correct |
34 ms |
40832 KB |
Output is correct |
16 |
Correct |
45 ms |
41080 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
40568 KB |
Output is correct |
2 |
Correct |
43 ms |
40696 KB |
Output is correct |
3 |
Correct |
43 ms |
40576 KB |
Output is correct |
4 |
Correct |
39 ms |
40680 KB |
Output is correct |
5 |
Correct |
43 ms |
41352 KB |
Output is correct |
6 |
Correct |
54 ms |
41336 KB |
Output is correct |
7 |
Correct |
43 ms |
41336 KB |
Output is correct |
8 |
Correct |
41 ms |
41344 KB |
Output is correct |
9 |
Correct |
57 ms |
41344 KB |
Output is correct |
10 |
Correct |
46 ms |
41336 KB |
Output is correct |
11 |
Correct |
42 ms |
41336 KB |
Output is correct |
12 |
Correct |
59 ms |
41404 KB |
Output is correct |
13 |
Correct |
46 ms |
41336 KB |
Output is correct |
14 |
Correct |
40 ms |
41344 KB |
Output is correct |
15 |
Correct |
34 ms |
40832 KB |
Output is correct |
16 |
Correct |
45 ms |
41080 KB |
Output is correct |
17 |
Correct |
41 ms |
41464 KB |
Output is correct |
18 |
Correct |
45 ms |
41440 KB |
Output is correct |
19 |
Correct |
55 ms |
41336 KB |
Output is correct |
20 |
Correct |
54 ms |
41468 KB |
Output is correct |
21 |
Correct |
43 ms |
41336 KB |
Output is correct |
22 |
Correct |
42 ms |
41464 KB |
Output is correct |
23 |
Correct |
59 ms |
41440 KB |
Output is correct |
24 |
Correct |
66 ms |
41336 KB |
Output is correct |
25 |
Correct |
40 ms |
41472 KB |
Output is correct |
26 |
Correct |
42 ms |
41464 KB |
Output is correct |
27 |
Correct |
36 ms |
40832 KB |
Output is correct |
28 |
Correct |
42 ms |
41088 KB |
Output is correct |
29 |
Correct |
54 ms |
41080 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
38 ms |
40568 KB |
Output is correct |
2 |
Correct |
43 ms |
40696 KB |
Output is correct |
3 |
Correct |
43 ms |
40576 KB |
Output is correct |
4 |
Correct |
39 ms |
40680 KB |
Output is correct |
5 |
Correct |
43 ms |
41352 KB |
Output is correct |
6 |
Correct |
54 ms |
41336 KB |
Output is correct |
7 |
Correct |
43 ms |
41336 KB |
Output is correct |
8 |
Correct |
41 ms |
41344 KB |
Output is correct |
9 |
Correct |
57 ms |
41344 KB |
Output is correct |
10 |
Correct |
46 ms |
41336 KB |
Output is correct |
11 |
Correct |
42 ms |
41336 KB |
Output is correct |
12 |
Correct |
59 ms |
41404 KB |
Output is correct |
13 |
Correct |
46 ms |
41336 KB |
Output is correct |
14 |
Correct |
40 ms |
41344 KB |
Output is correct |
15 |
Correct |
34 ms |
40832 KB |
Output is correct |
16 |
Correct |
45 ms |
41080 KB |
Output is correct |
17 |
Correct |
41 ms |
41464 KB |
Output is correct |
18 |
Correct |
45 ms |
41440 KB |
Output is correct |
19 |
Correct |
55 ms |
41336 KB |
Output is correct |
20 |
Correct |
54 ms |
41468 KB |
Output is correct |
21 |
Correct |
43 ms |
41336 KB |
Output is correct |
22 |
Correct |
42 ms |
41464 KB |
Output is correct |
23 |
Correct |
59 ms |
41440 KB |
Output is correct |
24 |
Correct |
66 ms |
41336 KB |
Output is correct |
25 |
Correct |
40 ms |
41472 KB |
Output is correct |
26 |
Correct |
42 ms |
41464 KB |
Output is correct |
27 |
Correct |
36 ms |
40832 KB |
Output is correct |
28 |
Correct |
42 ms |
41088 KB |
Output is correct |
29 |
Correct |
54 ms |
41080 KB |
Output is correct |
30 |
Correct |
2206 ms |
119920 KB |
Output is correct |
31 |
Correct |
1855 ms |
119500 KB |
Output is correct |
32 |
Correct |
3231 ms |
121928 KB |
Output is correct |
33 |
Correct |
2520 ms |
121060 KB |
Output is correct |
34 |
Correct |
1737 ms |
120128 KB |
Output is correct |
35 |
Correct |
1991 ms |
121288 KB |
Output is correct |
36 |
Correct |
2532 ms |
122288 KB |
Output is correct |
37 |
Correct |
3574 ms |
123572 KB |
Output is correct |
38 |
Correct |
2055 ms |
121368 KB |
Output is correct |
39 |
Correct |
3429 ms |
123284 KB |
Output is correct |
40 |
Runtime error |
2129 ms |
1048580 KB |
Execution killed with signal 9 (could be triggered by violating memory limits) |
41 |
Halted |
0 ms |
0 KB |
- |