ACM-ICPC 2018 南京賽區網路預賽__B The writing on the wall【列舉】

Enjoy_process發表於2018-09-05
  •  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;
} 

 

相關文章