Advertisement
Guest User

Untitled

a guest
May 27th, 2015
216
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 0.54 KB | None | 0 0
  1. import java.util.*;
  2. public class main {
  3. public static void main(String args[])
  4. {
  5.     Scanner s=new Scanner(System.in);
  6.     int n=s.nextInt();
  7.     int x=s.nextInt();
  8.     int []start=new int[n];
  9.     int []end=new int[n];
  10.     for(int i=0;i<n;i++)
  11.     {
  12.         start[i]=s.nextInt();
  13.         end[i]=s.nextInt();
  14.     }
  15.     long sum=0;
  16.     for(int i=0;i<n;i++)
  17.     {
  18.         int diff=0;
  19.         if(i==0)
  20.         {
  21.             diff=start[i]-1;
  22.         }
  23.         else
  24.         {
  25.             diff=start[i]-end[i-1]-1;
  26.         }
  27.         int rem=diff%x;
  28.         int magic=end[i]-start[i]+1;
  29.         sum=sum+rem+magic;
  30.        
  31.        
  32.     }
  33.     System.out.println(sum);
  34. }
  35. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement