B. Draw!
You still have partial information about the score during the historic football match. You are given a set of pairs ( a i , b i a_i,b_i ai,bi), indicating that at some point during the match the score was " a i a_i ai: b i b_i bi". It is known that if the current score is « x x x: y y y», then after the goal it will change to “ x + 1 x+1 x+1: y y y” or “ x x x: y + 1 y+1 y+1”. What is the largest number of times a draw could appear on the scoreboard?
The pairs “ a i a_i ai: b i b_i bi” are given in chronological order (time increases), but you are given score only for some moments of time. The last pair corresponds to the end of the match.
Input
The first line contains a single integer n n n (1≤ n n n≤10000) — the number of known moments in the match.
Each of the next n n n lines contains integers a i a_i ai and b i b_i bi (0≤ a i a_i ai, b i b_i bi≤109), denoting the score of the match at that moment (that is, the number of goals by the first team and the number of goals by the second team).
All moments are given in chronological order, that is, sequences x i x_i xi and y j y_j yj are non-decreasing. The last score denotes the final result of the match.
Output
Print the maximum number of moments of time, during which the score was a draw. The starting moment of the match (with a score 0:0) is also counted.
Examples
input
3
2 0
3 1
3 4
output
2
input
3
0 0
0 0
0 0
output
1
input
1
5 4
output
5
Note
In the example one of the possible score sequences leading to the maximum number of draws is as follows: 0:0, 1:0, 2:0, 2:1, 3:1, 3:2, 3:3, 3:4.
Reference Code
#include <cstdio>
#include <algorithm>
#define min std::min
int main(){
int n;
scanf("%d",&n);
int x1=0,y1=0,res=1;
for (int i=0;i<n;++i){
int x2,y2;
scanf("%d%d",&x2,&y2);
if (x2==x1&&y2==y1) continue;
if (x1>y1){
if (x2>y2){
if (y2>=x1) res+=y2-x1+1;
}
else res+=x2-x1+1;
}
else if (x1<y1){
if (x2<y2){
if (x2>=y1) res+=x2-y1+1;
}
else res+=y2-y1+1;
}
else res+=min(x2,y2)-x1;
x1=x2,y1=y2;
}
printf("%d\n",res);
}
Tips
没什么好想法,就是疯狂分类,反正出现的情况也不多。