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 ;
}

cf499A-Watching a movie的更多相关文章

  1. 题解 CF499A 【Watching a movie】

    题意 看一部电影,从第一分钟开始看,你每次可以按下快进键,快进x分钟,电影有n个精彩片段,问看到所有精彩片段至少需要看多少分钟电影.(数据保证能看到所有精彩片段) 我们可以发现答案其实 就是间隔时间% ...

  2. Codeforces Round #219 (Div. 1) C. Watching Fireworks is Fun

    C. Watching Fireworks is Fun time limit per test 4 seconds memory limit per test 256 megabytes input ...

  3. Codeforces Round #219 (Div. 2) E. Watching Fireworks is Fun

    http://codeforces.com/contest/373/problem/E E. Watching Fireworks is Fun time limit per test 4 secon ...

  4. 6656 Watching the Kangaroo

    6656 Watching the KangarooDay by day number of Kangaroos is decreasing just liketiger, whale or lion ...

  5. Uva 10339 - Watching Watches【数论,暴力】

    题目链接:10339 - Watching Watches 题意:两个时钟,一个每天慢a秒,一个每天慢b秒,问两钟重新相遇的时刻 1圈有12 * 60 * 60秒,然后1圈 / abs(a - b), ...

  6. Error watching file for changes: EMFILE

    运行reactnative项目时在编译过程中报错 Error watching file for changes: EMFILE 故障原因: 是升级后watchman不可用了,需要重装watchman ...

  7. 每日英语:In Digital Era, What Does 'Watching TV' Even Mean?

    We spend a full five hours and 16 minutes a day in front of a screen, and that's without even turnin ...

  8. CF372C Watching Fireworks is Fun(单调队列优化DP)

    A festival will be held in a town's main street. There are n sections in the main street. The sectio ...

  9. Django报错Watching for file changes with StatReloader

    Django项目运行时出现:Watching for file changes with StatReloader错误 原因:环境里的django或者python的版本有问题 解决方案:升级或者降级D ...

  10. F - Watching Fireworks is Fun

    C. Watching Fireworks is Fun 题目大意: 一个城镇有n个区域,从左到右1-n,每个区域之间距离1个单位距离.节日中有m个烟火要放,给定放的地点a[ i ].时间t[ i ] ...

随机推荐

  1. nginx 支持ie 6 等低版本https 的配置

    nginx 配置 https 支持ie6 等低版本(主要是加密套件的问题) server { listen 443 ssl; server_name itapiway.demo.com; ssl_ce ...

  2. ThinkPHP5 为什么取消了单字母函数?

    ThinkPHP5 为什么取消了单字母函数? 更容易理解. 理加规范. 个人喜好. 比如 TPShop 也是用 ThinkPHP5 又加回单字母函数. [话唠]教练,我想做菜-长沙 2018/10/8 ...

  3. luvcview,使用mplayer查看摄像头和luvcview保存YUV图像视频的播放(转)

    luvcview,使用mplayer查看摄像头和luvcview保存YUV图像视频的播放 在mplayer中查看摄像头,可使用如下命令:mplayer tv:// -tv driver=v4l2:in ...

  4. (转)Android内存泄漏分析及调试

      http://blog.csdn.net/gemmem/article/details/13017999 此文承接我的另一篇文章:Android进程的内存管理分析  首先了解一下dalvik的Ga ...

  5. Java NIO简单介绍(二)

    上一篇<NIO简单介绍(一)>中讲解了NIO中本地IO相关的内容,这篇重点介绍的NIO的非阻塞式网络通信 一.阻塞与非阻塞 传统的 IO 流都是阻塞式的.也就是说,当一个线程调用 read ...

  6. 安装MySQL-python 的问题

    安装MySQL-python 的问题 1.CentOS下载mysql-devel安装 yum install mysql-devel 2.Ubuntu下不叫mysql-devel,而是叫libmysq ...

  7. 20165226 实验四 Android程序设计

    实验四 Android程序设计 实验目的 一.Android Studio的安装测试 二.Activity测试 三.UI测试 四.布局测试 五.事件处理测试 实验内容及步骤 (一)Android St ...

  8. easyui datagrid 格式化列显示两位小数、千分位

    { field: , formatter: function (value, row, index) { if (row != null) { ); } } }, //二位小数.千分位 { field ...

  9. 【洛谷】P2694 接金币(排序)

    题目描述 在二维坐标系里,有N个金币,编号0至N-1.初始时,第i个金币的坐标是(Xi,Yi).所有的金币每秒向下垂直下降一个单位高度,例如有个金币当前坐标是(xf, yf),那么t秒后金币所在的位置 ...

  10. 11g的新特性:SQL Plan Management(SPM)

    Oracle11g中,Oracle提供dbms_spm包来管理SQL Plan,SPM是一个预防机制,它记录并评估sql的执行计划,将已知的高效的sql执行计划建立为SQL Plan Baseline ...