http://codeforces.com/problemset/problem/499/A

A. Watching a movie
 

You have decided to watch the best moments of some movie. There are two buttons on your player:

  1. 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.
  2. 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?

Input

The first line contains two space-separated integers nx (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 liri (1 ≤ li ≤ ri ≤ 105).

It is guaranteed that for all integers i from 2 to n the following condition holds: ri - 1 < li.

Output

Output a single number — the answer to the problem.

Sample test(s)
input
2 3
5 6
10 12
output
6
input
1 1
1 100000
output
100000
Note

In the first sample, the player was initially standing on the first minute. As the minutes from the 1-st to the 4-th one don't contain interesting moments, we press the second button. Now we can not press the second button and skip 3 more minutes, because some of them contain interesting moments. Therefore, we watch the movie from the 4-th to the 6-th minute, after that the current time is 7. Similarly, we again skip 3 minutes and then watch from the 10-th to the 12-th minute of the movie. In total, we watch 6 minutes of the movie.

In the second sample, the movie is very interesting, so you'll have to watch all 100000 minutes of the movie.

代码:

 #include <fstream>
#include <iostream> using namespace std; int main(){
//freopen("D:\\input.in","r",stdin);
//freopen("D:\\output.out","w",stdout);
int n,x,l,r,t=,ans=;
scanf("%d%d",&n,&x);
for(int i=;i<=n;i++){
scanf("%d%d",&l,&r);
ans+=(l-t)%x+r-l+;
t=r+;
}
printf("%d\n",ans);
return ;
}

最新文章

  1. [Unity2D]2D Mobile Joystick
  2. android 布局之滑动探究 scrollTo 和 scrollBy 方法使用说明
  3. freeCodeCamp:Missing letters
  4. pyunit实现数据测试框架
  5. CentOS中JAVA_HOME的环境变量设置
  6. 软硬结合的可穿戴式app
  7. STL中copy算法
  8. 查看文章strncpy()功能更好的文章
  9. aforge通过角点匹配图片相似度
  10. java数据结构之链表的实现
  11. Openstack Swift 原理、架构与 API 介绍
  12. [翻译]怎么写一个React组件库(二)
  13. pip install python 如何快速安装模块
  14. Spring MVC(一)五大核心组件和配置
  15. UOJ 7 NOI2014 购票
  16. python中并发编程基础1
  17. Python编程-从入门到实践 Eric Matthes 著 袁国忠 译 - - 第二章 动手试一试
  18. D - Equation Again
  19. 2018.11.17 bzoj4259: 残缺的字符串(fft)
  20. div+css ie6图片之间有间隙的问题

热门文章

  1. pandas 之 set_index
  2. JavaScript中的函数(一)
  3. Docker生态会重蹈Hadoop的覆辙吗?
  4. Oracle12c之 CDB数据库中数据字典架构
  5. SharePoint2013集成Exchange之任务同步
  6. 确保nginx安全的10个技巧
  7. NumberUtils、ArrayUtils和RandomUtils工具类用法
  8. bootstrap的折叠组件1
  9. docker 学习(十一) 镜像常用命令
  10. [知识整理]Linux系统WIFI知识的一些整理