题目:Codeforces 15B laser
地址:http://codeforces.com/problemset/problem/15/B
心得:
- 仔细考虑数据范围,包括过程量与最终结果
- 尽量不要写for(int i=0;;)
代码:
#include<string>
#include<cstring>
#include<algorithm>
#include<iostream>
using namespace std;
int main()
{
long long n, m ,x1, y1, x2, y2, cnt, h, l, i, t ;
long long v[4];
cin>>t;
while(t--)
{
cnt = 0;
cin>>n>>m>>x1>>y1>>x2>>y2;
v[0] = min(y1,y2) - 1;//上移
v[1] = min(x1,x2) - 1;//左移
v[2] = m - max(y1,y2);//下移
v[3] = n - max(x1,x2);//右移
for(i=0;i<4;i++)
{
cnt += v[i];
if(i==3)
cnt += v[3]*v[0];
else
cnt += v[i]*v[i+1];
}
if((max(y1,y2)-v[0])<=(min(y1,y2)+v[2]))//计算重复部分
h = v[2]+v[0]-abs(y1-y2) + 1;
else
h = 0;
if((min(x1,x2)+v[3])>=(max(x1,x2)-v[1]))
l = v[1]+v[3] - abs(x1-x2) +1;
else
l = 0;
cnt = m*n - cnt*2 - 2 + l*h;
cout<<cnt<<endl;
}
}