Under construction

Still being built. Things may change

Spider Walk

Time limit6sMemory limit1024 MB

Summary
Given n strands and m bridges in a spider web, find for each start strand the fewest bridges to add so Charlotte's autopilot walk ends at strand s.
Level

Medium7 of 10

Topics
Sorting, Graph, Simulation, Greedy
Solved
No attempts yet

Problem

Charlotte the spider sits at the center of her spiderweb. The web consists of silken straight strands that go from the center to the outer boundary. Charlotte's web also has bridges, each connecting two adjacent strands. The two endpoints of a bridge are always the same distance from the center.

When Charlotte finishes a late-night feast at the center and wants to retreat to a corner, she walks to the edge on autopilot. She picks a starting strand and walks along it until she meets the first bridge on that strand. She crosses the bridge onto the other strand and keeps walking outward until she meets another bridge. She crosses that bridge too and repeats the process. When no bridges remain on the current strand, she walks to the end of it. Charlotte must cross all the bridges she meets. The figure shows one path Charlotte could take.

Figure I.1: The path starting from strand 4.

Charlotte's favorite corner to sleep in during the daytime is at the end of strand ss. For each possible starting strand, she wants to know the minimum number of bridges to add to the original web so that she ends at ss. Charlotte can add a bridge at any point along a strand, as long as the added bridge does not touch any other bridge. The two endpoints of an added bridge must be the same distance from the center, and the bridge must connect two adjacent strands.

Input

The first line has three integers nn, mm, and ss, where 3≤n≤2000003 \le n \le 200000 is the number of strands, 0≤m≤5000000 \le m \le 500000 is the number of bridges, and 1≤s≤n1 \le s \le n is Charlotte's favorite strand. Strands are labeled from 1 to nn in counterclockwise order. Each of the remaining mm lines contains two integers dd and tt describing a bridge, where 1≤d≤1091 \le d \le 10^9 is the bridge's distance from the center and 1≤t≤n1 \le t \le n is the first strand of the bridge in counterclockwise order. If 1≤t<n1 \le t < n, the bridge connects strands tt and t+1t+1. If t=nt = n, the bridge connects strands 11 and nn. All bridge distances dd are distinct.

Output

Output nn lines. The ii-th line is the minimum number of bridges Charlotte needs to add so that she ends at strand ss after walking on autopilot from strand ii.

Examples2

  1. Example 1

    Input
    7 5 6
    2 1
    4 3
    6 3
    8 7
    10 5
    
    Expected output
    2
    1
    1
    1
    0
    1
    2
    
  2. Example 2

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