E. A rectangle

Developing tools for creation of locations maps for turn-based fights in a new game, Petya faced the following problem.

A field map consists of hexagonal cells. Since locations sizes are going to be big, a game designer wants to have a tool for quick filling of a field part with identical enemy units. This action will look like following: a game designer will select a rectangular area on the map, and each cell whose center belongs to the selected rectangle will be filled with the enemy unit.

More formally, if a game designer selected cells having coordinates (x1, y1) and (x2, y2), where x1 ≤ x2 and y1 ≤ y2, then all cells having center coordinates (x, y) such that x1 ≤ x ≤ x2 and y1 ≤ y ≤ y2 will be filled. Orthogonal coordinates system is set up so that one of cell sides is parallel to OX axis, all hexagon centers have integer coordinates and for each integer x there are cells having center with such x coordinate and for each integer y there are cells having center with such y coordinate. It is guaranteed that difference x2 - x1 is divisible by 2.

Working on the problem Petya decided that before painting selected units he wants to output number of units that will be painted on the map.

Help him implement counting of these units before painting.

Input

The only line of input contains four integers x1, y1, x2, y2 ( - 109 ≤ x1 ≤ x2 ≤ 109,  - 109 ≤ y1 ≤ y2 ≤ 109) — the coordinates of the centers of two cells.

Output

Output one integer — the number of cells to be filled.

Examples
input
1 1 5 5
output

13

题目大意:给你一个初始坐标和一个末坐标,然后求在这个范围内一共有多少个正六边形



数据范围:( - 109 ≤ x1 ≤ x2 ≤ 10000000000,  - 109 ≤ y1 ≤ y2 ≤ 10000000000)(x1,x2,y1,y2分别是初末X轴坐标,初末Y轴坐标)

解题思路:

图形如图所示,这道题就是一个找规律的题.

一开始的时候,是想着每一个单位方格里面是一个正六边形的一半,然后看给的范围内一共有多少个正六边形的一半就可以了,但是发现不是,所以又发现X轴都是对应的一竖列正六边形的中心,Y轴奇数对应一横排的正六边形的中心,偶数是正六边形的边,这种情况随着X轴的奇偶性发生变化,所以也是可以找到规律,x,y奇偶性相同就可以有一个正六边形的中心

所以奇数一共是(x2-x1)/2*(y2-y1)/2,偶数是((x2-x1)/2+1)*((y2-y1+1)/2+1)

两者相加即可

#include<iostream>
#include<algorithm>
#include<cstring>
#include<string>
#include<cmath>
using namespace std;
long long int a, b, c, d;
int main()
{
	cin >> a >> b >> c >> d;
	cout << ((c - a) / 2 + 1)*((d - b) / 2 + 1) + (c - a) / 2 * (d - b) / 2 << endl;
}


  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Sure, here's the C++ program that meets your requirements: ```c++ #include <iostream> #include <string> using namespace std; class Shape { protected: string name; string color; public: Shape(string name, string color) { this->name = name; this->color = color; } virtual void show() { cout << "Name: " << name << endl; cout << "Color: " << color << endl; } }; class Circle : public Shape { private: double radius; public: Circle(string name, string color, double radius) : Shape(name, color) { this->radius = radius; } void show() override { Shape::show(); cout << "Radius: " << radius << endl; } }; class Rectangle : public Shape { private: double width; double height; public: Rectangle(string name, string color, double width, double height) : Shape(name, color) { this->width = width; this->height = height; } void show() override { Shape::show(); cout << "Width: " << width << endl; cout << "Height: " << height << endl; } }; void showShape(const Shape &shape) { shape.show(); } int main() { Circle circle1("Circle", "Red", 5.0); Rectangle rectangle1("Rectangle", "Green", 4.0, 6.0); Shape *p_shape = &circle1; showShape(*p_shape); p_shape = &rectangle1; showShape(*p_shape); return 0; } ``` In this program, we define a `Shape` class and its subclasses `Circle` and `Rectangle`. The `Shape` class has two data members: `name` and `color`. The `Circle` class has an additional data member `radius`, and the `Rectangle` class has two additional data members `width` and `height`. Each class has a constructor to initialize its data members. In addition, each class defines a `show()` function to display its data members. The `Shape` class defines this function as virtual, so that its subclasses can override it. We also define a global function `showShape(const Shape &shape)` to call a `Shape` object's `show()` function to display its content. In the `main()` function, we create a `Circle` object `circle1` and a `Rectangle` object `rectangle1`. We then define a `Shape` pointer `p_shape` to point to `circle1`, and call `showShape(*p_shape)` to display the circle's information. We then change `p_shape` to point to `rectangle1`, and call `showShape(*p_shape)` to display the rectangle's information.

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值