树状数组与线段树(三) (2)

再看x<=0&&y<=0:x取绝对值,y取绝对值,若x>=y&&x!=0&&y!=0,dis=(2*x-1)*(2*x-1)+(x-y),若x<y&&x!=0&&y!=0,dis=(2*y+1)*(2*y+1)-(y+1-x)

其中需要特殊考虑x和y的负半轴,x!=0&&y==0,dis=(2*x-1)*(2*x-1)+(x-1),当x==0&&y!=0,dis=(2*y+1)*(2*y+1)-(y+1)

再看x>0&&y<0时:y取绝对值,当x<=y,dis=(2*y+1)*(2*y)+(y-x),当x>y时,dis=(2*x+1)*(2*x)-(x-y)

代码:

#include<iostream> using namespace std; typedef long long ll; int main() { ll x,y,i,j; cin>>x>>y; if(x>=0&&y>=0) { if(y>=x) cout<<(2*y)*(2*y)-(y-x)<<endl; else if(y<x) cout<<(2*x)*(2*x)+(x-y)<<endl; } else if(x<0&&y>0) { x=-x; if(x>=y) cout<<(2*x-1)*(2*x)-(x-y)<<endl; else if(x<y) cout<<(2*y-1)*(2*y)+(y-x)<<endl; } else if(x<=0&&y<=0) { x=-x;y=-y; if(x>=y&&x!=0&&y!=0) { cout<<(2*x-1)*(2*x-1)+(x-y)<<endl; } else if(x<y&&x!=0&&y!=0) cout<<(2*y+1)*(2*y+1)-(y+1-x)<<endl; else if(x!=0&&y==0) cout<<(2*x-1)*(2*x-1)+(x-1)<<endl; else if(x==0&&y!=0) cout<<(2*y+1)*(2*y+1)-(y+1)<<endl; } else if(x>0&&y<0) { y=-y; if(x<=y) cout<<(2*y+1)*(2*y)+(y-x)<<endl; else if(x>y) cout<<(2*x+1)*(2*x)-(x-y)<<endl; } return 0; }

内容版权声明:除非注明,否则皆为本站原创文章。

转载注明出处:https://www.heiqu.com/zwpjdg.html