P1496 Burning of Red Cliffs

Background

After pacifying the north, in AD 208, Cao Cao led a massive army south to attack Liu Biao. Before his forces reached Jingzhou, Liu Biao had already died of illness. His son Liu Cong, terrified by the might of Cao Cao’s army, sent envoys to surrender first. Sun Quan appointed Zhou Yu as the commander-in-chief, assigned him 30,000 marines, and ordered him to join forces with Liu Bei to resist Cao Cao. In the eleventh month of deep winter, the weather suddenly turned warm and a southeast wind began to blow. Unexpectedly, when the fleet of Eastern Wu was about two li away from the north bank, the front ten large ships suddenly caught fire at the same time. The fire rode the wind, and the wind fed the fire. The ten fire ships, like ten fire dragons, charged into Cao Cao’s naval camp. The ships there were all crowded together and had no way to evade, and they quickly caught fire. In the blink of an eye, the whole place was a sea of flames. Cao Cao, in a panic, summoned you and ordered you to plunge into the sea of fire to compute the total length of the ships on the chained line that are on fire.

Description

Given the start and end of each burning segment, please compute the total length of the burning positions.

Input Format

The first line contains an integer $n$, indicating the number of fire reports. Then follow $n$ lines; each line contains two integers $a, b$, denoting the start and end of a burning segment (Note: left-closed, right-open).

Output Format

Output a single line with one integer representing the answer.

Explanation/Hint

Constraints For all test points, it is guaranteed that $1 \leq n \leq 2 \times 10^4$, $-2^{31} \leq a < b \lt 2^{31}$, and the answer is less than $2^{31}$. Translated by ChatGPT 5