Algorithm
Code Examples
#1 Code Example with C++ Programming
Code -
C++ Programming
#include <bits/stdc++.h>
using namespace std;
int n, x, l, r;
int main() {
cin >> n >> x;
int res = 0;
for(int i = 0, cur = 1; i < n; ++i) {
cin >> l >> r;
while(cur + x <= l)
cur += x;
res += (r - cur + 1);
cur = r + 1;
}
cout << res << endl;
return 0;
}
Copy The Code &
Try With Live Editor
Input
2 3
5 6
10 12
5 6
10 12
Output
6
Demonstration
Codeforces Solution-Watching a movie-Solution in C, C++, Java, Python