【Gym - 101350G】Snake Rana 【状态压缩 +容斥定理】

15 篇文章 0 订阅
13 篇文章 0 订阅

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 N, M, and K (1 ≤ N, M ≤ 104) (1 ≤ K ≤ 20).

The next K lines each contains distinct pair of integers x, y (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的矩形,其中有k个格子中有炸弹,告诉你炸弹的坐标,问有多少种矩形的取法,使得取得的矩形中不含有炸弹。
容斥定理: 我们用矩形可以构造的所有的子矩形个数 - (含有炸弹的矩形数目) 。但是包含炸弹可以有多种情况,包含一个炸弹的,包含两个炸弹的,所以单纯的加是不行的我们要用容斥,首先我们用S[i] 表示所有包含i个炸弹的矩形个数。
那么S[1]-S[2]+S[3]-S[4]…. 奇加偶减。得到不重复的含有炸弹的矩形个数。
但是要怎么计算S[1] S[2]这些呢? 这里就要用到状态压缩,我们用最多20个二进制数来表示每个炸弹是否被选择。然后我们遍历其所有的子集 ,枚举所有的情况。
代码

#include<bits/stdc++.h>
using namespace std;
typedef pair<int,int>pii;
#define first fi
#define second se
#define  LL long long
#define fread() freopen("in.txt","r",stdin)
#define fwrite() freopen("out.txt","w",stdout)
#define CLOSE() ios_base::sync_with_stdio(false)

const int MAXN = 30;
const int MAXM = 1e6;
const int mod = 1e9+7;
const int inf = 0x3f3f3f3f;

struct Node{
    LL x,y;
}node[MAXN]; 
LL n,m,k;
int main(){
    CLOSE();
//  fread();
//  fwrite();
    int T;scanf("%d",&T);
    while(T--){
        scanf("%lld%lld%lld",&n,&m,&k);
        for(int i=0;i<k;i++) scanf("%lld%lld",&node[i].x,&node[i].y);
        LL ans=(n+1)*n/2*(m+1)*m/2; // C(n+1,2)*C(m+1,2)
        for(int i=1;i<(1<<k);i++) { // 枚举所有子集
            LL minx=inf,miny=inf,maxx=0,maxy=0;
            int cnt=0;
            for(int j=0;j<k;j++){// 是否有第j个个炸弹
                if((1<<j)&i) {
                    cnt++;
                    minx=min(minx,node[j].x);
                    miny=min(miny,node[j].y);
                    maxx=max(maxx,node[j].x);
                    maxy=max(maxy,node[j].y);
                }
            }
            LL t=minx*miny*(n-maxx+1)*(m-maxy+1);// x只能在minx的左边和maxx的右边分别选择一个点,y 只能在miny的下面和maxy的上面分别选择一个点才可以。
            if(cnt&1) ans-=t; // 这里是直接减的,所以成了奇减偶加
            else ans+=t;
        }
        printf("%lld\n",ans);
    }   
}
  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值