Find Direct Supervisors

Time limit2sMemory limit128 MB

Summary
Given employees with salary and tenure, build a supervisor hierarchy by nearest dominating employee and answer queries for direct supervisor and subtree subordinate counts.
Level

Medium6 of 10

Topics
Sorting, Tree, Implementation
Solved
No attempts yet

Problem

At World Company, an employee who has worked for a long time can be promoted and receive a higher salary.

For an employee, a supervisor is an employee who earns more and has worked at least as long. Among all such employees, the direct supervisor is the one with the smallest salary.

For a direct supervisor, that employee is a direct subordinate. Direct subordinates and all subordinates below them are counted as that supervisor's subordinates. An employee who is not anyone's supervisor has no subordinates.

Using this rule for supervisor-subordinate relationships, answer each query with the employee's direct supervisor ID and total number of subordinates.

Input

The first line contains two integers n (1 ≤ n ≤ 30,000) and m (1 ≤ m ≤ 200), where n is the number of employees and m is the number of queried employees.

Each of the next n lines contains three integers a, b, and c. Here a (1 ≤ a ≤ 2,147,483,647) is the employee ID, b (0 ≤ b ≤ 10,000,000) is the salary, and c (1,000,000 ≤ c ≤ 2,500,000) is the work time in minutes. Employee IDs are distinct.

Each of the next m lines contains the ID of one queried employee. Every queried ID appears among the employee records.

Output

For each queried employee, in the same order as the input, print one line containing the direct supervisor's ID and the total number of subordinates, separated by a space.

If the queried employee is the top employee, meaning the employee with both the longest work time and the highest salary, print 0 instead of a direct supervisor ID.

Examples2

  1. Example 1

    Input
    3 3
    123456 14323 1700000
    123458 41412 1900000
    123457 15221 1800000
    123456
    123458
    123457
    
    Expected output
    123457 0
    0 2
    123458 1
    
  2. Example 2

    Input
    4 4
    200002 12234 1832001
    200003 15002 1745201
    200004 18745 1883410
    200001 24834 1921313
    200004
    200002
    200003
    200001
    
    Expected output
    200001 2
    200004 0
    200004 0
    0 3