fork(1) download
  1.  
  2. #include <iostream>
  3. using namespace std;
  4. int main()
  5. {
  6. int n;
  7. cin>>n;
  8. long long x;
  9. cin >>x;
  10. long long result = 0;
  11.  
  12. long long l;
  13.  
  14. long long r;
  15.  
  16.  
  17.  
  18.  
  19. long long pointer= 0;
  20. for(int i= 0; i < n; i++){
  21. cin >>l;
  22. cin >>r;
  23.  
  24. if((l-pointer)%x==0){
  25. result+=x;
  26. }
  27. else{
  28. result += (l-pointer)%x;
  29. result += r-l;
  30. }
  31. pointer = r;
  32.  
  33.  
  34. }
  35. if (x == 1)
  36. result+=1;
  37. cout<<result;
  38. return 0;
  39. }
Success #stdin #stdout 0s 4524KB
stdin
2 3
4 7
10 12
stdout
7