This page is still under construction.

Parts of this page are still being built. What you see may change.

Cities

Interview

Time limit1sMemory limit128 MB

Summary
Count for each city on a directed line how many other cities are reachable through one-way and two-way roads.
Level

Medium5 of 10

Topics
Array, Prefix sum
Solved
No attempts yet

Problem

There are nn cities lined up along a river. Between every pair of adjacent cities exactly one road is built, but not every road is two-way, so you cannot always travel from a given city to all of the others.

Given which roads are built, determine for each city how many other cities are reachable from it.

Input

The first line contains one integer nn (1≤n≤1061 \le n \le 10^6), the number of cities.

The second line contains n−1n - 1 integers d1,d2,…,dn−1d_1, d_2, \dots, d_{n-1} (0≤di≤20 \le d_i \le 2), where did_i describes the road between city ii and city i+1i+1:

  • if di=0d_i = 0, there is a one-way road from city ii to city i+1i+1;
  • if di=1d_i = 1, there is a one-way road from city i+1i+1 to city ii;
  • if di=2d_i = 2, the two cities are joined by a two-way road.

Output

Print nn integers w1,w2,…,wnw_1, w_2, \dots, w_n on a single line, separated by spaces, where wiw_i is the number of cities reachable from city ii, not counting city ii itself.

Examples3

  1. Example 1

    Input
    5
    0 2 0 1
    
    Expected output
    3 2 2 0 1
    
  2. Example 2

    Input
    4
    0 0 0
    
    Expected output
    3 2 1 0
    
  3. Example 3

    Input
    4
    1 1 1
    
    Expected output
    0 1 2 3