The pairs “ai: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.
Each of the next n lines contains integers ai and bi (0≤ai,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 xi and yj are non-decreasing. The last score denotes the final result of
#include
#include
using namespace std;
int main()
{
int n,x,y;
int sum1=0,sum2=0,ans=1;
scanf("%d",&n);
while(n--)
{
scanf("%d%d",&x,&y);
if(sum1==x&&sum2==y) continue;
else {
if(sum1==sum2) {
ans+=(min(x,y)-sum1);
}
else {//假设min(x,y)是x,x一定大于sum1,
int sum=(min(x,y)-max(sum1,sum2)+1);//但x不一定大于sum2,
if(sum>0) ans+=sum;//因此判断一下sum的值
}
sum1=x;
sum2=y;
}
}
printf("%d\n",ans);
return 0;
}