Codeforces Round #395 (Div. 2)D. Timofey and rectangles

D. Timofey and rectangles
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output

One of Timofey's birthday presents is a colourbook in a shape of an infinite plane. On the plane n rectangles with sides parallel to coordinate axes are situated. All sides of the rectangles have odd length. Rectangles cannot intersect, but they can touch each other.

Help Timofey to color his rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color, or determine that it is impossible.

Two rectangles intersect if their intersection has positive area. Two rectangles touch by sides if there is a pair of sides such that their intersection has non-zero length

The picture corresponds to the first example
Input

The first line contains single integer n (1 ≤ n ≤ 5·105) — the number of rectangles.

n lines follow. The i-th of these lines contains four integers x1y1x2 and y2 ( - 109 ≤ x1 < x2 ≤ 109 - 109 ≤ y1 < y2 ≤ 109), that means that points (x1, y1) and (x2, y2) are the coordinates of two opposite corners of the i-th rectangle.

It is guaranteed, that all sides of the rectangles have odd lengths and rectangles don't intersect each other.

Output

Print "NO" in the only line if it is impossible to color the rectangles in 4 different colors in such a way that every two rectangles touching each other by side would have different color.

Otherwise, print "YES" in the first line. Then print n lines, in the i-th of them print single integer ci (1 ≤ ci ≤ 4) — the color of i-th rectangle.

Example
input
8
0 0 5 3
2 -1 5 0
-3 -4 2 -1
-1 -1 2 0
-3 0 0 5
5 2 10 3
7 -3 10 2
4 -2 7 -1
output
YES
1
2
2
3
2
2
4
1

題意:給出一些邊相接觸的矩形,用四種顏色裏面的一種給矩形上色,問是否存在一種方案讓所有邊接觸的矩形顏色

都不同。有的話打印出來每個矩形的顏色,沒有的話打印NO。

思路:題意中有個很奇怪的條件,邊長爲奇數。今天想了一上午纔想明白。

對於一個座標點(x, y) 如果x是奇數,那麼加奇數長度的值就是偶數,如果是偶數加奇數長度值就是奇數。

用左下角點表示一個矩形位置,對於某個點只有四種狀態 (奇數,奇數)(偶數,偶數)(奇數,偶數)(偶數,奇數)之一,

而邊接觸的矩形位置則是其它3種之一,而顏色只有4種,所以一種狀態給一種顏色,什麼情況都會有解。

printf("%d\n", (2*(x%2) + y%2)+1);

官方題解給的是上面的這種表達式,其實就是先把4種狀態用00 01 10 11編碼,x是高位,y是低位,2×x就是讓x的值

存在高位。這樣得到0 1 2 3,顏色是1 2 3 4所以要加1。





發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章