Untangling Chain

Time limit0.5sMemory limit512 MB

Summary
Given edge turn directions, rebuild the chain by always extending the current bounding box by one in the heading direction and print the resulting edge lengths.
Level

Medium4 of 10

Topics
Simulation, Implementation
Solved
No attempts yet

Problem

A rectilinear chain is an ordered sequence of segments that alternates between horizontal and vertical. Its first segment starts at the origin (0,0)(0,0) and goes to the right. A rectilinear chain of nn edges is described by nn pairs (lk,tk)(l_k, t_k), where lkl_k is the length of the kk-th edge eke_k and tkt_k is the turning direction from eke_k to ek+1e_{k+1}. For 1≤k<n1 \le k < n, tk=1t_k = 1 if the chain turns left and tk=−1t_k = -1 if it turns right. For k=nk = n, tn=0t_n = 0 marks ene_n as the last edge. For example, the chain in figure (a) is described by (4, 1), (5, -1), (2, -1), (2, -1), (4, 1), (5, 0).

Figure. (a) A chain that is not simple. (b) An untangled simple chain.

A chain is simple if no two of its edges share a point, apart from the endpoint that two adjacent edges share. The chain in figure (a) is not simple. Keeping every turning direction and changing only the length of each edge untangles the chain into a simple one. In the untangled chain each length must be between 11 and nn. Figure (b) shows one untangling of (a), described by (4, 1), (5, -1), (2, -1), (2, -1), (1, 1), (2, 0).

A chain usually has many untanglings. To pin the answer down to one, only the chain built by the rule in the output section counts as correct.

Input

The first line contains an integer nn (1≤n≤100001 \le n \le 10000), the number of edges of the chain. Each of the next nn lines contains two integers separated by a single space: the length lkl_k of eke_k (1≤lk≤100001 \le l_k \le 10000) and the turning direction tkt_k from eke_k to ek+1e_{k+1}. For 1≤k<n1 \le k < n, tkt_k is 11 for a left turn and −1-1 for a right turn, and tn=0t_n = 0.

Output

Print the nn edge lengths of the chain built by the rule below, in the input order, on one line separated by single spaces. The turning directions match the input, so do not print them.

Start at the origin (0,0)(0,0), which is the only visited point at the beginning. For k=1k = 1 up to nn in order, fix the endpoint of eke_k as follows. The visited points are the origin together with the endpoints of e1e_1 through ek−1e_{k-1}.

  • If eke_k goes right, the x coordinate of its endpoint is 11 more than the largest x coordinate among the visited points.
  • If eke_k goes left, the x coordinate of its endpoint is 11 less than the smallest x coordinate among the visited points.
  • If eke_k goes up, the y coordinate of its endpoint is 11 more than the largest y coordinate among the visited points.
  • If eke_k goes down, the y coordinate of its endpoint is 11 less than the smallest y coordinate among the visited points.

The length of eke_k is the distance between its start point and its endpoint. The chain built this way is always simple, and every length is between 11 and nn. The rule does not use the lengths lkl_k given in the input.

Examples2

  1. Example 1

    Input
    6
    4 1
    5 -1
    2 -1
    2 -1
    4 1
    5 0
    
    Expected output
    1 1 1 2 3 1
    
  2. Example 2

    Input
    6
    3 1
    3 1
    2 1
    4 1
    1 1
    3 0
    
    Expected output
    1 1 2 2 3 3