#include <bits/stdc++.h>
using namespace std;
using ll = long long;
const ll inf = 1e18;
void add (set<pair<ll, ll>>& st, ll l, ll r) {
auto it = st.lower_bound({l, 0}), tmp = it;
if (it != st.begin() && (--tmp)->second >= l) {
l = tmp->first;
r = max(r, tmp->second);
st.erase(tmp);
}
while (it != st.end()) {
if (r >= it->first) {
r = max(r, it->second);
it = st.erase(it);
} else break;
}
st.insert({l, r});
}
int main () {
ll n, a, b;
cin >> n >> a >> b;
ll mod;
if (a / gcd(a, b+1) > inf / b) mod = inf;
else mod = a / gcd(a, b+1) * b;
set<pair<ll, ll>> st;
while (n--) {
ll l, r;
cin >> l >> r;
if (r-l+1 >= mod) {
cout << mod << '\n';
return 0;
}
if (l % mod > r % mod) {
add(st, 0, r % mod);
add(st, l % mod, mod-1);
} else add(st, l % mod, r % mod);
}
ll ans = 0;
for (auto& [l, r] : st) ans += r-l+1;
cout << ans << '\n';
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
12 ms |
868 KB |
Output is correct |
3 |
Correct |
11 ms |
1200 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
296 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 |
300 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 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 |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
11 ms |
1304 KB |
Output is correct |
17 |
Correct |
114 ms |
10200 KB |
Output is correct |
18 |
Correct |
1 ms |
304 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 |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
2 ms |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
312 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
744 ms |
25404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1244 ms |
62988 KB |
Output is correct |
3 |
Correct |
1348 ms |
62796 KB |
Output is correct |
4 |
Correct |
1241 ms |
62784 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1244 ms |
62988 KB |
Output is correct |
3 |
Correct |
1348 ms |
62796 KB |
Output is correct |
4 |
Correct |
1241 ms |
62784 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1196 ms |
62852 KB |
Output is correct |
7 |
Correct |
1173 ms |
62776 KB |
Output is correct |
8 |
Correct |
1231 ms |
62796 KB |
Output is correct |
9 |
Correct |
1381 ms |
62884 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1244 ms |
62988 KB |
Output is correct |
3 |
Correct |
1348 ms |
62796 KB |
Output is correct |
4 |
Correct |
1241 ms |
62784 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
108 ms |
6496 KB |
Output is correct |
7 |
Correct |
108 ms |
6420 KB |
Output is correct |
8 |
Correct |
123 ms |
6476 KB |
Output is correct |
9 |
Correct |
114 ms |
6432 KB |
Output is correct |
10 |
Correct |
114 ms |
6468 KB |
Output is correct |
11 |
Correct |
110 ms |
6484 KB |
Output is correct |
12 |
Correct |
112 ms |
6528 KB |
Output is correct |
13 |
Correct |
120 ms |
6476 KB |
Output is correct |
14 |
Correct |
109 ms |
6428 KB |
Output is correct |
15 |
Correct |
117 ms |
6476 KB |
Output is correct |
16 |
Correct |
134 ms |
6488 KB |
Output is correct |
17 |
Correct |
120 ms |
10280 KB |
Output is correct |
18 |
Correct |
1198 ms |
100068 KB |
Output is correct |
19 |
Correct |
1228 ms |
100064 KB |
Output is correct |
20 |
Correct |
1280 ms |
100096 KB |
Output is correct |
21 |
Correct |
113 ms |
10156 KB |
Output is correct |
22 |
Correct |
118 ms |
10252 KB |
Output is correct |
23 |
Correct |
359 ms |
12816 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
118 ms |
6468 KB |
Output is correct |
3 |
Correct |
116 ms |
6460 KB |
Output is correct |
4 |
Correct |
1287 ms |
62904 KB |
Output is correct |
5 |
Correct |
135 ms |
6452 KB |
Output is correct |
6 |
Correct |
120 ms |
6420 KB |
Output is correct |
7 |
Correct |
128 ms |
6472 KB |
Output is correct |
8 |
Correct |
123 ms |
6512 KB |
Output is correct |
9 |
Correct |
132 ms |
6552 KB |
Output is correct |
10 |
Correct |
139 ms |
6484 KB |
Output is correct |
11 |
Correct |
122 ms |
6528 KB |
Output is correct |
12 |
Correct |
127 ms |
6476 KB |
Output is correct |
13 |
Correct |
121 ms |
6540 KB |
Output is correct |
14 |
Correct |
1261 ms |
63008 KB |
Output is correct |
15 |
Correct |
108 ms |
5784 KB |
Output is correct |
16 |
Correct |
1178 ms |
100024 KB |
Output is correct |
17 |
Correct |
1271 ms |
100068 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
12 ms |
868 KB |
Output is correct |
3 |
Correct |
11 ms |
1200 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1 ms |
296 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 |
300 KB |
Output is correct |
10 |
Correct |
0 ms |
212 KB |
Output is correct |
11 |
Correct |
1 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 |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
11 ms |
1304 KB |
Output is correct |
17 |
Correct |
114 ms |
10200 KB |
Output is correct |
18 |
Correct |
1 ms |
304 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 |
1 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 |
2 ms |
212 KB |
Output is correct |
26 |
Correct |
2 ms |
312 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
744 ms |
25404 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
1244 ms |
62988 KB |
Output is correct |
31 |
Correct |
1348 ms |
62796 KB |
Output is correct |
32 |
Correct |
1241 ms |
62784 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
1196 ms |
62852 KB |
Output is correct |
35 |
Correct |
1173 ms |
62776 KB |
Output is correct |
36 |
Correct |
1231 ms |
62796 KB |
Output is correct |
37 |
Correct |
1381 ms |
62884 KB |
Output is correct |
38 |
Correct |
0 ms |
212 KB |
Output is correct |
39 |
Correct |
108 ms |
6496 KB |
Output is correct |
40 |
Correct |
108 ms |
6420 KB |
Output is correct |
41 |
Correct |
123 ms |
6476 KB |
Output is correct |
42 |
Correct |
114 ms |
6432 KB |
Output is correct |
43 |
Correct |
114 ms |
6468 KB |
Output is correct |
44 |
Correct |
110 ms |
6484 KB |
Output is correct |
45 |
Correct |
112 ms |
6528 KB |
Output is correct |
46 |
Correct |
120 ms |
6476 KB |
Output is correct |
47 |
Correct |
109 ms |
6428 KB |
Output is correct |
48 |
Correct |
117 ms |
6476 KB |
Output is correct |
49 |
Correct |
134 ms |
6488 KB |
Output is correct |
50 |
Correct |
120 ms |
10280 KB |
Output is correct |
51 |
Correct |
1198 ms |
100068 KB |
Output is correct |
52 |
Correct |
1228 ms |
100064 KB |
Output is correct |
53 |
Correct |
1280 ms |
100096 KB |
Output is correct |
54 |
Correct |
113 ms |
10156 KB |
Output is correct |
55 |
Correct |
118 ms |
10252 KB |
Output is correct |
56 |
Correct |
359 ms |
12816 KB |
Output is correct |
57 |
Correct |
0 ms |
212 KB |
Output is correct |
58 |
Correct |
118 ms |
6468 KB |
Output is correct |
59 |
Correct |
116 ms |
6460 KB |
Output is correct |
60 |
Correct |
1287 ms |
62904 KB |
Output is correct |
61 |
Correct |
135 ms |
6452 KB |
Output is correct |
62 |
Correct |
120 ms |
6420 KB |
Output is correct |
63 |
Correct |
128 ms |
6472 KB |
Output is correct |
64 |
Correct |
123 ms |
6512 KB |
Output is correct |
65 |
Correct |
132 ms |
6552 KB |
Output is correct |
66 |
Correct |
139 ms |
6484 KB |
Output is correct |
67 |
Correct |
122 ms |
6528 KB |
Output is correct |
68 |
Correct |
127 ms |
6476 KB |
Output is correct |
69 |
Correct |
121 ms |
6540 KB |
Output is correct |
70 |
Correct |
1261 ms |
63008 KB |
Output is correct |
71 |
Correct |
108 ms |
5784 KB |
Output is correct |
72 |
Correct |
1178 ms |
100024 KB |
Output is correct |
73 |
Correct |
1271 ms |
100068 KB |
Output is correct |
74 |
Correct |
1 ms |
212 KB |
Output is correct |
75 |
Correct |
0 ms |
296 KB |
Output is correct |
76 |
Correct |
1 ms |
212 KB |
Output is correct |
77 |
Correct |
0 ms |
212 KB |
Output is correct |
78 |
Correct |
0 ms |
212 KB |
Output is correct |
79 |
Correct |
18 ms |
1236 KB |
Output is correct |
80 |
Correct |
1544 ms |
100052 KB |
Output is correct |
81 |
Correct |
1228 ms |
100028 KB |
Output is correct |
82 |
Correct |
1336 ms |
100016 KB |
Output is correct |
83 |
Correct |
1299 ms |
100052 KB |
Output is correct |
84 |
Correct |
1351 ms |
100044 KB |
Output is correct |
85 |
Correct |
1243 ms |
100096 KB |
Output is correct |
86 |
Correct |
334 ms |
12828 KB |
Output is correct |
87 |
Correct |
1 ms |
212 KB |
Output is correct |