Snake Rana ——(暴力+容斥+计数)

Old Macdonald wants to build a new hen house for his hens. He buys a new rectangular area of size N by M. The night before he builds the hen house, snake Rana devises an evil plan to plant bombs in K distinct cells in the area to kill the hens and eat them for dinner later.

The morning of, Old Macdonald notices that each of the K cells, where snake Rana planted a bomb, have a marking on them. That won’t stop him though, all he must do is build the hen house in an area with no bombs.

Assume that rows are numbered from top to bottom, and columns are numbered from left to right. Old Macdonald now wants to know the number of ways he can choose sub-rectangles of top left coordinates (x1, y1) and bottom right coordinates (x2, y2) (x1 ≤ x2) (y1 ≤ y2) such that there are no bombs in the sub rectangle.

Input

The first line of input is T – the number of test cases.

The first line of each test case is three integers NM, and K (1 ≤ N, M ≤ 104) (1 ≤ K ≤ 20).

The next K lines each contains distinct pair of integers xy (1 ≤ x ≤ N) (1 ≤ y ≤ M)- where (x, y) is the coordinate of the bomb.

Output

For each test case, output a line containing a single integer - the number of sub-rectangles that don’t contain any bombs.

Example
Input
3
2 2 1
2 2
6 6 2
5 2
2 5
10000 10000 1
1 1
Output
5
257
2500499925000000
题目思路:对于这道题我们容易想到用容斥,就是先算出全部的矩形,然后再减去包含炸弹的矩形个数,
那么怎么去算呢,对于全部的矩形个数,我们可以用公式n*m*(n+1)*(m+1)/4算出,然后对于包含炸弹的矩形,根据容斥原理奇数个个数的减,偶数的加,因为k只有20个,所以可以用暴力枚举子集,然后根据边界算出包含当前选中炸弹的矩形有多少个
ac代码:
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<iostream>
#include<cmath>
#include<sstream>
#define LL long long
#define INF 0x3f3f3f3f
using namespace std;
const int maxn = 1e5;
LL n,m,k;
int T;
struct node
{
    LL x,y;
}E[25];
int main()
{
    scanf("%d",&T);
    while(T--){
        scanf("%lld%lld%lld",&n,&m,&k);
        for(int i = 0;i<k;i++){
            scanf("%lld%lld",&E[i].x,&E[i].y);
        }
        LL all = n*m*(n+1)*(m+1)/4;
        //cout<<all<<endl;
        for(int i = 0;i<(1<<k);i++){
            int cnt = 0;
            LL mnl = (LL)INF;
            LL mnu = (LL)INF;
            LL mxr = -1LL;
            LL mxd = -1LL;
            for(int j = 0;j<k;j++){
                if(i&(1<<j)){
                    cnt++;
                    mnl = (LL)min(mnl,E[j].y);
                    mxr = (LL)max(mxr,E[j].y);
                    mnu = (LL)min(mnu,E[j].x);
                    mxd = (LL)max(mxd,E[j].x);
                }
            }
            if(cnt==0)
                continue;
            //cout<<cnt<<endl;
            if(cnt%2==0){
                all += mnl*(m-mxr+1LL)*mnu*(n-mxd+1LL);
            }
            else{
                all -= mnl*(m-mxr+1LL)*mnu*(n-mxd+1LL);
            }
        //cout<<mnl*(m-mxr+1)*mnu*(n-mxd+1)<<' '<<all<<endl;
        }
        printf("%I64d\n",all);
    }
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值