题目链接:https://nanti.jisuanke.com/t/30991
Feeling hungry, a cute hamster decides to order some take-away food (like fried chicken for only 3030 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 * mn∗m grid. CXY wants her hamster to calculate the number of rectangles composed of these tiles.
For example, the following 3 * 33∗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.
There are multiple test cases in the input data.
The first line contains a integer TT : number of test cases. T \le 5T≤5.
For each test case, the first line contains 33integers n , m , kn,m,k , denoting that the wall is a n \times mn×m grid, and the number of the black tiles is kk.
For the next kk lines, each line contains 22integers: x\ yx y ,denoting a black tile is on the xx-th row and yy-th column. It's guaranteed that all the positions of the black tiles are distinct.
For all the test cases,
1 \le n \le 10^5,1\le m \le 1001≤n≤105,1≤m≤100,
0 \le k \le 10^5 , 1 \le x \le n, 1 \le y \le m0≤k≤105,1≤x≤n,1≤y≤m.
It's guaranteed that at most 22 test cases satisfy that n \ge 20000n≥20000.
For each test case, print "Case #xx: ansans" (without quotes) in a single line, where xx is the test case number and ansans is the answer for this test case.
The second test case looks as follows:
样例输入复制
2 3 3 0 3 3 1 2 2
样例输出复制
Case #1: 36 Case #2: 20
dp【i】【j】代表以方块ij为左上角的矩阵数目。一边维护一边加。
按行优先从后向前遍历,同时维护到当前为止每一行最右侧被挖掉的方块,每一列最上边被挖掉的方块。如果某个方块下面或者右面的方块被挖掉,则以它作为左上角的矩阵数等于同行或者同列到挖掉为止的最大值。否则暴力计算符合条件的方块数。注意要沿着列计算,因为m最大为100,这样不会t(虽然不知道为什么。。)。
#include
#define inf 0x3f3f3f3f
#define mod 1000000007
#define For(i,m,n) for(int i=m;i<=n;i++)
#define Dor(i,m,n) for(int i=m;i>=n;i--)
#define LL long long
#define lan(a,b) memset(a,b,sizeof(a))
using namespace std;
int vis[100010][110];
LL dp[100010][110];
int line[100010],clu[100010];
int main()
{
int t;
scanf("%d",&t);
For(tt,1,t)
{
int n,m,k;
scanf("%d%d%d",&n,&m,&k);
lan(vis,0);
For(i,1,n)
line[i]=m+1,dp[i][m+1]=0;
For(i,1,m)
clu[i]=n+1,dp[n+1][i]=0;
while(k--)
{
int x,y;
scanf("%d%d",&x,&y);
vis[x][y]=1;
}
LL sum=0;
for(int i=n;i>=1;i--)
{
for(int j=m;j>=1;j--)
{
if(vis[i][j]==1)
{
line[i]=j;
clu[j]=i;
dp[i][j]=0;
continue;
}
if(vis[i][j+1]==1)
{
dp[i][j]=clu[j]-i;
}
else if(vis[i+1][j]==1)
{
dp[i][j]=line[i]-j;
}
else
{
int minn=inf;
LL ans=0;
For(k,j,line[i]-1)
{
if(clu[k]