#include<bits/stdc++.h>
#define endl '\n'
using namespace std;
typedef long long ll;
void speed()
{
ios_base::sync_with_stdio(false);
cin.tie(NULL);
cout.tie(NULL);
}
const int maxn = 1e6 + 10;
int n;
ll l[maxn], r[maxn];
ll A, B;
struct point
{
ll x, t;
point(ll _x = 0, ll _t = 0)
{
x = _x;
t = _t;
}
bool operator < (const point &p) const
{
if (x == p.x)
return t < p.t;
return x < p.x;
}
};
vector < point > vec;
void add_segment(int l, int r)
{
vec.push_back(point(l, 1));
vec.push_back(point(r, -1));
}
void solve()
{
cin >> n >> A >> B;
for (int i = 1; i <= n; i ++)
{
cin >> l[i] >> r[i];
}
ll g = __gcd(A, B + 1);
/// period is A * B / g
if ((double)(1e18) / (double)(A) < (double)(B) / (double)(g))
{
ll ans = 0;
for (int i = 1; i <= n; i ++)
{
ans = ans + (r[i] - l[i] + 1);
}
cout << ans << endl;
return;
}
long long period = (A / g) * B;
for (int i = 1; i <= n; i ++)
{
if (r[i] - l[i] + 1 >= period)
{
cout << period << endl;
return;
}
r[i] ++;
ll x = l[i] % period, y = r[i] % period;
if (x <= y)
{
add_segment(x, y);
}
else
{
add_segment(0, y);
add_segment(x, period);
}
}
sort(vec.begin(), vec.end());
ll ans = 0, cnt = 0;
for (int i = 0; i < vec.size();)
{
int r = i;
while(r < vec.size() && vec[i].x == vec[r].x)
r ++;
for (int j = i; j < r; j ++)
cnt += vec[j].t;
//cout << i << " : " << r << " " << cnt << " " << vec[r].x << " " << vec[i].x << endl;
if (cnt > 0)
ans = ans + vec[r].x - vec[i].x;
i = r;
}
cout << ans << endl;
}
int main()
{
solve();
return 0;
}
/**
1 10000 3312452154131231
1 1
*/
Compilation message
strange_device.cpp: In function 'void solve()':
strange_device.cpp:93:23: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<point>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
93 | for (int i = 0; i < vec.size();)
| ~~^~~~~~~~~~~~
strange_device.cpp:96:17: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<point>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
96 | while(r < vec.size() && vec[i].x == vec[r].x)
| ~~^~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
14 ms |
1104 KB |
Output is correct |
3 |
Correct |
14 ms |
1104 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
15 ms |
1104 KB |
Output is correct |
17 |
Correct |
145 ms |
6024 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
0 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
2 ms |
340 KB |
Output is correct |
3 |
Correct |
2 ms |
340 KB |
Output is correct |
4 |
Correct |
2 ms |
340 KB |
Output is correct |
5 |
Correct |
967 ms |
48916 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1381 ms |
48912 KB |
Output is correct |
3 |
Incorrect |
1318 ms |
49048 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1381 ms |
48912 KB |
Output is correct |
3 |
Incorrect |
1318 ms |
49048 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1381 ms |
48912 KB |
Output is correct |
3 |
Incorrect |
1318 ms |
49048 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
145 ms |
6068 KB |
Output is correct |
3 |
Incorrect |
147 ms |
6076 KB |
Output isn't correct |
4 |
Halted |
0 ms |
0 KB |
- |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
14 ms |
1104 KB |
Output is correct |
3 |
Correct |
14 ms |
1104 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
0 ms |
212 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
0 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
0 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
15 ms |
1104 KB |
Output is correct |
17 |
Correct |
145 ms |
6024 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
0 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
0 ms |
212 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
2 ms |
340 KB |
Output is correct |
26 |
Correct |
2 ms |
340 KB |
Output is correct |
27 |
Correct |
2 ms |
340 KB |
Output is correct |
28 |
Correct |
967 ms |
48916 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
1381 ms |
48912 KB |
Output is correct |
31 |
Incorrect |
1318 ms |
49048 KB |
Output isn't correct |
32 |
Halted |
0 ms |
0 KB |
- |