//In The Name Of Allah
#include<bits/stdc++.h>
using namespace std;
typedef long long ll;
#define pb push_back
#define F first
#define S second
#define pll pair<ll, ll>
//#pragma GCC optimize("Ofast")
const int maxn = 1e6 + 5;
const ll inf = 1e18 + 5;
bool cmp(pll x, pll y)
{
if(x.F != y.F)
return x.F < y.F;
return x.S > y.S;
}
ll n, A, B, g, ans;
vector<pll> seg;
int main()
{
ios::sync_with_stdio(0);cin.tie(0);cout.tie(0);
cin >> n >> A >> B;
g = A / __gcd(A, B + 1);
if(inf / g < B)
g = inf;
else
g = g * B;
//cout << g << '\n';
for(int i = 0; i < n; i++)
{
int l, r;
cin >> l >> r;
if(r - l + 1 >= g)
seg.pb({0, g - 1});
else if(l % g <= r % g)
seg.pb({l % g, r % g});
else
{
seg.pb({l % g, g - 1});
seg.pb({0, r % g});
}
}
sort(seg.begin(), seg.end(), cmp);
for(int i = 1; i < seg.size(); i++)
if(seg[i].F <= seg[i - 1].S)
{
seg[i].F = min(seg[i].F, seg[i - 1].F);
seg[i].S = max(seg[i].S, seg[i - 1].S);
seg[i - 1] = {1, 0};
}
for(auto x : seg)
ans += x.S - x.F + 1;
//sort(seg.begin(), seg.end());
cout << ans << '\n';
}
/*
3 3 3
4 4
7 9
17 18
3 5 10
1 20
50 68
89 98
2 16 13
2 5
18 18
*/
Compilation message
strange_device.cpp: In function 'int main()':
strange_device.cpp:51:22: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
51 | for(int i = 1; i < seg.size(); i++)
| ~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Incorrect |
2 ms |
1028 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Incorrect |
1 ms |
384 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Incorrect |
1 ms |
364 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Incorrect |
202 ms |
33380 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Incorrect |
202 ms |
33380 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Incorrect |
202 ms |
33380 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Incorrect |
18 ms |
4580 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
364 KB |
Output is correct |
2 |
Incorrect |
2 ms |
1028 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |