#P9957. [USACO20DEC] Stuck in a Rut B

[USACO20DEC] Stuck in a Rut B

Problem Description

Farmer John has recently expanded his farm. From the cows’ point of view, the farm is basically infinite! The cows imagine the grazing area on the farm as an infinite two-dimensional grid made of square cells, and each cell contains delicious grass (treat each cell like a square on a chessboard). Farmer John’s NN cows (1N501 \le N \le 50) start in different cells. Some face north, and some face east.

Every hour, each cow does one of the following:

  • If the grass in her current cell has already been eaten by another cow, then she stops.
  • She eats all the grass in her current cell, and moves one cell in the direction she is facing.

After some time, each cow will leave behind a trail of cells with no grass.

If two cows move into the same grassy cell in a single move, they share the grass in that cell, and in the next hour they continue moving in the directions they are facing.

Please find the amount of grass each cow eats. Some cows will never stop, and thus eat infinitely much grass.

Input Format

The first line contains NN. The next NN lines each describe a cow’s starting position, containing a character N (meaning she faces north) or E (meaning she faces east), and two nonnegative integers xx and yy (0x1090 \le x \le 10^9, 0y1090 \le y \le 10^9) giving the cell coordinates. All xx coordinates are distinct, and all yy coordinates are distinct.

To make directions and coordinates as clear as possible: if a cow is in cell (x,y)(x, y) and moves north, she reaches cell (x,y+1)(x, y+1). If she moves east, she reaches cell (x+1,y)(x+1, y).

Output Format

Output NN lines. The ii-th line should contain the number of grass cells eaten by the ii-th cow in the input. If a cow can eat infinitely much grass, output Infinity for that cow.

6
E 3 5
N 5 3
E 4 6
E 10 4
N 11 2
N 8 1
5
3
Infinity
Infinity
2
5

Hint

Testdata Properties

  • In testdata 252-5, all coordinates are at most 100100.
  • Testdata 6106-10 has no additional constraints.

Translated by ChatGPT 5