精确覆盖的模板题, 把每一个格子当成一列就可以。
S忘记初始化TLE N次, 哭晕在厕所......
#include<bits/stdc++.h>
using namespace std;
#define pb(x) push_back(x)
#define ll long long
#define mk(x, y) make_pair(x, y)
#define lson l, m, rt<<1
#define mem(a) memset(a, 0, sizeof(a))
#define rson m+1, r, rt<<1|1
#define mem1(a) memset(a, -1, sizeof(a))
#define mem2(a) memset(a, 0x3f, sizeof(a))
#define rep(i, a, n) for(int i = a; i<n; i++)
#define ull unsigned long long
typedef pair<int, int> pll;
const double PI = acos(-1.0);
const double eps = 1e-;
const int mod = 1e9+;
const int inf = ;
const int dir[][] = { {-, }, {, }, {, -}, {, } };
const int maxn = ;
const int maxNode = ;
struct DLX {
int L[maxNode], R[maxNode], U[maxNode], D[maxNode], row[maxNode], col[maxNode];
int S[maxn], H[maxn], deep, sz, n, m, num;
void remove(int c) {
L[R[c]] = L[c];
R[L[c]] = R[c];
for(int i = D[c]; i!=c; i = D[i])
for(int j = R[i]; j!=i; j = R[j]) {
U[D[j]] = U[j];
D[U[j]] = D[j];
S[col[j]]--;
}
}
void resume(int c) {
for(int i = U[c]; i!=c; i = U[i])
for(int j = L[i]; j!=i; j = L[j]) {
S[col[j]]++;
D[U[j]] = j;
U[D[j]] = j;
}
R[L[c]] = c;
L[R[c]] = c;
}
void dfs(int d) {
if(d>=deep)
return ;
if(R[] == ) {
deep = min(d, deep);
return ;
}
int c = R[];
for(int i = R[]; i!=; i = R[i])
if(S[c]>S[i])
c = i;
remove(c);
for(int i = D[c]; i!=c; i = D[i]) {
for(int j = R[i]; j!=i; j = R[j])
remove(col[j]);
dfs(d+);
for(int j = L[i]; j!=i; j = L[j])
resume(col[j]);
}
resume(c);
return ;
}
void add(int r, int c) {
sz++;
col[sz] = c;
S[c]++;
U[sz] = U[c];
D[sz] = c;
D[U[c]] = sz;
U[c] = sz;
if(~H[r]) {
R[sz] = H[r];
L[sz] = L[H[r]];
L[R[sz]] = sz;
R[L[sz]] = sz;
} else {
H[r] = L[sz] = R[sz] = sz;
}
}
void init(){
mem1(H);
for(int i = ; i<=n; i++) {
R[i] = i+;
L[i] = i-;
D[i] = U[i] = i;
}
R[n] = ;
L[] = n;
mem(S);
sz = n;
deep = inf;
}
void solve() {
scanf("%d%d%d", &n, &m, &num);
n *= m;
init();
int x1, y1, x2, y2;
int cnt = ;
while(num--) {
scanf("%d%d%d%d", &x1, &y1, &x2, &y2);
for(int i = x1+; i<=x2; i++) {
for(int j = y1+; j<=y2; j++) {
add(cnt, (i-)*m+j);
}
}
cnt++;
}
dfs();
if(deep == inf) {
puts("-1");
} else {
printf("%d\n", deep);
}
}
}dlx;
int main()
{
int t;
cin>>t;
while(t--) {
dlx.solve();
}
}