Under construction

Still being built. Things may change

Tallest Cow

Interview

Time limit1sMemory limit128 MB

Summary
Given the tallest cow's height and index plus pairs where cow a sees cow b, find each cow's maximum possible height consistent with all observations.
Level

Medium5 of 10

Topics
Greedy, Prefix sum, Hash map, Implementation
Solved
No attempts yet

Problem

Farmer John's NN cows (1≤N≤100001 \le N \le 10000), numbered 11 through NN, are standing in a line. Every cow has a positive integer height, most of which are secret. You are told only the height HH (1≤H≤10000001 \le H \le 1000000) of the tallest cow and its index II.

You are also given RR pieces of information (0≤R≤100000 \le R \le 10000) of the form "cow aa sees cow bb". This means cow bb is at least as tall as cow aa, and every cow strictly between aa and bb is strictly shorter than cow aa.

For each cow from 11 to NN, determine its maximum possible height so that all of the given information still holds. It is guaranteed that all constraints can be satisfied simultaneously.

Input

  • Line 1: Four space-separated integers NN, II, HH, and RR.
  • Lines 2 to R+1R+1: Two distinct space-separated integers AA and BB (1≤A,B≤N1 \le A, B \le N), meaning cow AA sees cow BB.

Output

  • Print NN lines. Line ii contains the maximum possible height of cow ii.

Examples1

  1. Example 1

    Input
    9 3 5 5
    1 3
    5 3
    4 3
    3 7
    9 8
    
    Expected output
    5
    4
    5
    3
    4
    4
    5
    5
    5