判断矩形相交的方法
给你两个矩形,分别给你矩形的左下角和右上角,问你是否相交。
第一个矩形:(x1,y1),(x2,y2)
第二个矩形: (x3,y3),(x4,y4)
如果满足max(x1,x3)<=min(x2,x4)&&max(y1,y3)<=min(y2,y4),则相交。
具体链接:http://codeforces.com/contest/1080/problem/C
AC代码:
1 #include <bits/stdc++.h> 2 using namespace std; 3 # define ll long long 4 int main() 5 { 6 int T; 7 scanf("%d",&T); 8 while(T--) 9 { 10 ll n,m; 11 scanf("%lld %lld",&n,&m); 12 ll x1,y1,x2,y2; 13 ll x3,y3,x4,y4; 14 scanf("%lld %lld %lld %lld %lld %lld %lld %lld",&x1,&y1,&x2,&y2,&x3,&y3,&x4,&y4); 15 ll white=0; 16 // if(n%2!=0&&m%2!=0) 17 // { 18 white=(n*m+1)/2; 19 // cout<<1<<" "<<white<<endl; 20 // } 21 // else 22 // white=(n*m)/2; 23 // int len1,len2; 24 if((x1%2+y1%2)==0||(x1%2+y1%2)==2) 25 { 26 white+=((x2-x1+1)*(y2-y1+1))/2; 27 } 28 else 29 white+=((x2-x1+1)*(y2-y1+1)+1)/2; 30 //cout<<2<<" "<<white<<endl; 31 if((x3%2+y3%2)==0||(x3%2+y3%2)==2) 32 { 33 white-=((x4-x3+1)*(y4-y3+1)+1)/2; 34 } 35 else 36 white-=((x4-x3+1)*(y4-y3+1))/2; 37 //cout<<3<<" "<<white<<endl; 38 ll x5,y5,x6,y6; 39 x5=max(x1,x3); 40 x6=min(x2,x4); 41 y5=max(y1,y3); 42 y6=min(y2,y4); 43 // cout<<white<<endl; 44 //cout<<x5<<" "<<y5<<endl<<x6<<" "<<y6<<endl; 45 if(x5<=x6&&y5<=y6) 46 { 47 if((x5%2+y5%2)==0||(x5%2+y5%2)==2) 48 { 49 white-=((x6-x5+1)*(y6-y5+1))/2; 50 } 51 else 52 white-=((x6-x5+1)*(y6-y5+1)+1)/2; 53 } 54 // cout<<white<<endl; 55 printf("%lld %lld\n",white,n*m-white); 56 } 57 return 0; 58 }