#include<bits/stdc++.h>
using namespace std;
typedef long long int ll;
typedef long double ld;
#define sync ios_base::sync_with_stdio(false),cin.tie(0),cout.tie(0)
#define pb push_back
#define pii pair<int, int>
#define mp make_pair
#define fi first
#define se second
const ll inf = 2e18;
int main()
{
int n;
ll A, B, AB;
cin >> n >> A >> B;
A /= __gcd(A, (B + 1));
if(inf / A >= B)
AB = A * B;
else
AB = inf;
vector<pair<ll, ll> > vec;
bool check = false;
for(int i = 0; i < n; i++)
{
ll l, r;
cin >> l >> r;
if(check)
{
continue;
}
if(r - l + 1 >= AB)
{
check = true;
continue;
}
r %= AB, l %= AB;
if(l > r)
{
vec.pb(mp(0, r));
vec.pb(mp(l, AB - 1));
}
else
vec.pb(mp(l, r));
}
sort(vec.begin(), vec.end());
ll ans = 0, mx = -1;
for(auto tmp : vec)
{
ll x = tmp.fi, y = tmp.se;
if(mx < x)
ans += y - x + 1;
else
ans += max(ll(0), y - mx);
mx = max(mx, y);
}
if(check)
ans = AB;
cout << ans << '\n';
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
15 ms |
692 KB |
Output is correct |
3 |
Correct |
13 ms |
596 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 |
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 |
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 |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
12 ms |
652 KB |
Output is correct |
17 |
Correct |
131 ms |
2504 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 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 |
212 KB |
Output is correct |
3 |
Correct |
2 ms |
212 KB |
Output is correct |
4 |
Correct |
2 ms |
212 KB |
Output is correct |
5 |
Correct |
881 ms |
16872 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1268 ms |
16872 KB |
Output is correct |
3 |
Correct |
1253 ms |
16848 KB |
Output is correct |
4 |
Correct |
1288 ms |
30044 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1268 ms |
16872 KB |
Output is correct |
3 |
Correct |
1253 ms |
16848 KB |
Output is correct |
4 |
Correct |
1288 ms |
30044 KB |
Output is correct |
5 |
Correct |
0 ms |
212 KB |
Output is correct |
6 |
Correct |
1262 ms |
29808 KB |
Output is correct |
7 |
Correct |
1245 ms |
29960 KB |
Output is correct |
8 |
Correct |
1238 ms |
29820 KB |
Output is correct |
9 |
Correct |
1324 ms |
31016 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1268 ms |
16872 KB |
Output is correct |
3 |
Correct |
1253 ms |
16848 KB |
Output is correct |
4 |
Correct |
1288 ms |
30044 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
130 ms |
5668 KB |
Output is correct |
7 |
Correct |
128 ms |
5620 KB |
Output is correct |
8 |
Correct |
135 ms |
5688 KB |
Output is correct |
9 |
Correct |
127 ms |
5588 KB |
Output is correct |
10 |
Correct |
125 ms |
5576 KB |
Output is correct |
11 |
Correct |
125 ms |
5680 KB |
Output is correct |
12 |
Correct |
133 ms |
5600 KB |
Output is correct |
13 |
Correct |
129 ms |
5672 KB |
Output is correct |
14 |
Correct |
126 ms |
5636 KB |
Output is correct |
15 |
Correct |
128 ms |
5668 KB |
Output is correct |
16 |
Correct |
130 ms |
5676 KB |
Output is correct |
17 |
Correct |
127 ms |
5652 KB |
Output is correct |
18 |
Correct |
1256 ms |
30948 KB |
Output is correct |
19 |
Correct |
1318 ms |
29800 KB |
Output is correct |
20 |
Correct |
1357 ms |
30916 KB |
Output is correct |
21 |
Correct |
138 ms |
5704 KB |
Output is correct |
22 |
Correct |
138 ms |
5576 KB |
Output is correct |
23 |
Correct |
449 ms |
18364 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
131 ms |
2424 KB |
Output is correct |
3 |
Correct |
132 ms |
5652 KB |
Output is correct |
4 |
Correct |
1346 ms |
30856 KB |
Output is correct |
5 |
Correct |
136 ms |
5612 KB |
Output is correct |
6 |
Correct |
132 ms |
5592 KB |
Output is correct |
7 |
Correct |
136 ms |
5816 KB |
Output is correct |
8 |
Correct |
132 ms |
5688 KB |
Output is correct |
9 |
Correct |
129 ms |
5868 KB |
Output is correct |
10 |
Correct |
127 ms |
5644 KB |
Output is correct |
11 |
Correct |
129 ms |
5660 KB |
Output is correct |
12 |
Correct |
124 ms |
5608 KB |
Output is correct |
13 |
Correct |
128 ms |
5596 KB |
Output is correct |
14 |
Correct |
1294 ms |
31120 KB |
Output is correct |
15 |
Correct |
132 ms |
5576 KB |
Output is correct |
16 |
Correct |
1253 ms |
31028 KB |
Output is correct |
17 |
Correct |
1335 ms |
30860 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 |
15 ms |
692 KB |
Output is correct |
3 |
Correct |
13 ms |
596 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 |
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 |
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 |
0 ms |
212 KB |
Output is correct |
16 |
Correct |
12 ms |
652 KB |
Output is correct |
17 |
Correct |
131 ms |
2504 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 |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
1 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 |
212 KB |
Output is correct |
26 |
Correct |
2 ms |
212 KB |
Output is correct |
27 |
Correct |
2 ms |
212 KB |
Output is correct |
28 |
Correct |
881 ms |
16872 KB |
Output is correct |
29 |
Correct |
0 ms |
212 KB |
Output is correct |
30 |
Correct |
1268 ms |
16872 KB |
Output is correct |
31 |
Correct |
1253 ms |
16848 KB |
Output is correct |
32 |
Correct |
1288 ms |
30044 KB |
Output is correct |
33 |
Correct |
0 ms |
212 KB |
Output is correct |
34 |
Correct |
1262 ms |
29808 KB |
Output is correct |
35 |
Correct |
1245 ms |
29960 KB |
Output is correct |
36 |
Correct |
1238 ms |
29820 KB |
Output is correct |
37 |
Correct |
1324 ms |
31016 KB |
Output is correct |
38 |
Correct |
1 ms |
340 KB |
Output is correct |
39 |
Correct |
130 ms |
5668 KB |
Output is correct |
40 |
Correct |
128 ms |
5620 KB |
Output is correct |
41 |
Correct |
135 ms |
5688 KB |
Output is correct |
42 |
Correct |
127 ms |
5588 KB |
Output is correct |
43 |
Correct |
125 ms |
5576 KB |
Output is correct |
44 |
Correct |
125 ms |
5680 KB |
Output is correct |
45 |
Correct |
133 ms |
5600 KB |
Output is correct |
46 |
Correct |
129 ms |
5672 KB |
Output is correct |
47 |
Correct |
126 ms |
5636 KB |
Output is correct |
48 |
Correct |
128 ms |
5668 KB |
Output is correct |
49 |
Correct |
130 ms |
5676 KB |
Output is correct |
50 |
Correct |
127 ms |
5652 KB |
Output is correct |
51 |
Correct |
1256 ms |
30948 KB |
Output is correct |
52 |
Correct |
1318 ms |
29800 KB |
Output is correct |
53 |
Correct |
1357 ms |
30916 KB |
Output is correct |
54 |
Correct |
138 ms |
5704 KB |
Output is correct |
55 |
Correct |
138 ms |
5576 KB |
Output is correct |
56 |
Correct |
449 ms |
18364 KB |
Output is correct |
57 |
Correct |
1 ms |
212 KB |
Output is correct |
58 |
Correct |
131 ms |
2424 KB |
Output is correct |
59 |
Correct |
132 ms |
5652 KB |
Output is correct |
60 |
Correct |
1346 ms |
30856 KB |
Output is correct |
61 |
Correct |
136 ms |
5612 KB |
Output is correct |
62 |
Correct |
132 ms |
5592 KB |
Output is correct |
63 |
Correct |
136 ms |
5816 KB |
Output is correct |
64 |
Correct |
132 ms |
5688 KB |
Output is correct |
65 |
Correct |
129 ms |
5868 KB |
Output is correct |
66 |
Correct |
127 ms |
5644 KB |
Output is correct |
67 |
Correct |
129 ms |
5660 KB |
Output is correct |
68 |
Correct |
124 ms |
5608 KB |
Output is correct |
69 |
Correct |
128 ms |
5596 KB |
Output is correct |
70 |
Correct |
1294 ms |
31120 KB |
Output is correct |
71 |
Correct |
132 ms |
5576 KB |
Output is correct |
72 |
Correct |
1253 ms |
31028 KB |
Output is correct |
73 |
Correct |
1335 ms |
30860 KB |
Output is correct |
74 |
Correct |
0 ms |
212 KB |
Output is correct |
75 |
Correct |
1 ms |
292 KB |
Output is correct |
76 |
Correct |
1 ms |
212 KB |
Output is correct |
77 |
Correct |
1 ms |
300 KB |
Output is correct |
78 |
Correct |
0 ms |
212 KB |
Output is correct |
79 |
Correct |
15 ms |
980 KB |
Output is correct |
80 |
Correct |
1684 ms |
30880 KB |
Output is correct |
81 |
Correct |
1372 ms |
30820 KB |
Output is correct |
82 |
Correct |
1346 ms |
30796 KB |
Output is correct |
83 |
Correct |
1372 ms |
30428 KB |
Output is correct |
84 |
Correct |
1285 ms |
30816 KB |
Output is correct |
85 |
Correct |
1293 ms |
30100 KB |
Output is correct |
86 |
Correct |
422 ms |
18436 KB |
Output is correct |
87 |
Correct |
1 ms |
212 KB |
Output is correct |