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. windows 2016 dockerfile 以及简单使用

    Docker 引擎包含用于自动创建容器映像的工具. 尽管可以使用 docker commit 命令手动创建容器映像,然而采用自动映像创建过程可获得许多好处,其中包括: 将容器映像存储为代码. 可出于维 ...

  2. 笔记:使用 stylus stylus-loader

    笔记:使用 stylus stylus-loader 安装 stylus Stylus-loader cnpm i stylus stylus-loader --save 配置 webpack.con ...

  3. ecmall 2.3.0 最新补丁20140618

    特别提示:补丁下载地址为:http://download.ecshop.com开头,该地址为ecmall下载站,如果非以http://download.ecshop.com开头,请勿下载,同时请反馈给 ...

  4. bzoj1426(洛谷4550)收集邮票

    题目:https://www.luogu.org/problemnew/show/P4550 全靠看TJ.怎么办?可是感觉好难呀. 首先设出 f[i] 为“买了 i 种,还要买到n种的期望次数”,s[ ...

  5. J2EE项目在weblogic下的改动

    1.struts所有配置文件放到classes根目录下 2〉java.lang.ClassCastException:weblogic.xml.jaxp.RegistryDocumentBuilder ...

  6. RK3288 摄像头左右镜像

    系统:Android 5.1 设置摄像头左右镜像 diff --git a/frameworks/av/services/camera/libcameraservice/api1/CameraClie ...

  7. ruby关于require路径

    ruby里面的require说明 require './aaaa' 这种方式,包含的是系统路径 相对路径得用下面的 require_relative "./xxxx" 或者使用这个 ...

  8. 关于千兆以太网芯片及VLAN浅析

    MARVEL出产的高端千兆以太网交换芯片,对每个端口支持不同的交换模式. 包括4种模式: Secure模式:所带VLAN tag必须存在于VTU表中,且入端口必须是该VLAN成员,否则丢弃报文. Ch ...

  9. java web 程序---猜数字游戏的猜了多少次的代码

    思路:用setAttribute()放 ,然后直接输出 Integer str=(Integer)session.getAttribute("count"); int num3= ...

  10. HDU 2669 Romantic(裸的拓展欧几里得)

    Romantic Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 32768/32768 K (Java/Others)Total Su ...