<pre name="code" class="cpp">#include <bits/stdc++.h> using namespace std; #define maxn 505 typedef pair<int, int> P; int W, H, N; int X1[maxn], X2[maxn], Y1[maxn], Y2[maxn]; bool fld[maxn][maxn]; int d[2][4] = {{-1, 0, 1, 0}, {0, -1, 0, 1}}; 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 xx1 = x1[i] + d; int xx2 = x2[i] + d; if(xx1>=1 && xx1<=w) xs.push_back(xx1); if(xx2>=1 && xx2<=w) xs.push_back(xx2); } 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(); } void solve() { memset(fld, false, sizeof(fld)); W = compress(X1, X2, W); H = compress(Y1, Y2, H); 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++) fld[y][x] = true; for(int y=0; y<H; y++) { for(int x=0; x<W; x++) cout<<fld[y][x]<<" "; cout<<endl; } int cnt = 0; for(int y=0; y<H; y++) for(int x=0; x<W; x++) if(!fld[y][x]) { cnt++; queue<P> que; que.push(make_pair(y, x)); while(!que.empty()) { P p = que.front(); que.pop(); for(int i=0; i<4; i++) { int ty = p.first + d[0][i]; int tx = p.second + d[1][i]; while(tx>=0 && tx<W && ty>=0 && ty<H && !fld[ty][tx]) { fld[ty][tx] = true; que.push(make_pair(ty, tx)); } } } } cout<<cnt<<endl; } int main() { while(~scanf("%d%d", &W, &H) && W + H) { cin>>N; for(int i=0; i<N; i++) scanf("%d", &X1[i]); for(int i=0; i<N; i++) scanf("%d", &X2[i]); for(int i=0; i<N; i++) scanf("%d", &Y1[i]); for(int i=0; i<N; i++) scanf("%d", &Y2[i]); solve(); } return 0; } /* 10 10 5 1 1 4 9 10 6 10 4 9 10 4 8 1 1 6 4 8 10 5 10 */