#include <bits/stdc++.h>
using namespace std;
const long long N = ((long long)1e18) + 5;
long long gcd(long long x, long long y)
{
if (x < y)
swap(x, y);
while (y)
{
x %= y;
swap(x, y);
}
return x;
}
vector<pair<long long, long long>> v;
long long ans;
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
long long n, a, b;
cin >> n >> a >> b;
long long aa = a / gcd(a, b + 1);
long long len;
if (aa > (N / b))
len = N;
else
len = aa * b;
for (long long i = 0; i < n; i++)
{
long long bg, en;
cin >> bg >> en;
if (en - bg >= len)
{
cout << len << '\n';
exit(0);
}
bg %= len;
en %= len;
if (bg > en)
{
v.push_back(make_pair(bg, len - 1));
v.push_back(make_pair(0, en));
}
else
{
v.push_back(make_pair(bg, en));
}
}
sort(v.begin(), v.end());
long long bg = 0, en = -1;
for (pair<long long, long long> x : v)
{
long long bgg = x.first;
long long enn = x.second;
if (bgg > en)
{
ans += en - bg + 1;
bg = bgg;
en = enn;
continue;
}
if (bgg <= en)
{
en = max(en, enn);
}
}
ans += en - bg + 1;
cout << ans << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
768 KB |
Output is correct |
3 |
Correct |
6 ms |
768 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
388 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
288 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
9 ms |
1152 KB |
Output is correct |
17 |
Correct |
70 ms |
5736 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
360 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
1 ms |
384 KB |
Output is correct |
3 |
Correct |
1 ms |
384 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
332 ms |
16984 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
467 ms |
16892 KB |
Output is correct |
3 |
Correct |
487 ms |
16948 KB |
Output is correct |
4 |
Correct |
464 ms |
35776 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
467 ms |
16892 KB |
Output is correct |
3 |
Correct |
487 ms |
16948 KB |
Output is correct |
4 |
Correct |
464 ms |
35776 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
500 ms |
35776 KB |
Output is correct |
7 |
Correct |
476 ms |
35796 KB |
Output is correct |
8 |
Correct |
459 ms |
35904 KB |
Output is correct |
9 |
Correct |
521 ms |
35776 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
467 ms |
16892 KB |
Output is correct |
3 |
Correct |
487 ms |
16948 KB |
Output is correct |
4 |
Correct |
464 ms |
35776 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
46 ms |
5740 KB |
Output is correct |
7 |
Correct |
54 ms |
5740 KB |
Output is correct |
8 |
Correct |
47 ms |
5748 KB |
Output is correct |
9 |
Correct |
46 ms |
5740 KB |
Output is correct |
10 |
Correct |
55 ms |
5740 KB |
Output is correct |
11 |
Correct |
48 ms |
5740 KB |
Output is correct |
12 |
Correct |
44 ms |
5740 KB |
Output is correct |
13 |
Correct |
48 ms |
5740 KB |
Output is correct |
14 |
Correct |
51 ms |
5736 KB |
Output is correct |
15 |
Correct |
58 ms |
5740 KB |
Output is correct |
16 |
Correct |
48 ms |
5740 KB |
Output is correct |
17 |
Correct |
46 ms |
5740 KB |
Output is correct |
18 |
Correct |
474 ms |
21076 KB |
Output is correct |
19 |
Correct |
440 ms |
35912 KB |
Output is correct |
20 |
Correct |
516 ms |
35904 KB |
Output is correct |
21 |
Correct |
56 ms |
5740 KB |
Output is correct |
22 |
Correct |
44 ms |
5740 KB |
Output is correct |
23 |
Correct |
183 ms |
18528 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
49 ms |
2672 KB |
Output is correct |
3 |
Correct |
91 ms |
2672 KB |
Output is correct |
4 |
Correct |
564 ms |
17108 KB |
Output is correct |
5 |
Correct |
47 ms |
2800 KB |
Output is correct |
6 |
Correct |
65 ms |
2800 KB |
Output is correct |
7 |
Correct |
49 ms |
2800 KB |
Output is correct |
8 |
Correct |
47 ms |
2800 KB |
Output is correct |
9 |
Correct |
47 ms |
2800 KB |
Output is correct |
10 |
Correct |
59 ms |
2856 KB |
Output is correct |
11 |
Correct |
53 ms |
2808 KB |
Output is correct |
12 |
Correct |
50 ms |
2796 KB |
Output is correct |
13 |
Correct |
62 ms |
2808 KB |
Output is correct |
14 |
Correct |
528 ms |
17108 KB |
Output is correct |
15 |
Correct |
61 ms |
2968 KB |
Output is correct |
16 |
Correct |
472 ms |
34832 KB |
Output is correct |
17 |
Correct |
480 ms |
31292 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
768 KB |
Output is correct |
3 |
Correct |
6 ms |
768 KB |
Output is correct |
4 |
Correct |
1 ms |
384 KB |
Output is correct |
5 |
Correct |
1 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
388 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
1 ms |
288 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
1 ms |
384 KB |
Output is correct |
12 |
Correct |
1 ms |
384 KB |
Output is correct |
13 |
Correct |
1 ms |
384 KB |
Output is correct |
14 |
Correct |
1 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
9 ms |
1152 KB |
Output is correct |
17 |
Correct |
70 ms |
5736 KB |
Output is correct |
18 |
Correct |
1 ms |
384 KB |
Output is correct |
19 |
Correct |
1 ms |
384 KB |
Output is correct |
20 |
Correct |
1 ms |
384 KB |
Output is correct |
21 |
Correct |
1 ms |
384 KB |
Output is correct |
22 |
Correct |
1 ms |
384 KB |
Output is correct |
23 |
Correct |
1 ms |
360 KB |
Output is correct |
24 |
Correct |
1 ms |
384 KB |
Output is correct |
25 |
Correct |
1 ms |
384 KB |
Output is correct |
26 |
Correct |
1 ms |
384 KB |
Output is correct |
27 |
Correct |
1 ms |
384 KB |
Output is correct |
28 |
Correct |
332 ms |
16984 KB |
Output is correct |
29 |
Correct |
1 ms |
384 KB |
Output is correct |
30 |
Correct |
467 ms |
16892 KB |
Output is correct |
31 |
Correct |
487 ms |
16948 KB |
Output is correct |
32 |
Correct |
464 ms |
35776 KB |
Output is correct |
33 |
Correct |
0 ms |
384 KB |
Output is correct |
34 |
Correct |
500 ms |
35776 KB |
Output is correct |
35 |
Correct |
476 ms |
35796 KB |
Output is correct |
36 |
Correct |
459 ms |
35904 KB |
Output is correct |
37 |
Correct |
521 ms |
35776 KB |
Output is correct |
38 |
Correct |
1 ms |
384 KB |
Output is correct |
39 |
Correct |
46 ms |
5740 KB |
Output is correct |
40 |
Correct |
54 ms |
5740 KB |
Output is correct |
41 |
Correct |
47 ms |
5748 KB |
Output is correct |
42 |
Correct |
46 ms |
5740 KB |
Output is correct |
43 |
Correct |
55 ms |
5740 KB |
Output is correct |
44 |
Correct |
48 ms |
5740 KB |
Output is correct |
45 |
Correct |
44 ms |
5740 KB |
Output is correct |
46 |
Correct |
48 ms |
5740 KB |
Output is correct |
47 |
Correct |
51 ms |
5736 KB |
Output is correct |
48 |
Correct |
58 ms |
5740 KB |
Output is correct |
49 |
Correct |
48 ms |
5740 KB |
Output is correct |
50 |
Correct |
46 ms |
5740 KB |
Output is correct |
51 |
Correct |
474 ms |
21076 KB |
Output is correct |
52 |
Correct |
440 ms |
35912 KB |
Output is correct |
53 |
Correct |
516 ms |
35904 KB |
Output is correct |
54 |
Correct |
56 ms |
5740 KB |
Output is correct |
55 |
Correct |
44 ms |
5740 KB |
Output is correct |
56 |
Correct |
183 ms |
18528 KB |
Output is correct |
57 |
Correct |
1 ms |
384 KB |
Output is correct |
58 |
Correct |
49 ms |
2672 KB |
Output is correct |
59 |
Correct |
91 ms |
2672 KB |
Output is correct |
60 |
Correct |
564 ms |
17108 KB |
Output is correct |
61 |
Correct |
47 ms |
2800 KB |
Output is correct |
62 |
Correct |
65 ms |
2800 KB |
Output is correct |
63 |
Correct |
49 ms |
2800 KB |
Output is correct |
64 |
Correct |
47 ms |
2800 KB |
Output is correct |
65 |
Correct |
47 ms |
2800 KB |
Output is correct |
66 |
Correct |
59 ms |
2856 KB |
Output is correct |
67 |
Correct |
53 ms |
2808 KB |
Output is correct |
68 |
Correct |
50 ms |
2796 KB |
Output is correct |
69 |
Correct |
62 ms |
2808 KB |
Output is correct |
70 |
Correct |
528 ms |
17108 KB |
Output is correct |
71 |
Correct |
61 ms |
2968 KB |
Output is correct |
72 |
Correct |
472 ms |
34832 KB |
Output is correct |
73 |
Correct |
480 ms |
31292 KB |
Output is correct |
74 |
Correct |
1 ms |
384 KB |
Output is correct |
75 |
Correct |
0 ms |
384 KB |
Output is correct |
76 |
Correct |
1 ms |
384 KB |
Output is correct |
77 |
Correct |
1 ms |
384 KB |
Output is correct |
78 |
Correct |
0 ms |
384 KB |
Output is correct |
79 |
Correct |
5 ms |
1152 KB |
Output is correct |
80 |
Correct |
577 ms |
25096 KB |
Output is correct |
81 |
Correct |
597 ms |
50416 KB |
Output is correct |
82 |
Correct |
586 ms |
47164 KB |
Output is correct |
83 |
Correct |
546 ms |
43212 KB |
Output is correct |
84 |
Correct |
706 ms |
40624 KB |
Output is correct |
85 |
Correct |
555 ms |
39216 KB |
Output is correct |
86 |
Correct |
150 ms |
18584 KB |
Output is correct |
87 |
Correct |
1 ms |
384 KB |
Output is correct |