A. Watching a movie
You have decided to watch the best moments of some movie. There are two buttons on your player:
- Watch the current minute of the movie. By pressing this button, you watch the current minute of the movie and the player automatically proceeds to the next minute of the movie.
- Skip exactly x minutes of the movie (x is some fixed positive integer). If the player is now at the t-th minute of the movie, then as a result of pressing this button, it proceeds to the minute (t + x).
Initially the movie is turned on in the player on the first minute, and you want to watch exactly n best moments of the movie, the i-th best moment starts at the li-th minute and ends at the ri-th minute (more formally, the i-th best moment consists of minutes: li, li + 1, ..., ri).
Determine, what is the minimum number of minutes of the movie you have to watch if you want to watch all the best moments?
The first line contains two space-separated integers n, x (1 ≤ n ≤ 50, 1 ≤ x ≤ 105) — the number of the best moments of the movie and the value of x for the second button.
The following n lines contain the descriptions of the best moments of the movie, the i-th line of the description contains two integers separated by a space li, ri (1 ≤ li ≤ ri ≤ 105).
It is guaranteed that for all integers i from 2 to n the following condition holds: ri - 1 < li.
Output a single number — the answer to the problem.
每次第一题都是这种过程题,要仔细想清楚包不包括现在,+1
#include<iostream>
using namespace std;
int main()
{
int n,x,a,b;
cin>>n>>x;
int begin=1;
int sum=0;
for(int i=0;i<n;i++)
{
cin>>a>>b;
while(a-begin>=x){begin+=x;} //没有跳过就继续
sum+=b-begin+1; //不能再跳了,就直接看到b处
begin=b+1;
}
cout<<sum<<endl;
return 0;
}