-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcompress.cpp
164 lines (136 loc) · 2.89 KB
/
compress.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
#define _CRT_SECURE_NO_WARNINGS
#include <cstdio>
#include <iostream>
#include <stack>
#include <queue>
#include <algorithm>
#include <functional>
#include <set>
#include <map>
#include <string>
#include <vector>
#include <cmath>
#include<sstream>
#include<list>
#include<iomanip>
#include <cstdlib>
#include <cstring>
#include <stack>
#include <bitset>
#include <cassert>
#include <stdlib.h>
#include <stdio.h>
using namespace std;
const int INF = 100000000;
const long long LINF = 3e18 + 7;
const int MAX_N = 20010;
const int MAX_W = 10002;
const int MAX_ARRAYK = 100000;
double PI = 3.14159265358979323846;
//using ll = long long;
int W, H, N;
int X1[MAX_N], X2[MAX_N], Y1[MAX_N], Y2[MAX_N];
int dx[4] = { -1, 0, 1, 0 };
int dy[4] = { 0, -1, 0, 1 };
bool field[MAX_N][MAX_N];
// p.150 compress
//10 10 5
//1 1 4 9 10
//6 10 4 9 10
//4 8 1 1 6
//4 8 10 5 10
// X1, X2を座標圧縮し、座標圧縮した際の幅を返す
int compress(int* x1, int* x2, int w) {
vector<int> xs;
for (int i = 0; i < N; i++) {
for (int d = -1; d <= 1; d++) {
int tx1 = x1[i] + d;
int tx2 = x2[i] + d;
if (0 <= tx1 && tx1 < w) {
xs.push_back(tx1);
}
if (0 <= tx2 && tx2 < w) {
xs.push_back(tx2);
}
}
}
sort(xs.begin(), xs.end());
xs.erase(unique(xs.begin(), xs.end()), xs.end());
for (int i = 0; i < N; i++) {
x1[i] = find(xs.begin(), xs.end(), x1[i]) - xs.begin();
x2[i] = find(xs.begin(), xs.end(), x2[i]) - xs.begin();
}
return xs.size();
}
int compress(int p[]) {
vector<int> ps;
ps.resize(N);
for (int i = 0; i < N; ++i) {
ps[i] = p[i];
}
sort(ps.begin(), ps.end());
ps.erase(unique(ps.begin(), ps.end()), ps.end());
for (int i = 0; i < N; ++i) {
p[i] = 2 + (int)distance(ps.begin(), lower_bound(ps.begin(), ps.end(), p[i]));
}
return 2 + (int)ps.size();
}
int main() {
cin >> W >> H >> N;
for (int i = 0; i < N; i++) {
cin >> X1[i];
X1[i]--;
}
for (int i = 0; i < N; i++) {
cin >> X2[i];
X2[i]--;
}
for (int i = 0; i < N; i++) {
cin >> Y1[i];
Y1[i]--;
}
for (int i = 0; i < N; i++) {
cin >> Y2[i];
Y2[i]--;
}
W = compress(X1, X2, W);
H = compress(Y1, Y2, H);
for (int i = 0; i < H; i++) {
for (int j = 0; j < W; j++) {
field[i][j] = false;
}
}
for (int i = 0; i < N; i++) {
for (int y = Y1[i]; y <= Y2[i]; y++) {
for (int x = X1[i]; x <= X2[i]; x++) {
field[y][x] = true;
}
}
}
int ans = 0;
for (int y = 0; y < H; y++) {
for (int x = 0; x < W; x++) {
if (field[y][x]) {
continue;
}
ans++;
queue<pair<int, int> > que;
que.push(make_pair(y, x));
while (!que.empty()) {
int sy = que.front().first;
int sx = que.front().second;
que.pop();
for (int i = 0; i < 4; i++) {
int tx = sx + dx[i];
int ty = sy + dy[i];
if (tx >= 0 && tx < W && ty >= 0 && ty < H && !field[ty][tx]) {
que.push(make_pair(ty, tx));
field[ty][tx] = true;
}
}
}
}
}
cout << ans << endl;
return 0;
}