Codeforces gym 101350G 数学

来源:互联网 发布:华硕mg248q设置软件 编辑:程序博客网 时间:2024/05/22 00:18

Snake Rana
time limit per test
4 seconds
memory limit per test
256 megabytes
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 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
32 2 12 26 6 25 22 510000 10000 11 1
output
52572500499925000000


题意:给你一个n*m的矩阵和k个雷  问有多少矩形  不包含雷


题解:容斥搞一搞  0-1+2-3...  代表雷数  搞一搞就是答案


#include<iostream>#include<cstdio>#include<cstring>#include<algorithm>#include<cmath>using namespace std;typedef long long ll;ll t,ans,n,m,k,d,x[30],y[30];ll lx,rx,ly,ry,xc;int c[30];ll C(ll a,ll b){ll ans=1,c=a-b;if(a==0)return 0;while(a!=c){ans=ans*a;a--;}while(b!=0){ans=ans/b;b--;}return ans;}int solve(int a){lx=n+2;rx=0;ly=m+2;ry=0;d=0;for(int i=0;i<k;i++)if(a&(1<<i)){if(x[i+1]<lx)lx=x[i+1];if(x[i+1]>rx)rx=x[i+1];if(y[i+1]<ly)ly=y[i+1];if(y[i+1]>ry)ry=y[i+1];d++;}ll ans1=0,ans2=0;if(lx==rx){ans1=n+(lx-1)*(n-rx);}else ans1=(lx)*(n-rx+1);if(ly==ry)ans2=m+(ly-1)*(m-ry);else ans2=(ly)*(m-ry+1);ll xc=ans1*ans2; if(d%2==1)ans-=xc;else ans+=xc;}int main(){scanf("%lld",&t);c[0]=1;for(int i=1;i<=21;i++)c[i]=c[i-1]*2;while(t--){scanf("%lld%lld%lld",&n,&m,&k);for(int i=1;i<=k;i++)scanf("%lld%lld",&x[i],&y[i]);ans=C(n+1,2)*C(m+1,2);for(int i=1;i<=c[k]-1;i++)solve(i);printf("%lld\n",ans);}return 0;}


0 0
原创粉丝点击