#include<bits/stdc++.h>
using namespace std;
#define ioss ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0)
#define int long long
#define pii pair<int, int>
#define fi first
#define se second
#define pb push_back
int n, a, b;
vector<pii> range;
bool comp(pii a, pii b) {
return (a.fi < b.fi || (a.fi == b.fi && a.se < b.se));
}
signed main() {
ioss;
// cari cycle (bisa manual, baru cari pola)
// --- a*b / fpb(a, b+1)
// hitung segment aktif
// line sweep
cin >> n >> a >> b;
__int128_t cycle = (__int128_t)a*b/(__gcd(a, (b+1)));
int ans = 0;
for(int i = 0; i < n; i++) {
int l, r; cin >> l >> r;
if((r-l)+1 >= cycle) ans = cycle;
l %= cycle, r %= cycle;
if(l <= r) range.pb({l, 1}), range.pb({r, 2});
else range.pb({l, 1}), range.pb({cycle-1, 2}), range.pb({0, 1}), range.pb({r, 2});
}
sort(range.begin(), range.end(), comp);
if(ans == cycle) {
cout << ans << endl;
return 0;
}
int l = -1, cnt = 0, r = 0;
for(auto [val, type] : range) {
if(type == 1) {
if(l == -1) l = val;
cnt++;
}
else r = val, cnt--;
if(cnt == 0) ans += (r-l)+1, l = -1;
// cout << " :: " << l << " " << r << " " << cnt << " " << ans << endl;
}
cout << ans << endl;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
5 ms |
856 KB |
Output is correct |
3 |
Correct |
6 ms |
1240 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
324 KB |
Output is correct |
16 |
Correct |
5 ms |
1240 KB |
Output is correct |
17 |
Correct |
62 ms |
7180 KB |
Output is correct |
18 |
Correct |
0 ms |
320 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
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 |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
376 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
366 ms |
36348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
585 ms |
33236 KB |
Output is correct |
3 |
Correct |
634 ms |
61844 KB |
Output is correct |
4 |
Correct |
613 ms |
57948 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
585 ms |
33236 KB |
Output is correct |
3 |
Correct |
634 ms |
61844 KB |
Output is correct |
4 |
Correct |
613 ms |
57948 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
615 ms |
58068 KB |
Output is correct |
7 |
Correct |
583 ms |
58040 KB |
Output is correct |
8 |
Correct |
593 ms |
57324 KB |
Output is correct |
9 |
Correct |
671 ms |
57740 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
585 ms |
33236 KB |
Output is correct |
3 |
Correct |
634 ms |
61844 KB |
Output is correct |
4 |
Correct |
613 ms |
57948 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
51 ms |
7396 KB |
Output is correct |
7 |
Correct |
56 ms |
7288 KB |
Output is correct |
8 |
Correct |
51 ms |
7232 KB |
Output is correct |
9 |
Correct |
60 ms |
7332 KB |
Output is correct |
10 |
Correct |
46 ms |
7256 KB |
Output is correct |
11 |
Correct |
53 ms |
7212 KB |
Output is correct |
12 |
Correct |
49 ms |
7236 KB |
Output is correct |
13 |
Correct |
62 ms |
7292 KB |
Output is correct |
14 |
Correct |
54 ms |
7224 KB |
Output is correct |
15 |
Correct |
69 ms |
7216 KB |
Output is correct |
16 |
Correct |
59 ms |
7216 KB |
Output is correct |
17 |
Correct |
52 ms |
7272 KB |
Output is correct |
18 |
Correct |
585 ms |
57492 KB |
Output is correct |
19 |
Correct |
529 ms |
56960 KB |
Output is correct |
20 |
Correct |
700 ms |
58408 KB |
Output is correct |
21 |
Correct |
76 ms |
7296 KB |
Output is correct |
22 |
Correct |
44 ms |
7196 KB |
Output is correct |
23 |
Correct |
153 ms |
21492 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
61 ms |
4544 KB |
Output is correct |
3 |
Correct |
60 ms |
7292 KB |
Output is correct |
4 |
Correct |
726 ms |
62944 KB |
Output is correct |
5 |
Correct |
61 ms |
7208 KB |
Output is correct |
6 |
Correct |
66 ms |
7340 KB |
Output is correct |
7 |
Correct |
58 ms |
7232 KB |
Output is correct |
8 |
Correct |
61 ms |
7292 KB |
Output is correct |
9 |
Correct |
57 ms |
7292 KB |
Output is correct |
10 |
Correct |
63 ms |
7292 KB |
Output is correct |
11 |
Correct |
58 ms |
7228 KB |
Output is correct |
12 |
Correct |
51 ms |
7216 KB |
Output is correct |
13 |
Correct |
61 ms |
7188 KB |
Output is correct |
14 |
Correct |
652 ms |
62944 KB |
Output is correct |
15 |
Correct |
61 ms |
7292 KB |
Output is correct |
16 |
Correct |
533 ms |
58052 KB |
Output is correct |
17 |
Correct |
568 ms |
57224 KB |
Output is correct |
18 |
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 |
5 ms |
856 KB |
Output is correct |
3 |
Correct |
6 ms |
1240 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 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 |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
1 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
324 KB |
Output is correct |
16 |
Correct |
5 ms |
1240 KB |
Output is correct |
17 |
Correct |
62 ms |
7180 KB |
Output is correct |
18 |
Correct |
0 ms |
320 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 |
0 ms |
212 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
376 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
366 ms |
36348 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
585 ms |
33236 KB |
Output is correct |
31 |
Correct |
634 ms |
61844 KB |
Output is correct |
32 |
Correct |
613 ms |
57948 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
615 ms |
58068 KB |
Output is correct |
35 |
Correct |
583 ms |
58040 KB |
Output is correct |
36 |
Correct |
593 ms |
57324 KB |
Output is correct |
37 |
Correct |
671 ms |
57740 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
51 ms |
7396 KB |
Output is correct |
40 |
Correct |
56 ms |
7288 KB |
Output is correct |
41 |
Correct |
51 ms |
7232 KB |
Output is correct |
42 |
Correct |
60 ms |
7332 KB |
Output is correct |
43 |
Correct |
46 ms |
7256 KB |
Output is correct |
44 |
Correct |
53 ms |
7212 KB |
Output is correct |
45 |
Correct |
49 ms |
7236 KB |
Output is correct |
46 |
Correct |
62 ms |
7292 KB |
Output is correct |
47 |
Correct |
54 ms |
7224 KB |
Output is correct |
48 |
Correct |
69 ms |
7216 KB |
Output is correct |
49 |
Correct |
59 ms |
7216 KB |
Output is correct |
50 |
Correct |
52 ms |
7272 KB |
Output is correct |
51 |
Correct |
585 ms |
57492 KB |
Output is correct |
52 |
Correct |
529 ms |
56960 KB |
Output is correct |
53 |
Correct |
700 ms |
58408 KB |
Output is correct |
54 |
Correct |
76 ms |
7296 KB |
Output is correct |
55 |
Correct |
44 ms |
7196 KB |
Output is correct |
56 |
Correct |
153 ms |
21492 KB |
Output is correct |
57 |
Correct |
0 ms |
212 KB |
Output is correct |
58 |
Correct |
61 ms |
4544 KB |
Output is correct |
59 |
Correct |
60 ms |
7292 KB |
Output is correct |
60 |
Correct |
726 ms |
62944 KB |
Output is correct |
61 |
Correct |
61 ms |
7208 KB |
Output is correct |
62 |
Correct |
66 ms |
7340 KB |
Output is correct |
63 |
Correct |
58 ms |
7232 KB |
Output is correct |
64 |
Correct |
61 ms |
7292 KB |
Output is correct |
65 |
Correct |
57 ms |
7292 KB |
Output is correct |
66 |
Correct |
63 ms |
7292 KB |
Output is correct |
67 |
Correct |
58 ms |
7228 KB |
Output is correct |
68 |
Correct |
51 ms |
7216 KB |
Output is correct |
69 |
Correct |
61 ms |
7188 KB |
Output is correct |
70 |
Correct |
652 ms |
62944 KB |
Output is correct |
71 |
Correct |
61 ms |
7292 KB |
Output is correct |
72 |
Correct |
533 ms |
58052 KB |
Output is correct |
73 |
Correct |
568 ms |
57224 KB |
Output is correct |
74 |
Correct |
1 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 |
0 ms |
212 KB |
Output is correct |
78 |
Correct |
1 ms |
316 KB |
Output is correct |
79 |
Correct |
5 ms |
1288 KB |
Output is correct |
80 |
Correct |
722 ms |
37852 KB |
Output is correct |
81 |
Correct |
706 ms |
37748 KB |
Output is correct |
82 |
Correct |
649 ms |
37672 KB |
Output is correct |
83 |
Correct |
649 ms |
37912 KB |
Output is correct |
84 |
Correct |
661 ms |
37784 KB |
Output is correct |
85 |
Correct |
661 ms |
37688 KB |
Output is correct |
86 |
Correct |
135 ms |
21468 KB |
Output is correct |
87 |
Correct |
1 ms |
212 KB |
Output is correct |