- 2000ms
- 262144K
Feeling hungry, a cute hamster decides to order some take-away food (like fried chicken for only 30 Yuan).
However, his owner CXY thinks that take-away food is unhealthy and expensive. So she demands her hamster to fulfill a mission before ordering the take-away food. Then she brings the hamster to a wall.
The wall is covered by square ceramic tiles, which can be regarded as a n∗m grid. CXY wants her hamster to calculate the number of rectangles composed of these tiles.
For example, the following 3∗3 wall contains 3636 rectangles:
Such problem is quite easy for little hamster to solve, and he quickly manages to get the answer.
Seeing this, the evil girl CXY picks up a brush and paint some tiles into black, claiming that only those rectangles which don't contain any black tiles are valid and the poor hamster should only calculate the number of the valid rectangles. Now the hamster feels the problem is too difficult for him to solve, so he decides to turn to your help. Please help this little hamster solve the problem so that he can enjoy his favorite fried chicken.
Input
There are multiple test cases in the input data.
The first line contains a integer T : number of test cases. T≤5.
For each test case, the first line contains 3 integers n,m,k , denoting that the wall is a n×m grid, and the number of the black tiles is k.
For the next k lines, each line contains 2 integers: x y ,denoting a black tile is on the x-th row and y-th column. It's guaranteed that all the positions of the black tiles are distinct.
For all the test cases,
1≤n≤10^5,1≤m≤100,
0≤k≤10^5,1≤x≤n,1≤y≤m.
It's guaranteed that at most 2 test cases satisfy that n≥20000.
Output
For each test case, print "Case #x: ans" (without quotes) in a single line, where x is the test case number and ansans is the answer for this test case.
Hint
The second test case looks as follows:
样例输入
2
3 3 0
3 3 1
2 2
样例输出
Case #1: 36
Case #2: 20
题目来源
题目大意:一个n*m的方格矩阵,有的格子被涂成了黑色,问该矩阵中有多少个子矩阵,子矩阵不包含黑色格子
题解:枚举每个以(i,j)为右下角的矩阵的个数,具体细节看代码
AC的C++代码:
#include<iostream>
#include<cstring>
using namespace std;
typedef long long ll;
const int INF=0x3f3f3f3f;
const int N=100002;
bool flag[N][105];
int up[105];
int main()
{
int T,n,m,k,x,y;
scanf("%d",&T);
for(int t=1;t<=T;t++){
memset(flag,false,sizeof(flag));
memset(up,0,sizeof(up));
scanf("%d%d%d",&n,&m,&k);
while(k--){
scanf("%d%d",&x,&y);
flag[x][y]=true;
}
ll ans=0;
for(int i=1;i<=n;i++){
//更新up数组
for(int j=1;j<=m;j++)
if(flag[i][j])
up[j]=i;//第j列中为黑块的行(在1~i行中取最大行标
for(int j=1;j<=m;j++){//统计以(i,j)为右下角的矩阵的个数
int h=INF;//统计矩阵长为k时高最大为多少
for(int k=1;k<=j;k++){//枚举矩阵的长
//j-k+1表示以(i,j)为右下角长为k的矩阵的左边界下标
h=min(h,i-up[j-k+1]);//i-up[j-k+1]表示此列能取得的最大高
ans+=h;
}
}
}
printf("Case #%d: %lld\n",t,ans);
}
return 0;
}