ACM-ICPC 2018 南京赛区网络预赛__B The writing on the wall【枚举】

  •  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

题目来源

ACM-ICPC 2018 南京赛区网络预赛

题目大意:一个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;
} 

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值