Club Room Project (Small)
Time limit1sMemory limit512 MB
Given N rooms in a row and M wall-breaking actions, count how many rooms remain after all actions merge neighboring rooms.
- Level
Easy3 of 10
- Topics
- Union-find, Implementation
- Solved
- No attempts yet
Problem
Byeongchan wanted a club room, so he asked the LINK office and got a chance to receive one of rooms. The building is a single straight corridor with rooms in a row. The leftmost room is numbered 1, the numbers grow by 1 toward the right, and the rightmost room is numbered . Between every pair of neighbouring rooms there is one wall that separates them.
Plenty of other people want a club room too. Rooms were plentiful, so Byeongchan was not worried.
Then the Big Jongbin Villain showed up and started knocking down the walls of the building. The villain follows these rules.
- The villain picks two rooms with , then knocks down every wall that lies between room and room .
- Once the wall between two rooms is gone, the two rooms become one room.
- If a wall is already down, the villain passes over it and moves on to the next wall.
- The villain does not want the building to collapse, so the wall on the left of room 1 and the wall on the right of room , the two walls that face the outside, are never touched.
As the number of rooms dropped, Byeongchan grew nervous. To work out his chance of getting a club room he first needs the number of rooms that are left. Given the number of actions and the starting number of rooms , find how many rooms remain after every action is done.
Input
The first line contains a positive integer (), the starting number of rooms. The second line contains a non-negative integer (), the number of actions. Each of the next lines describes one action with two positive integers and (), meaning that every wall between room and room is knocked down.
The villain is clumsy, so the same action may appear several times.
Output
Print the number of rooms that remain after every action is done, on one line.
Hint
Take with the actions and . The first action merges rooms 1 and 2, leaving (1, 2), (3), (4), (5). The second action merges rooms 2, 3 and 4, leaving (1, 2, 3, 4) and (5). So 2 rooms remain.