International Event
Time limit5sMemory limit128 MB
Find the minimum distance for a robot starting and ending at A to move every flag along a line from its old pole to a pole requesting that nation.
- Level
Medium7 of 10
- Topics
- Greedy, Prefix sum, Intervals
- Solved
- No attempts yet
Problem
A large international event is held every year. A long, wide street runs in front of the event hall. When the event opens, the organizers put up flagpoles along the street. They raise national flags on the flagpoles and rearrange them every year. This has become the symbol of the event.
The flagpoles stand on a line, and flagpole is at location . The locations are integers and all distinct. Every flag raised on a flagpole belongs to a nation in the set . Last year the flag of nation was raised on flagpole . On the first day of the new year, the nation whose flag is newly raised on flagpole is decided.
The flag on flagpole has to change from to . A robot does this work by lowering and raising flags. can carry an almost unlimited number of flags. It may lower the flag of nation on flagpole and carry it. It may then move to the location of a flagpole with and raise the flag there. This work is always possible because the following condition holds.
For each nation , the number of flagpoles with equals the number of flagpoles with .
There is a special location , different from every , where the robot must always start and finish. No flagpole stands at . So starts at , delivers every flag of nation to a flagpole with , and ends at .
Given the location , the locations of the flagpoles, and the nations and of each flagpole , write a program that computes the minimum travel distance of the robot to deliver all the flags.
Figure 1 shows six points representing the flagpoles and the special point where the robot starts and finishes. The nations of the flags correspond to the integers in the set . Each point carries a pair of integers , where is the nation of last year's flag and is the nation of the new year's flag. The arrows show a movement of that makes the travel distance as short as possible. moves right from to 5 and loads the flag of nation 2 from the point at 5. Moving left from 5 to 1, it delivers the flags of the points at 3 and 5 to the points at 1 and 2. Then it moves right from 1 to 7 and delivers the flags of the points at 1, 2, and 6 to the points at 3, 5, and 7. Finally it moves left from 7 to and delivers the flag of the point at 7 to the point at 6 on the way. The travel distance of is 14.

Figure 1.
Input
Your program reads from standard input. The input consists of test cases. The first line of the input contains the number of test cases . Each test case starts with a line containing an integer (), the number of points representing the flagpoles, which does not count . The second line contains an integer (), the coordinate of . The third line contains an integer (), which stands for the set of nations . For each integer , at least one flag of nation is raised on a flagpole. The -th of the following lines contains three integers , , and : the coordinate of flagpole , the nation of its flag last year, and the nation of its flag in the new year. Here with , and with . All are distinct and are given in increasing order.
Output
Your program writes to standard output. Print exactly one line for each test case. The line contains the minimum travel distance of the robot .