zoukankan      html  css  js  c++  java
  • G. Snake Rana(容斥)

    G. Snake Rana
    time limit per test
    4.0 s
    memory limit per test
    256 MB
    input
    standard input
    output
    standard output

    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
    Copy
    3
    2 2 1
    2 2
    6 6 2
    5 2
    2 5
    10000 10000 1
    1 1
    Output
    Copy
    5
    257
    2500499925000000






     1 #include <iostream>
     2 #include <cstdio>
     3 #include <algorithm>
     4 #include <cstdlib>
     5 #include <cstring>
     6 #include <string>
     7 #include <deque>
     8 #include <set>
     9 #include <queue>
    10 using namespace std;
    11 #define  ll long long 
    12 #define  N 10009
    13 #define  gep(i,a,b)   for(int  i=a;i<=b;i++)
    14 #define  gepp(i,a,b)  for(int  i=a;i>=b;i--)
    15 #define  gep1(i,a,b)  for(ll i=a;i<=b;i++)
    16 #define  gepp1(i,a,b) for(ll i=a;i>=b;i--)    
    17 #define  mem(a,b)  memset(a,b,sizeof(a))
    18 #define  P  pair<int,int>
    19 #define  inf 10000009
    20 struct Node{
    21     ll x,y;
    22 }node[30];
    23 int  main()
    24 {
    25     int t,k;
    26     ll n,m;
    27     scanf("%d",&t);
    28     while(t--)
    29     {        
    30         scanf("%lld%lld%d",&n,&m,&k);//开始时k %lld ,t直接变为0了        
    31         ll ans=n*(n+1)/2*m*(m+1)/2;
    32         //一共ans个,要在减去含有炸弹的,容斥定理
    33         //含有炸弹的=仅含有一个的-含有两个的+含有三个的-……
    34         /*
    35         
    36 要计算几个集合并集的大小,我们要先将所有单个集合的大小
    37 计算出来,然后减去所有两个集合相交的部分,
    38 再加回所有三个集合相交的部分,
    39 再减去所有四个集合相交的部分,依此类推,
    40 一直计算到所有集合相交的部分。
    41 
    42 */
    43         gep(i,0,k-1){
    44             scanf("%lld %lld",&node[i].x,&node[i].y);
    45         }    
    46         for(int i=1;i<(1<<k);i++)//二进制枚举所有的情况
    47         {
    48             ll m1=inf,m2=inf,m3=-1,m4=-1;
    49             int cnt=0;
    50             gep(j,0,k-1){
    51                 if(i>>j&1){//i的j位是1吗
    52                     m1=min(m1,node[j].x);
    53                     m2=min(m2,node[j].y);
    54                     m3=max(m3,node[j].x);
    55                     m4=max(m4,node[j].y);
    56                     cnt++;//有几个炸弹 
    57                 }
    58             }
    59             //m1 :横坐标的最小值 m2 :纵坐标的最小值
    60             //m3  :横坐标的最大值  m4 :纵坐标的最大值
    61             //一个矩形由左上角 和右下角 决定 
    62             //m1*m2  左上的位置数  ,(n-m3+1)*(m-m4+1):右下的位置数
    63             ll ret=m1*m2*(n-m3+1)*(m-m4+1);
    64             if(cnt&1) ans-=ret;
    65             else ans+=ret;
    66         }    
    67         printf("%lld
    ",ans);    
    68     }
    69     return 0;
    70 }
















  • 相关阅读:
    基于NPOI的报表引擎——ExcelReport
    XML数据源快速开发框架——XmlFramwork
    SqlExcel使用文档及源码
    第三篇:属性_第二节:控件属性在页面及源码中的表示方式
    第三篇:属性_第一节:控件属性与属性的持久化
    第二篇:呈现内容_第四节:个性化自定义控件
    第二篇:呈现内容_第三节:CompositeControl呈现
    Web用户控件开发--星型评分控件
    iOS 统计Xcode整个工程的代码行数
    iOS开发中的火星坐标系及各种坐标系转换算法
  • 原文地址:https://www.cnblogs.com/zhangbuang/p/10920307.html
Copyright © 2011-2022 走看看