#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 ll maxn = 1e6 + 10;
ll n;
ll l[maxn], r[maxn];
ll A, B;
struct poll
{
ll x, t;
poll(ll _x = 0, ll _t = 0)
{
x = _x;
t = _t;
}
bool operator < (const poll &p) const
{
if (x == p.x)
return t < p.t;
return x < p.x;
}
};
vector < poll > vec;
void add_segment(ll l, ll r)
{
vec.push_back(poll(l, 1));
vec.push_back(poll(r, -1));
}
void solve()
{
cin >> n >> A >> B;
for (ll 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 (ll i = 1; i <= n; i ++)
{
ans = ans + (r[i] - l[i] + 1);
}
cout << ans << endl;
return;
}
long long period = (A / g) * B;
for (ll 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 (ll i = 0; i < vec.size();)
{
ll r = i;
while(r < vec.size() && vec[i].x == vec[r].x)
r ++;
for (ll 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:94:22: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<poll>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
94 | for (ll i = 0; i < vec.size();)
| ~~^~~~~~~~~~~~
strange_device.cpp:97:17: warning: comparison of integer expressions of different signedness: 'll' {aka 'long long int'} and 'std::vector<poll>::size_type' {aka 'long unsigned int'} [-Wsign-compare]
97 | while(r < vec.size() && vec[i].x == vec[r].x)
| ~~^~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
14 ms |
1084 KB |
Output is correct |
3 |
Correct |
14 ms |
1016 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
14 ms |
1104 KB |
Output is correct |
17 |
Correct |
143 ms |
6020 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 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 |
# |
Verdict |
Execution time |
Memory |
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 |
977 ms |
48988 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1433 ms |
48992 KB |
Output is correct |
3 |
Correct |
1438 ms |
48956 KB |
Output is correct |
4 |
Correct |
1480 ms |
49944 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1433 ms |
48992 KB |
Output is correct |
3 |
Correct |
1438 ms |
48956 KB |
Output is correct |
4 |
Correct |
1480 ms |
49944 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1456 ms |
49900 KB |
Output is correct |
7 |
Correct |
1415 ms |
49660 KB |
Output is correct |
8 |
Correct |
1412 ms |
49800 KB |
Output is correct |
9 |
Correct |
1492 ms |
49792 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1433 ms |
48992 KB |
Output is correct |
3 |
Correct |
1438 ms |
48956 KB |
Output is correct |
4 |
Correct |
1480 ms |
49944 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
153 ms |
9884 KB |
Output is correct |
7 |
Correct |
146 ms |
9796 KB |
Output is correct |
8 |
Correct |
141 ms |
9828 KB |
Output is correct |
9 |
Correct |
159 ms |
9796 KB |
Output is correct |
10 |
Correct |
142 ms |
9740 KB |
Output is correct |
11 |
Correct |
151 ms |
9712 KB |
Output is correct |
12 |
Correct |
181 ms |
9736 KB |
Output is correct |
13 |
Correct |
149 ms |
9912 KB |
Output is correct |
14 |
Correct |
139 ms |
9796 KB |
Output is correct |
15 |
Correct |
154 ms |
9788 KB |
Output is correct |
16 |
Correct |
154 ms |
9796 KB |
Output is correct |
17 |
Correct |
144 ms |
9736 KB |
Output is correct |
18 |
Correct |
1499 ms |
49688 KB |
Output is correct |
19 |
Correct |
1442 ms |
49668 KB |
Output is correct |
20 |
Correct |
1599 ms |
49840 KB |
Output is correct |
21 |
Correct |
160 ms |
9788 KB |
Output is correct |
22 |
Correct |
136 ms |
9808 KB |
Output is correct |
23 |
Correct |
476 ms |
33220 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
141 ms |
6004 KB |
Output is correct |
3 |
Correct |
141 ms |
6116 KB |
Output is correct |
4 |
Correct |
1586 ms |
49004 KB |
Output is correct |
5 |
Correct |
140 ms |
6036 KB |
Output is correct |
6 |
Correct |
149 ms |
6176 KB |
Output is correct |
7 |
Correct |
144 ms |
6008 KB |
Output is correct |
8 |
Correct |
181 ms |
6100 KB |
Output is correct |
9 |
Correct |
140 ms |
6032 KB |
Output is correct |
10 |
Correct |
145 ms |
6064 KB |
Output is correct |
11 |
Correct |
141 ms |
6112 KB |
Output is correct |
12 |
Correct |
136 ms |
6112 KB |
Output is correct |
13 |
Correct |
143 ms |
9780 KB |
Output is correct |
14 |
Correct |
1599 ms |
49720 KB |
Output is correct |
15 |
Correct |
157 ms |
9736 KB |
Output is correct |
16 |
Correct |
1387 ms |
49644 KB |
Output is correct |
17 |
Correct |
1406 ms |
49736 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
14 ms |
1084 KB |
Output is correct |
3 |
Correct |
14 ms |
1016 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
14 ms |
1104 KB |
Output is correct |
17 |
Correct |
143 ms |
6020 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 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 |
977 ms |
48988 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
1433 ms |
48992 KB |
Output is correct |
31 |
Correct |
1438 ms |
48956 KB |
Output is correct |
32 |
Correct |
1480 ms |
49944 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
1456 ms |
49900 KB |
Output is correct |
35 |
Correct |
1415 ms |
49660 KB |
Output is correct |
36 |
Correct |
1412 ms |
49800 KB |
Output is correct |
37 |
Correct |
1492 ms |
49792 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
153 ms |
9884 KB |
Output is correct |
40 |
Correct |
146 ms |
9796 KB |
Output is correct |
41 |
Correct |
141 ms |
9828 KB |
Output is correct |
42 |
Correct |
159 ms |
9796 KB |
Output is correct |
43 |
Correct |
142 ms |
9740 KB |
Output is correct |
44 |
Correct |
151 ms |
9712 KB |
Output is correct |
45 |
Correct |
181 ms |
9736 KB |
Output is correct |
46 |
Correct |
149 ms |
9912 KB |
Output is correct |
47 |
Correct |
139 ms |
9796 KB |
Output is correct |
48 |
Correct |
154 ms |
9788 KB |
Output is correct |
49 |
Correct |
154 ms |
9796 KB |
Output is correct |
50 |
Correct |
144 ms |
9736 KB |
Output is correct |
51 |
Correct |
1499 ms |
49688 KB |
Output is correct |
52 |
Correct |
1442 ms |
49668 KB |
Output is correct |
53 |
Correct |
1599 ms |
49840 KB |
Output is correct |
54 |
Correct |
160 ms |
9788 KB |
Output is correct |
55 |
Correct |
136 ms |
9808 KB |
Output is correct |
56 |
Correct |
476 ms |
33220 KB |
Output is correct |
57 |
Correct |
0 ms |
212 KB |
Output is correct |
58 |
Correct |
141 ms |
6004 KB |
Output is correct |
59 |
Correct |
141 ms |
6116 KB |
Output is correct |
60 |
Correct |
1586 ms |
49004 KB |
Output is correct |
61 |
Correct |
140 ms |
6036 KB |
Output is correct |
62 |
Correct |
149 ms |
6176 KB |
Output is correct |
63 |
Correct |
144 ms |
6008 KB |
Output is correct |
64 |
Correct |
181 ms |
6100 KB |
Output is correct |
65 |
Correct |
140 ms |
6032 KB |
Output is correct |
66 |
Correct |
145 ms |
6064 KB |
Output is correct |
67 |
Correct |
141 ms |
6112 KB |
Output is correct |
68 |
Correct |
136 ms |
6112 KB |
Output is correct |
69 |
Correct |
143 ms |
9780 KB |
Output is correct |
70 |
Correct |
1599 ms |
49720 KB |
Output is correct |
71 |
Correct |
157 ms |
9736 KB |
Output is correct |
72 |
Correct |
1387 ms |
49644 KB |
Output is correct |
73 |
Correct |
1406 ms |
49736 KB |
Output is correct |
74 |
Correct |
0 ms |
212 KB |
Output is correct |
75 |
Correct |
1 ms |
212 KB |
Output is correct |
76 |
Correct |
1 ms |
212 KB |
Output is correct |
77 |
Correct |
1 ms |
312 KB |
Output is correct |
78 |
Correct |
1 ms |
316 KB |
Output is correct |
79 |
Correct |
16 ms |
1352 KB |
Output is correct |
80 |
Correct |
1649 ms |
50740 KB |
Output is correct |
81 |
Correct |
1614 ms |
50880 KB |
Output is correct |
82 |
Correct |
1607 ms |
50696 KB |
Output is correct |
83 |
Correct |
1618 ms |
50744 KB |
Output is correct |
84 |
Correct |
1511 ms |
50804 KB |
Output is correct |
85 |
Correct |
1563 ms |
50732 KB |
Output is correct |
86 |
Correct |
538 ms |
34704 KB |
Output is correct |
87 |
Correct |
1 ms |
212 KB |
Output is correct |