• Gym


    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


    题意:
    给了一个矩阵,里面有炸弹,求不含炸弹的子矩阵个数。
    思路:
    状压枚举每种状况,减去奇数个炸弹的情况,加上偶数个炸弹的情况。
    计算情况种数的方法,就是计算出与当前点有关的区间重合的左上角和右上角,把左上角和右上角的个数相乘就行了。
    #include<iostream>
    #include<algorithm>
    #include<vector>
    #include<stack>
    #include<queue>
    #include<map>
    #include<set>
    #include<cstdio>
    #include<cstring>
    #include<cmath>
    #include<ctime>
    #define fuck(x) cout<<#x<<" = "<<x<<endl;
    #define ls (t<<1)
    #define rs ((t<<1)+1)
    using namespace std;
    typedef long long ll;
    typedef unsigned long long ull;
    const int maxn = 100086;
    const int inf = 2.1e9;
    const ll Inf = 999999999999999999;
    const int mod = 1000000007;
    const double eps = 1e-6;
    const double pi = acos(-1);
    ll ans=0;
    int x[maxn],y[maxn];
    int n,m;
    void solve(int p){
        int k=0;
        int maxx=0,maxy=0;
        int minx=inf,miny=inf;
        int t=1;
        while(p){
            if(p&1){
                maxx=max(maxx,x[t]);
                maxy=max(maxy,y[t]);
                minx=min(minx,x[t]);
                miny=min(miny,y[t]);
                k++;
            }
            p>>=1;
            t++;
        }
        if(k&1){
            k=-1;
        }
        else{
            k=1;
        }
        ans+=1ll*k*(minx)*miny*(n-maxx+1)*(m-maxy+1);
    }
    
    int main()
    {
        int T;
        scanf("%d",&T);
        while(T--){
            int k;
            scanf("%d%d%d",&n,&m,&k);
            int tot=1<<k;
            ans=1ll*n*(n+1)*m*(m+1)/4;
            for(int i=1;i<=k;i++){
                scanf("%d%d",&x[i],&y[i]);
            }
    
            for(int i=1;i<tot;i++){
                solve(i);
            }
            printf("%lld
    ",ans);
        }
        return 0;
    }
    View Code
  • 相关阅读:
    python学习的第20天内置模块之sys、os、os下的path、random、shutil
    【数据结构】数组
    【INDEX】【C和C++】学习汇总
    【Spark】Spark环境配置
    【Scala】一些没有的关键字和声明
    【Scala】异常控制
    二叉树的最大深度
    回文链表
    环形链表
    删除链表的倒数第N个节点
  • 原文地址:https://www.cnblogs.com/ZGQblogs/p/10662176.html
Copyright © 2020-2023  润新知