Army Buddies
Time limit1sMemory limit128 MB
After each loss report removes living soldiers L through R, print the nearest surviving neighbors on both sides, or * when none exists.
- Level
Medium4 of 10
- Topics
- Union-find, Linked list, Implementation
- Solved
- No attempts yet
Problem
Soldiers stand in a single line, numbered from to from left to right. Besides protecting himself and attacking the enemy, each soldier must also protect his two nearest neighbors — one immediately to his left and one immediately to his right. These two neighbors are called his buddies. The leftmost soldier has no left buddy, and the rightmost soldier has no right buddy.
If a soldier's left or right buddy is killed, then the next living soldier in that direction becomes his new buddy.
As the battle rages, soldiers are killed. Each time losses occur, the army's information system must tell the soldiers who their new buddies are. Each loss report describes a group of contiguous soldiers that were just killed.
For each loss report, write a program that prints the new buddies formed by removing that group from the line: the first surviving soldier immediately to the left of the removed group, and the first surviving soldier immediately to its right.
Input
The input consists of several test cases.
The first line of each test case contains two integers and : the number of soldiers in the line and the number of loss reports (). Soldiers are numbered from to by their position, where is the leftmost soldier and is the rightmost.
Each of the next lines describes one loss report with two integers and (), meaning that soldiers through were just killed. You may assume that all of these soldiers were alive up to that moment.
The last test case is followed by a line containing two zeros.
Output
For each test case, output lines.
On the -th line, for the -th loss report L R, print the newly formed buddies after removing soldiers through : the first surviving soldier to the left of and the first surviving soldier to the right of , separated by a single space. If there is no surviving soldier in a direction, print an asterisk * in its place.
After each test case, print a line containing a single hyphen -.