Codeforces Beta Round #15

A - Cottage Village

A new cottage village called «Flatville» is being built in Flatland. By now they have already built in «Flatville» n square houses with the centres on the Оx-axis. The houses’ sides are parallel to the coordinate axes. It’s known that no two houses overlap, but they can touch each other.

The architect bureau, where Peter works, was commissioned to build a new house in «Flatville». The customer wants his future house to be on the Оx-axis, to be square in shape, have a side t, and touch at least one of the already built houses. For sure, its sides should be parallel to the coordinate axes, its centre should be on the Ox-axis and it shouldn’t overlap any of the houses in the village.

Peter was given a list of all the houses in «Flatville». Would you help him find the amount of possible positions of the new house?

Input

The first line of the input data contains numbers n and t (1 ≤ n, t ≤ 1000). Then there follow n lines, each of them contains two space-separated integer numbers: xi ai, where xi — x-coordinate of the centre of the i-th house, and ai — length of its side ( - 1000 ≤ xi ≤ 1000, 1 ≤ ai ≤ 1000).

Output

Output the amount of possible positions of the new house.

Examples

Input

2 2
0 4
6 2

Output

4

Input

2 2
0 4
5 2

Output

3

Input

2 3
0 4
5 2

Output

2

Note

It is possible for the x-coordinate of the new house to have non-integer value.

This question needs to be noted that where the length of the house if you want, then divided by 2, you need to save to floating point type

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

typedef pair<double, double> p;
p arr[1005];

bool cmp(p a, p b)
{
	return a.first < b.first;
}

int main(void)
{
	int n, t, ans = 2;
	cin >> n >> t;
	for (int i = 0; i < n; i++)
		cin >> arr[i].first >> arr[i].second;
	sort(arr, arr + n, cmp);
	for (int i = 0; i < n - 1; i++)
	{
		if (arr[i].first + arr[i].second / 2 + arr[i + 1].second / 2 + t < arr[i + 1].first)
			ans += 2;
		else if (arr[i].first + arr[i].second / 2 + arr[i + 1].second / 2 + t == arr[i + 1].first)
			ans++;
	}
	cout << ans << endl; 
	return 0;
}

B - Laser

Petya is the most responsible worker in the Research Institute. So he was asked to make a very important experiment: to melt the chocolate bar with a new laser device. The device consists of a rectangular field of n × m cells and a robotic arm. Each cell of the field is a 1 × 1 square. The robotic arm has two lasers pointed at the field perpendicularly to its surface. At any one time lasers are pointed at the centres of some two cells. Since the lasers are on the robotic hand, their movements are synchronized — if you move one of the lasers by a vector, another one moves by the same vector.

The following facts about the experiment are known:

initially the whole field is covered with a chocolate bar of the size n × m, both lasers are located above the field and are active;
the chocolate melts within one cell of the field at which the laser is pointed;
all moves of the robotic arm should be parallel to the sides of the field, after each move the lasers should be pointed at the centres of some two cells;
at any one time both lasers should be pointed at the field. Petya doesn’t want to become a second Gordon Freeman.
You are given n, m and the cells (x1, y1) and (x2, y2), where the lasers are initially pointed at (xi is a column number, yi is a row number). Rows are numbered from 1 to m from top to bottom and columns are numbered from 1 to n from left to right. You are to find the amount of cells of the field on which the chocolate can’t be melted in the given conditions.

Input

The first line contains one integer number t (1 ≤ t ≤ 10000) — the number of test sets. Each of the following t lines describes one test set. Each line contains integer numbers n, m, x1, y1, x2, y2, separated by a space (2 ≤ n, m ≤ 109, 1 ≤ x1, x2 ≤ n, 1 ≤ y1, y2 ≤ m). Cells (x1, y1) and (x2, y2) are distinct.

Output

Each of the t lines of the output should contain the answer to the corresponding input test set.

Examples

Input

2
4 4 1 1 3 3
4 3 1 1 2 2

Output

8
2

Two rectangular region represented by the two points can be reached, if there are two rectangular portions intersecting subtracted.

Solution to a problem link: https: //blog.csdn.net/codeswarrior/article/details/79330202

#include <cstdio>
#include <cmath>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;

typedef long long ll;

int main(void)
{
	int t;
	ll m, n, x1, y1, x2, y2;
	ll dx, dy, cnt, ans;
	cin >> t;
	while (t--)
	{
		cin >> n >> m >> x1 >> y1 >> x2 >> y2;
		if (x1 > x2)
			swap(x1, x2);
		if (y1 > y2)
			swap(y1, y2);
		dx = n - x2 + x1;
		dy = m - y2 + y1;
		cnt = dx * dy * 2;
		if (dx * 2 > n || dy * 2 > m)
			cnt -= (dx * 2 - n) * (dy * 2 - m);
		ans = n * m - cnt;
		cout << ans << endl;
	}
	return 0;
}
Published 162 original articles · won praise 18 · views 20000 +

Guess you like

Origin blog.csdn.net/weixin_43772166/article/details/102882719