Language: C++

#include <iostream>
#include <queue>
#include <map>
#include <vector>
using namespace std;

int W,H,N;
bool fld[6000][6000];
int X1[1000],X2[1000],Y1[1000],Y2[1000];
const int dx[4]={0,0,1,-1},dy[4]={1,-1,0,0};

int compress(int *x1,int *x2,int w){
	vector<int> xs;
	for(int i=0;i<N;i++){
		for(int j=-1;j<=1;j++){
			int tx1=x1[i]+j,tx2=x2[i]+j;
			if(1<=tx1 && tx1<=W)xs.push_back(tx1);
			if(1<=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();
}

void solve(){
	W=compress(X1,X2,W);
	H=compress(Y1,Y2,H);
	memset(fld,0,sizeof(fld));
	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;
			}
		}
	}
	int ans=0;
	for(int y=0;y<H;y++){
		for(int x=0;x<W;x++){
			if(fld[y][x])continue;
			ans++;

			queue<pair<int,int>> que;
			que.push(make_pair(x,y));
			while(!que.empty()){
				int sx=que.front().first,sy=que.front().second;
				que.pop();
				for(int i=0;i<4;i++){
					int tx=sx+dx[i],ty=sy+dy[i];
					if(tx<0 || W<=tx || ty<0 || H<=ty)continue;
					if(fld[ty][tx])continue;
					que.push(make_pair(tx,ty));
					fld[ty][tx]=true;
				}
			}
		}
	}
	printf("%d\n",ans);
}

int main(void){
	while(1){
		scanf("%d%d",&W,&H);
		if(W==0 && H==0)break;
		scanf("%d",&N);
		for(int i=0;i<N;i++){
			scanf("%d%d%d%d",&X1[i],&Y1[i],&X2[i],&Y2[i]);
		}
		solve();
	}
    return 0;
}

Compiler output:
a.cc: In function 'int compress(int*, int*, int)':
a.cc:21:9: error: 'sort' was not declared in this scope; did you mean 'short'?
   21 |         sort(xs.begin(),xs.end());
      |         ^~~~
      |         short
a.cc:22:18: error: 'unique' was not declared in this scope
   22 |         xs.erase(unique(xs.begin(),xs.end()),xs.end());
      |                  ^~~~~~
a.cc:24:27: error: no matching function for call to 'find(std::vector<int>::iterator, std::vector<int>::iterator, int&)'
   24 |                 x1[i]=find(xs.begin(),xs.end(),x1[i])-xs.begin();
      |                       ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/bits/locale_facets.h:48,
                 from /usr/include/c++/14/bits/basic_ios.h:37,
                 from /usr/include/c++/14/ios:46,
                 from /usr/include/c++/14/ostream:40,
                 from /usr/include/c++/14/iostream:41,
                 from a.cc:1:
/usr/include/c++/14/bits/streambuf_iterator.h:435:5: note: candidate: 'template<class _CharT2> typename __gnu_cxx::__enable_if<std::__is_char<_CharT2>::__value, std::istreambuf_iterator<_CharT> >::__type std::find(istreambuf_iterator<_CharT>, istreambuf_iterator<_CharT>, const _CharT2&)'
  435 |     find(istreambuf_iterator<_CharT> __first,
      |     ^~~~
/usr/include/c++/14/bits/streambuf_iterator.h:435:5: note:   template argument deduction/substitution failed:
a.cc:24:27: note:   '__gnu_cxx::__normal_iterator<int*, std::vector<int> >' is not derived from 'std::istreambuf_iterator<_CharT>'
   24 |                 x1[i]=find(xs.begin(),xs.end(),x1[i])-xs.begin();
      |                       ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:25:27: error: no matching function for call to 'find(std::vector<int>::iterator, std::vector<int>::iterator, int&)'
   25 |                 x2[i]=find(xs.begin(),xs.end(),x2[i])-xs.begin();
      |                       ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/streambuf_iterator.h:435:5: note: candidate: 'template<class _CharT2> typename __gnu_cxx::__enable_if<std::__is_char<_CharT2>::__value, std::istreambuf_iterator<_CharT> >::__type std::find(istreambuf_iterator<_CharT>, istreambuf_iterator<_CharT>, const _CharT2&)'
  435 |     find(istreambuf_iterator<_CharT> __first,
      |     ^~~~
/usr/include/c++/14/bits/streambuf_iterator.h:435:5: note:   template argument deduction/substitution failed:
a.cc:25:27: note:   '__gnu_cxx::__normal_iterator<int*, std::vector<int> >' is not derived from 'std::istreambuf_iterator<_CharT>'
   25 |                 x2[i]=find(xs.begin(),xs.end(),x2[i])-xs.begin();
      |                       ~~~~^~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc: In function 'void solve()':
a.cc:33:9: error: 'memset' was not declared in this scope
   33 |         memset(fld,0,sizeof(fld));
      |         ^~~~~~
a.cc:4:1: note: 'memset' is defined in header '<cstring>'; this is probably fixable by adding '#include <cstring>'
    3 | #include <map>
  +++ |+#include <cstring>
    4 | #include <vector>