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
  1. 2 3
    5 6
    10 12
output
  1. 6
input
  1. 1 1
    1 100000
output
  1. 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.

代码:

  1. #include <fstream>
  2. #include <iostream>
  3.  
  4. using namespace std;
  5.  
  6. int main(){
  7. //freopen("D:\\input.in","r",stdin);
  8. //freopen("D:\\output.out","w",stdout);
  9. int n,x,l,r,t=,ans=;
  10. scanf("%d%d",&n,&x);
  11. for(int i=;i<=n;i++){
  12. scanf("%d%d",&l,&r);
  13. ans+=(l-t)%x+r-l+;
  14. t=r+;
  15. }
  16. printf("%d\n",ans);
  17. return ;
  18. }

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. 什么是虚拟环境、为什么使用虚拟环境、Anaconda创建、激活、退出、删除虚拟环境

    一.虚拟环境 virtual environment 它是一个虚拟化,从电脑独立开辟出来的环境.通俗的来讲,虚拟环境就是借助虚拟机docker来把一部分内容独立出来,我们把这部分独立出来的东西称作“容 ...

  2. unity导入3dsMax源文件.max

    https://blog.csdn.net/qq_28002559/article/details/53693621 首先unity导入3dsMax文件为什么要使用.max而不用.fbx,因为我不是做 ...

  3. <>这个符号表示泛型的意思

    <>这个符号表示泛型的意思,泛型不是类而是类的模版 为什么是模版 这就与这个T有关了,如List<T> 这个T只是一个占位符,可以代表任意类型如 List<string& ...

  4. Oracle12c之 CDB数据库中数据字典架构

    数据字典就是元数据的集合,比如创建的表,列,约束,触发器等等这些都是元数据,需要保存到数据库中.除此之外,Oracle自身的一些数据库对象,如目录,PL/SQL代码等等这些都是元数据,都需要存放在数据 ...

  5. oracle之 12.1.0.1.0 C 在 linux 7 上安装报错处理

    环境说明:-- os[root@host-172-16-3-132 ~]# cat /etc/redhat-release CentOS Linux release 7.2.1511 (Core) - ...

  6. vs2017 xamarin导入jar,SO文件的问题

    最近要弄用vs弄个安卓的系统,因为要使用硬件,所以要引进jar,SO文件 导入jar文件很顺利,具体步骤我也是在网上找的这里给个链接 http://www.2cto.com/kf/201604/502 ...

  7. SharePoint2010基于表单验证方法总结(转载)

    系统环境: win2008r2+ sql2008r2 +Visual Studio2010+sharepoint 2010 A.如果已经建立了web application  例如名字为: http: ...

  8. 常见企业IT支撑【8、端口回流问题】

    端口回流故障场景,常见于内网启用了Server服务器,使用出口路由设备的外网口NAT映射了一个公网地址,域内内网主机访问了这个公网地址,访问不通. 故障容易出现在办公网内的带有对外Server测试环境 ...

  9. 理解加密算法——创建CA机构,签发证书并开始TLS通信

    1 不安全的TCP通信 普通的TCP通信数据是明文传输的,所以存在数据泄露和被篡改的风险,我们可以写一段测试代码试验一下,NODE.JS代码: TCP Server: const net=requir ...

  10. struts2学习(5)拦截器简介以及例子执行过程

    一.拦截器简介: 二.Struts2预定义拦截器&拦截器栈 在执行action之前和之后,拦截器进行了操作: 比如struts-default.xml中就有很多预定义的拦截器:   拦截器栈: ...