Time-Traveling Vitaro
Time limit3sMemory limit512 MB
Cities are joined in a line by roads that open only in given time windows; the task answers minimum time-rewind uses for trips while windows change.
- Level
Hard8 of 10
- Topics
- Segment tree, Intervals, Greedy
- Solved
- No attempts yet
Problem
Beverland has cities numbered 1 through . The -th road () connects city and city in both directions. A day in Beverland is divided into 1 000 000 000 unit times, called cho. The moment cho after the start of a day is called time . Crossing a road takes 1 cho. The -th road can be crossed only between time and time . To be exact, to cross the -th road, one must leave city or city at a time with and arrive at the other city at time .
Vitaro is an ordinary beaver who lives in Beverland. He was often late, and while trying to fix that, he gained the ability to go back in time. Each use of the ability sends him back 1 cho. He cannot go back to yesterday. If he uses the ability between time 0 and time 1, he returns to time 0. He can use this ability only while he is in a city. His position does not change when he uses the ability.
Using the ability makes Vitaro tired. To find a way to move with the fewest uses of the ability, Vitaro ran thought experiments. In step of the experiments, he does one of the following:
- Changes the times at which the -th road can be crossed. After the change, the -th road can be crossed only between time and time .
- Starts in city at time and finds the minimum number of uses of the ability needed to reach city at time .
Input
The first line contains and . Each of the next lines contains and . Then queries follow, one per line. Each query has one of two forms.
- If , the query consists of four integers , , , .
- If , the query consists of five integers , , , , .
Output
For each query with , print the minimum number of uses of the ability, one per line, in the order the queries appear.
Constraints
- .
- .
- ().
- ().
- (, ).
- (, ).
- (, ).
- (, ).
- (, ).
- (, ).