题解 P3813 【[FJOI2017]矩阵填数】
Update On 2020.3.6:修复了一些细节问题
“子矩阵最大值等于
复杂度
#include<bits/stdc++.h>
//this code is written by Itst
using namespace std;
const int MOD = 1e9 + 7;
int arr[25][25] , X[25] , Y[25] , cntx , cnty , W , H , N , M , T , pos[11][5];
int poww(long long a , int b){
int times = 1;
while(b){
if(b & 1) times = times * a % MOD;
a = a * a % MOD; b >>= 1;
}
return times;
}
int main(){
#ifdef ONLINE_JUDGE
freopen("in","r",stdin);
freopen("out","w",stdout);
#endif
ios::sync_with_stdio(0);
for(cin >> T ; T ; --T){
cin >> W >> H >> M >> N; cntx = cnty = 0;
for(int i = 1 ; i <= N ; ++i){
for(int j = 0 ; j < 5 ; ++j)
cin >> pos[i][j];
X[++cntx] = pos[i][0]; Y[++cnty] = pos[i][1];
X[++cntx] = ++pos[i][2]; Y[++cnty] = ++pos[i][3];
}
X[++cntx] = 1; X[++cntx] = W + 1;
Y[++cnty] = 1; Y[++cnty] = H + 1;
sort(X + 1 , X + cntx + 1); cntx = unique(X + 1 , X + cntx + 1) - X - 1;
sort(Y + 1 , Y + cnty + 1); cnty = unique(Y + 1 , Y + cnty + 1) - Y - 1;
int ans = 0;
for(int i = 0 ; i < 1 << N ; ++i){
for(int j = 1 ; j < cntx ; ++j)
for(int k = 1 ; k < cnty ; ++k)
arr[j][k] = M;
for(int j = 1 ; j <= N ; ++j)
for(int k = lower_bound(X + 1 , X + cntx + 1 , pos[j][0]) - X ; X[k] != pos[j][2] ; ++k)
for(int l = lower_bound(Y + 1 , Y + cnty + 1 , pos[j][1]) - Y ; Y[l] != pos[j][3] ; ++l)
arr[k][l] = min(arr[k][l] , pos[j][4] - (i >> (j - 1) & 1));
int tmp = 1;
for(int j = 1 ; j < cntx ; ++j)
for(int k = 1 ; k < cnty ; ++k)
tmp = 1ll * tmp * poww(arr[j][k] , (X[j + 1] - X[j]) * (Y[k + 1] - Y[k])) % MOD;
ans = (ans + MOD + (__builtin_popcount(i) & 1 ? -1ll : 1ll) * tmp) % MOD;
}
cout << ans << endl;
}
return 0;
}