题意
题解
答案为各子矩阵 i i i 的最大值小于等于 v i v_i vi 的方案数,减去各子矩阵 i i i 的最大值小于 v i v_i vi 的方案数。考虑容斥原理,则需要求解数个子矩阵的最大值同时小于自身 v v v 的方案数。有如下两种方案:坐标离散化;分值域统计 + 容斥原理。
坐标离散化
横纵轴值域较大,坐标离散化处理。此时,平面划分为 O ( N 2 ) O(N^2) O(N2) 个值域相同的区域。 O ( N 3 2 N log N ) O(N^32^N\log N) O(N32NlogN) 枚举子集,暴力扫描区域,使用快速幂进行统计。
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 11, maxx = maxn * 2, mod = 1e9 + 7;
struct matrix
{
int x1, y1, x2, y2, v;
} mat[maxn];
int T, H, W, M, N;
int F[maxx][maxx], nx, sx[maxx], ny, sy[maxx];
inline int cpx(int x) { return lower_bound(sx, sx + nx, x) - sx; }
inline int cpy(int y) { return lower_bound(sy, sy + ny, y) - sy; }
ll pw(ll x, int n)
{
ll res = 1;
while (n)
{
if (n & 1)
res = res * x % mod;
x = x * x % mod, n >>= 1;
}
return res;
}
int main()
{
scanf("%d", &T);
while (T--)
{
scanf("%d%d%d%d", &H, &W, &M, &N);
nx = ny = 0;
sx[nx++] = 0, sx[nx++] = H;
sy[ny++] = 0, sy[ny++] = W;
for (int i = 0; i < N; ++i)
{
matrix &t = mat[i];
scanf("%d%d%d%d%d", &t.x1, &t.y1, &t.x2, &t.y2, &t.v);
sx[nx++] = --t.x1, sx[nx++] = t.x2;
sy[ny++] = --t.y1, sy[ny++] = t.y2;
}
sort(sx, sx + nx), nx = unique(sx, sx + nx) - sx;
sort(sy, sy + ny), ny = unique(sy, sy + ny) - sy;
for (int i = 0; i < N; ++i)
{
matrix &t = mat[i];
t.x1 = cpx(t.x1), t.y1 = cpy(t.y1);
t.x2 = cpx(t.x2), t.y2 = cpy(t.y2);
}
ll res = 0;
for (int i = 0; i < (1 << N); ++i)
{
for (int x = 0; x + 1 < nx; ++x)
for (int y = 0; y + 1 < ny; ++y)
F[x][y] = M;
int c = 0;
for (int j = 0; j < N; ++j)
{
if (i >> j & 1)
++c;
matrix &t = mat[j];
for (int x = t.x1; x < t.x2; ++x)
for (int y = t.y1; y < t.y2; ++y)
F[x][y] = min(F[x][y], (i >> j & 1) ? t.v - 1 : t.v);
}
ll tmp = 1;
for (int x = 0; x + 1 < nx; ++x)
for (int y = 0; y + 1 < ny; ++y)
tmp = tmp * pw(F[x][y], (sx[x + 1] - sx[x]) * (sy[y + 1] - sy[y])) % mod;
res += (c & 1) ? -tmp : tmp;
}
printf("%lld\n", (res % mod + mod) % mod);
}
return 0;
}
分值域统计 + 容斥原理
若一个子矩阵满足最大值为 v v v 的条件,那么合法方案中子矩阵所有元素小于等于 v v v,且至少存在一个值为 v v v 的元素。容易观察到,值域不同的元素可以独立地计算,最后通过乘法原理统计。
将子矩阵按照 v v v 排序,分值域统计。设所有元素值域为 [ 1 , v ] [1,v] [1,v] 的区域面积为 s u m sum sum,某个子矩阵中所有元素值域为 [ 1 , v ] [1,v] [1,v] 的区域面积为 s s s。则子矩阵的最大值小于 v v v 的方案数为 ( v − 1 ) s v s u m − s (v-1)^sv^{sum-s} (v−1)svsum−s。值域为 [ 1 , v ] [1,v] [1,v] 的区域面积,可以通过值域为 [ 1 , v ] [1,v] [1,v] 与 [ 1 , v ′ ] , v ′ < v [1,v'],v'<v [1,v′],v′<v 的区域并集的面积减去值域为 [ 1 , v ′ ] , v ′ < v [1,v'],v'<v [1,v′],v′<v 的区域面积求解。
O ( 3 N ) O(3^N) O(3N) 应用容斥原理预处理出所有矩阵并的面积,总时间复杂度 ( 3 N + 2 N log N ) (3^N+2^N\log N) (3N+2NlogN).
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 11, mod = 1e9 + 7;
struct matrix
{
int x1, y1, x2, y2, v;
bool operator<(const matrix &o) const { return v < o.v; }
matrix operator&(const matrix &o)
{
matrix t;
t.x1 = max(x1, o.x1), t.y1 = max(y1, o.y1);
t.x2 = min(x2, o.x2), t.y2 = min(y2, o.y2);
return t;
}
int S()
{
if (x1 > x2 || y1 > y2)
return 0;
return (x2 - x1 + 1) * (y2 - y1 + 1);
}
} org[maxn], mat[1 << maxn];
int T, H, W, M, N, sz[1 << maxn];
ll is[1 << maxn], us[1 << maxn];
ll pw(ll x, int n)
{
ll res = 1;
while (n)
{
if (n & 1)
res = res * x % mod;
x = x * x % mod, n >>= 1;
}
return res;
}
int main()
{
scanf("%d", &T);
while (T--)
{
scanf("%d%d%d%d", &H, &W, &M, &N);
for (int i = 0; i < N; ++i)
scanf("%d%d%d%d%d", &org[i].x1, &org[i].y1, &org[i].x2, &org[i].y2, &org[i].v);
sort(org, org + N);
for (int i = 0; i < N; ++i)
mat[1 << i] = org[i], is[1 << i] = org[i].S(), sz[1 << i] = 1;
for (int i = 1, low; i < (1 << N); ++i)
if (i != (low = i & -i))
{
mat[i] = mat[i - low] & mat[low];
is[i] = mat[i].S(), sz[i] = sz[i - low] + 1;
}
us[0] = 0;
for (int i = 1; i < (1 << N); ++i)
{
us[i] = 0;
for (int sub = i; sub; sub = i & (sub - 1))
us[i] += (sz[sub] & 1) ? is[sub] : -is[sub];
}
ll res = 1;
for (int i = 0, l = 0, r = 0; i < N; ++i)
if (i + 1 == N || org[i].v != org[i + 1].v)
{
r = i + 1;
if (org[i].v > 1)
{
int ls = (1 << l) - 1, rs = (1 << r) - 1 - ls;
for (int i = l; i < r; ++i)
if (us[(1 << i) | ls] - us[ls] == 0)
res = 0;
if (!res)
break;
int sum = us[rs | ls] - us[ls], v = org[i].v;
ll tmp = pw(v, sum);
for (int sub = rs; sub; sub = rs & (sub - 1))
{
int s = us[sub | ls] - us[ls];
ll t = pw(v - 1, s) * pw(v, sum - s) % mod;
tmp += (sz[sub] & 1) ? -t : t, tmp %= mod;
}
res = tmp * res % mod;
}
l = r;
}
res = res * pw(M, H * W - us[(1 << N) - 1]) % mod;
printf("%lld\n", (res % mod + mod) % mod);
}
return 0;
}