题意 :给出一个初始的黑白相间的棋盘 有两个人 第一个人先用白色染一块矩形区域
第二个人再用黑色染一块矩形区域 问最后黑白格子各有多少个
思路:这题的关键在于求相交的矩形区间
给出一个矩形的左下和右上角 则相交的条件为 max(X1,X3)<=min(X2,X4)&&max(Y1,Y3)<=min(Y2,Y4) 这里可以用线和线有公共区间的条件来推 矩形就是两个线
还有就是如果相交则求 相交的矩形的 左下和右上的坐标 ll xa=max(X1,X3),ya=max(Y1,Y3),xb=min(X4,X2),yb=min(Y2,Y4);
然后就是简单的容斥正常做就行了
1 #include<bits/stdc++.h> 2 #define FOR(i,f_start,f_end) for(int i=f_start;i<=f_end;i++) 3 #define MS(arr,arr_value) memset(arr,arr_value,sizeof(arr)) 4 #define F first 5 #define S second 6 #define pii pair<int ,int > 7 #define mkp make_pair 8 #define pb push_back 9 #define arr(zzz) array<ll,zzz> 10 using namespace std; 11 #define ll long long 12 #define int long long 13 const int maxn=3e5+5; 14 const int inf=0x3f3f3f3f; 15 ll count(int x1,int y1,int x2,int y2,int o){//return black 16 ll n=x2-x1+1; 17 ll m=y2-y1+1; 18 ll white=0,black=0; 19 if(n&1){ 20 if(m&1){ 21 white=(n+1)/2+1ll*(n/2+(n+1)/2)*(m/2); 22 black=1ll*m*n-white; 23 } 24 else { 25 white=black=1ll*n*m/2; 26 } 27 } 28 else { 29 white=black=1ll*n*m/2; 30 } 31 if(o==0){ 32 return white; 33 } 34 else return black; 35 36 } 37 void exchange(ll &x,ll&y){ 38 ll tmp=x; 39 x=y; 40 y=tmp; 41 } 42 int32_t main(){ 43 int t; 44 scanf("%lld",&t); 45 int n,m; 46 while(t--){ 47 ll black=0,white=0; 48 int X1,Y1,X2,Y2,X3,Y3,X4,Y4; 49 scanf("%lld%lld",&n,&m); 50 scanf("%lld%lld%lld%lld%lld%lld%lld%lld",&X1,&Y1,&X2,&Y2,&X3,&Y3,&X4,&Y4); 51 if(n&1){ 52 if(m&1){ 53 white=(n+1)/2+ (n/2+(n+1)/2)*(m/2); 54 black=1ll*m*n-white; 55 } 56 else { 57 white=black=1ll*n*m/2; 58 } 59 } 60 else { 61 white=black=1ll*n*m/2; 62 } 63 ll tmp1=count(X1,Y1,X2,Y2,(X1+Y1)%2==0); 64 white-=1ll*(X2-X1+1)*(Y2-Y1+1)-tmp1; 65 white+=1ll*(X2-X1+1)*(Y2-Y1+1); 66 black-=tmp1; 67 ll tmp2=count(X3,Y3,X4,Y4,(X3+Y3)%2==0); 68 white-=1ll*(X4-X3+1)*(Y4-Y3+1)-tmp2; 69 black-=tmp2; 70 black+=1ll*(X4-X3+1)*(Y4-Y3+1); 71 /*if(X1<=X3){ 72 exchange(X1,X3); 73 exchange(X2,X4); 74 exchange(Y1,Y3); 75 exchange(Y2,Y4); 76 } 77 if(){ 78 79 }*/ 80 ll minx=min(X1,X3); 81 ll miny=min(Y1,Y3); 82 if(max(X1,X3)<=min(X2,X4)&&max(Y1,Y3)<=min(Y2,Y4)){ 83 ll xa=max(X1,X3),ya=max(Y1,Y3),xb=min(X4,X2),yb=min(Y2,Y4); 84 ll tmp=count(xa,ya,xb,yb,(xa+ya)%2==0); 85 //cout<<xa<<" "<<ya<<" "<<xb<<""<<yb<<" fuck"<<tmp<<endl; 86 black+=tmp; 87 white-=1ll*(xb-xa+1)*(ya-yb+1); 88 white+=1ll*(xb-xa+1)*(ya-yb+1)-tmp; 89 90 } 91 cout<<white<<" "<<black<<endl; 92 } 93 94 return 0; 95 }