#include <bits/stdc++.h>
#define eb emplace_back
#define sz(V) ((int)(V).size())
#define allv(V) ((V).begin()),((V).end())
#define sorv(V) sort(allv(V))
#define univ(V) (V).erase(unique(allv(V)),(V).end())
#define INF (0x3f3f3f3f)
using namespace std;
typedef pair<int, int> pii;
const int MAXN = 100055;
const int MX = 262144;
struct LNE {
LNE(int x, int sy, int ey, bool isf = false)
: x(x), sy(sy), ey(ey), isf(isf) {}
int x, sy, ey;
bool isf;
bool operator < (const LNE &t) const {
return x < t.x;
}
bool operator == (const LNE &t) const {
return x == t.x && sy == t.sy && ey == t.ey;
}
};
struct SEG {
set<pii> d[MX*2];
void push(int x, int y, int i) { d[x].insert({y, i}); }
void push(int s, int e, int y, int i) {
for(s += MX, e += MX; s <= e; s >>= 1, e >>= 1) {
if(s&1) push(s++, y, i);
if(~e&1) push(e--, y, i);
}
}
void pop(int x, int y, int i) { d[x].erase({y, i}); }
void pop(int s, int e, int y, int i) {
for(s += MX, e += MX; s <= e; s >>= 1, e >>= 1) {
if(s&1) pop(s++, y, i);
if(~e&1) pop(e--, y, i);
}
}
int get(int x, int s, int e) {
for(x += MX; x; x >>= 1) {
auto it = d[x].lower_bound({s, -1});
if(d[x].end() != it && it->first <= e)
return it->second;
}
return -1;
}
} segX, segY;
vector<LNE> LXV, LYV;
int DX[MAXN*2], DY[MAXN*2];
int SX[MAXN], SY[MAXN], EX[MAXN], EY[MAXN];
int PSX, PSY, PEX, PEY;
int SXI, SYI, EXI, EYI;
int N;
void compress() {
vector<int> V;
auto f = [&](int x) { return int(lower_bound(allv(V), x) - V.begin()) + 1; };
V.eb(PSX); V.eb(PEX);
for(int i = 1; i <= N; i++) {
V.eb(SX[i]);
V.eb(EX[i]);
}
sorv(V); univ(V);
PSX = f(PSX); PEX = f(PEX);
for(int i = 1; i <= N; i++) {
SX[i] = f(SX[i]);
EX[i] = f(EX[i]);
}
V.clear(); V.eb(PSY); V.eb(PEY);
for(int i = 1; i <= N; i++) {
V.eb(SY[i]);
V.eb(EY[i]);
}
sorv(V); univ(V);
PSY = f(PSY); PEY = f(PEY);
for(int i = 1; i <= N; i++) {
SY[i] = f(SY[i]);
EY[i] = f(EY[i]);
}
}
void makeLines(vector<LNE> &EV, vector<LNE> &LV) {
set<int> CH;
sorv(EV);
CH.insert(0); CH.insert((N+1)<<1|1);
for(int s = 0, e, x, n = sz(EV); s < n; s = e) {
x = EV[s].x;
for(e = s+1; e < n && x == EV[e].x; e++);
for(int i = s; i < e; i++) if(EV[i].sy != EV[i].ey) {
CH.erase(EV[i].sy);
CH.erase(EV[i].ey);
}
for(int i = s; i < e; i++) {
auto it = CH.lower_bound(EV[i].sy);
LV.eb(x, *prev(it), *it);
}
for(int i = s; i < e; i++) if(EV[i].isf) {
CH.insert(EV[i].sy);
CH.insert(EV[i].ey);
}
}
sorv(LV); univ(LV);
}
void makeLines() {
vector<LNE> V;
V.eb(PSX, PSY, PSY); V.eb(PEX, PEY, PEY);
for(int i = 1; i <= N; i++) {
V.eb(SX[i], SY[i], EY[i], true);
V.eb(EX[i], SY[i], EY[i]);
}
makeLines(V, LYV);
V.clear();
V.eb(PSY, PSX, PSX); V.eb(PEY, PEX, PEX);
for(int i = 1; i <= N; i++) {
V.eb(SY[i], SX[i], EX[i], true);
V.eb(EY[i], SX[i], EX[i]);
}
makeLines(V, LXV);
}
void pushLX(int i) { segX.push(LXV[i].sy, LXV[i].ey, LXV[i].x, i); }
void pushLY(int i) { segY.push(LYV[i].sy, LYV[i].ey, LYV[i].x, i); }
void popLX(int i) { segX.pop(LXV[i].sy, LXV[i].ey, LXV[i].x, i); }
void popLY(int i) { segY.pop(LYV[i].sy, LYV[i].ey, LYV[i].x, i); }
int getAns() {
fill(DX, DX+MAXN*2, INF);
fill(DY, DY+MAXN*2, INF);
queue<int> PQ;
DX[SXI] = 1; DY[SYI] = 1;
PQ.push(SXI<<1);
PQ.push(SYI<<1|1);
popLX(SXI); popLY(SYI);
for(int idx, dst; !PQ.empty();) {
idx = PQ.front(); PQ.pop();
if(idx&1) {
idx >>= 1;
dst = DY[idx] + 1;
for(int v;;) {
v = segX.get(LYV[idx].x, LYV[idx].sy, LYV[idx].ey);
if(v < 0) break;
DX[v] = dst;
popLX(v);
PQ.push(v<<1);
}
} else {
idx >>= 1;
dst = DX[idx] + 1;
for(int v;;) {
v = segY.get(LXV[idx].x, LXV[idx].sy, LXV[idx].ey);
if(v < 0) break;
DY[v] = dst;
popLY(v);
PQ.push(v<<1|1);
}
}
}
return min(DX[EXI], DY[EYI]);
}
int main() {
ios::sync_with_stdio(false);
cin >> PSX >> PSY >> PEX >> PEY >> N;
for(int i = 1; i <= N; i++)
cin >> SX[i] >> EX[i] >> SY[i] >> EY[i];
compress();
makeLines();
SXI = EXI = SYI = EYI = -1;
for(int i = sz(LXV); i--;) {
pushLX(i);
if(LXV[i].sy <= PSX && PSX <= LXV[i].ey && LXV[i].x == PSY) SXI = i;
if(LXV[i].sy <= PEX && PEX <= LXV[i].ey && LXV[i].x == PEY) EXI = i;
}
for(int i = sz(LYV); i--;) {
pushLY(i);
if(LYV[i].x == PSX && LYV[i].sy <= PSY && PSY <= LYV[i].ey) SYI = i;
if(LYV[i].x == PEX && LYV[i].sy <= PEY && PEY <= LYV[i].ey) EYI = i;
}
cout << getAns() << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
51200 KB |
Output is correct |
2 |
Correct |
40 ms |
51192 KB |
Output is correct |
3 |
Correct |
39 ms |
51200 KB |
Output is correct |
4 |
Correct |
39 ms |
51320 KB |
Output is correct |
5 |
Correct |
48 ms |
52140 KB |
Output is correct |
6 |
Correct |
51 ms |
52096 KB |
Output is correct |
7 |
Correct |
50 ms |
52096 KB |
Output is correct |
8 |
Correct |
52 ms |
52092 KB |
Output is correct |
9 |
Correct |
56 ms |
52088 KB |
Output is correct |
10 |
Correct |
58 ms |
52272 KB |
Output is correct |
11 |
Correct |
44 ms |
52200 KB |
Output is correct |
12 |
Correct |
52 ms |
52088 KB |
Output is correct |
13 |
Correct |
43 ms |
52096 KB |
Output is correct |
14 |
Correct |
48 ms |
52088 KB |
Output is correct |
15 |
Correct |
41 ms |
51448 KB |
Output is correct |
16 |
Correct |
41 ms |
51840 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
51200 KB |
Output is correct |
2 |
Correct |
40 ms |
51192 KB |
Output is correct |
3 |
Correct |
39 ms |
51200 KB |
Output is correct |
4 |
Correct |
39 ms |
51320 KB |
Output is correct |
5 |
Correct |
48 ms |
52140 KB |
Output is correct |
6 |
Correct |
51 ms |
52096 KB |
Output is correct |
7 |
Correct |
50 ms |
52096 KB |
Output is correct |
8 |
Correct |
52 ms |
52092 KB |
Output is correct |
9 |
Correct |
56 ms |
52088 KB |
Output is correct |
10 |
Correct |
58 ms |
52272 KB |
Output is correct |
11 |
Correct |
44 ms |
52200 KB |
Output is correct |
12 |
Correct |
52 ms |
52088 KB |
Output is correct |
13 |
Correct |
43 ms |
52096 KB |
Output is correct |
14 |
Correct |
48 ms |
52088 KB |
Output is correct |
15 |
Correct |
41 ms |
51448 KB |
Output is correct |
16 |
Correct |
41 ms |
51840 KB |
Output is correct |
17 |
Correct |
45 ms |
52476 KB |
Output is correct |
18 |
Correct |
47 ms |
52480 KB |
Output is correct |
19 |
Correct |
59 ms |
52472 KB |
Output is correct |
20 |
Correct |
50 ms |
52476 KB |
Output is correct |
21 |
Correct |
46 ms |
52472 KB |
Output is correct |
22 |
Correct |
50 ms |
52600 KB |
Output is correct |
23 |
Correct |
55 ms |
52472 KB |
Output is correct |
24 |
Correct |
50 ms |
52480 KB |
Output is correct |
25 |
Correct |
54 ms |
52476 KB |
Output is correct |
26 |
Correct |
47 ms |
52456 KB |
Output is correct |
27 |
Correct |
50 ms |
51600 KB |
Output is correct |
28 |
Correct |
41 ms |
51936 KB |
Output is correct |
29 |
Correct |
47 ms |
51960 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
34 ms |
51200 KB |
Output is correct |
2 |
Correct |
40 ms |
51192 KB |
Output is correct |
3 |
Correct |
39 ms |
51200 KB |
Output is correct |
4 |
Correct |
39 ms |
51320 KB |
Output is correct |
5 |
Correct |
48 ms |
52140 KB |
Output is correct |
6 |
Correct |
51 ms |
52096 KB |
Output is correct |
7 |
Correct |
50 ms |
52096 KB |
Output is correct |
8 |
Correct |
52 ms |
52092 KB |
Output is correct |
9 |
Correct |
56 ms |
52088 KB |
Output is correct |
10 |
Correct |
58 ms |
52272 KB |
Output is correct |
11 |
Correct |
44 ms |
52200 KB |
Output is correct |
12 |
Correct |
52 ms |
52088 KB |
Output is correct |
13 |
Correct |
43 ms |
52096 KB |
Output is correct |
14 |
Correct |
48 ms |
52088 KB |
Output is correct |
15 |
Correct |
41 ms |
51448 KB |
Output is correct |
16 |
Correct |
41 ms |
51840 KB |
Output is correct |
17 |
Correct |
45 ms |
52476 KB |
Output is correct |
18 |
Correct |
47 ms |
52480 KB |
Output is correct |
19 |
Correct |
59 ms |
52472 KB |
Output is correct |
20 |
Correct |
50 ms |
52476 KB |
Output is correct |
21 |
Correct |
46 ms |
52472 KB |
Output is correct |
22 |
Correct |
50 ms |
52600 KB |
Output is correct |
23 |
Correct |
55 ms |
52472 KB |
Output is correct |
24 |
Correct |
50 ms |
52480 KB |
Output is correct |
25 |
Correct |
54 ms |
52476 KB |
Output is correct |
26 |
Correct |
47 ms |
52456 KB |
Output is correct |
27 |
Correct |
50 ms |
51600 KB |
Output is correct |
28 |
Correct |
41 ms |
51936 KB |
Output is correct |
29 |
Correct |
47 ms |
51960 KB |
Output is correct |
30 |
Correct |
3488 ms |
243912 KB |
Output is correct |
31 |
Correct |
4089 ms |
244208 KB |
Output is correct |
32 |
Correct |
3757 ms |
245344 KB |
Output is correct |
33 |
Correct |
4310 ms |
245636 KB |
Output is correct |
34 |
Correct |
3778 ms |
247996 KB |
Output is correct |
35 |
Correct |
3676 ms |
247432 KB |
Output is correct |
36 |
Correct |
3733 ms |
247276 KB |
Output is correct |
37 |
Correct |
4090 ms |
246484 KB |
Output is correct |
38 |
Correct |
3458 ms |
247880 KB |
Output is correct |
39 |
Correct |
3560 ms |
246416 KB |
Output is correct |
40 |
Correct |
2395 ms |
261340 KB |
Output is correct |
41 |
Correct |
1785 ms |
261508 KB |
Output is correct |
42 |
Correct |
1518 ms |
207868 KB |
Output is correct |
43 |
Correct |
1488 ms |
207768 KB |
Output is correct |
44 |
Correct |
1281 ms |
190684 KB |
Output is correct |
45 |
Correct |
1570 ms |
190572 KB |
Output is correct |
46 |
Correct |
1261 ms |
190576 KB |
Output is correct |
47 |
Correct |
1263 ms |
190556 KB |
Output is correct |
48 |
Correct |
1257 ms |
190576 KB |
Output is correct |
49 |
Correct |
1267 ms |
190500 KB |
Output is correct |
50 |
Correct |
40 ms |
51960 KB |
Output is correct |
51 |
Correct |
57 ms |
51960 KB |
Output is correct |
52 |
Correct |
85 ms |
51960 KB |
Output is correct |