#include <bits/stdc++.h>
using namespace std;
#define f first
#define s second
#define pb push_back
#define mp make_pair
#define all(v) v.begin(), v.end()
#define sz(v) (int)v.size()
#define MOO(i, a, b) for(int i=a; i<b; i++)
#define M00(i, a) for(int i=0; i<a; i++)
#define MOOd(i,a,b) for(int i = (b)-1; i >= a; i--)
#define M00d(i,a) for(int i = (a)-1; i>=0; i--)
#define FAST ios::sync_with_stdio(0); cin.tie(0);
#define finish(x) return cout << x << '\n', 0;
#define dbg(x) cerr << ">>> " << #x << " = " << x << "\n";
#define _ << " _ " <<
template<class T> bool ckmin(T& a, const T& b) {return b < a ? a = b, 1 : 0;}
template<class T> bool ckmax(T& a, const T& b) {return a < b ? a = b, 1 : 0;}
typedef unsigned long long ull;
typedef long long ll;
typedef long double ld;
typedef vector<int> vi;
typedef pair<int,int> pi;
typedef pair<ld,ld> pd;
typedef complex<ld> cd;
const int MAX_N = 1000010;
const ll inf = (ll)(2e18);
int n;
ll A, B;
ll gcd(ll a, ll b) {
if(a > b) swap(a,b);
if(a == 0) return b;
return gcd(b%a, a);
}
bool overflow(ull a, ull b) {
ll val = a*b;
if(val / b == a) return 0;
return val <= inf;
}
int main() { FAST
cin >> n >> A >> B;
ll period = A/gcd(A,B+1);
if(overflow(B, period)) {
period = inf;
} else {
period = B * period;
}
vector<pair<ll,ll>> intervals;
M00(i, n) {
ll l, r; cin >> l >> r;
if(r-l+1 >= period) {
finish(period);
}
l %= period;
r %= period;
if(l <= r) intervals.pb(mp(l, r));
else {
intervals.pb(mp(l, period-1));
intervals.pb(mp(0,r));
}
}
sort(all(intervals));
ll last = -1;
ll res = 0;
for(pair<ll,ll> interval: intervals) {
if(interval.f > last) {
res += interval.s - interval.f + 1;
} else {
if(interval.s > last) res += interval.s - last;
}
ckmax(last, interval.s);
}
finish(res);
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
6 ms |
768 KB |
Output is correct |
17 |
Correct |
58 ms |
2544 KB |
Output is correct |
18 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
0 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 |
372 ms |
16956 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
503 ms |
16852 KB |
Output is correct |
3 |
Correct |
509 ms |
16980 KB |
Output is correct |
4 |
Correct |
534 ms |
16856 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
503 ms |
16852 KB |
Output is correct |
3 |
Correct |
509 ms |
16980 KB |
Output is correct |
4 |
Correct |
534 ms |
16856 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
497 ms |
16852 KB |
Output is correct |
7 |
Correct |
533 ms |
16852 KB |
Output is correct |
8 |
Correct |
515 ms |
16852 KB |
Output is correct |
9 |
Correct |
568 ms |
16852 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
384 KB |
Output is correct |
2 |
Correct |
503 ms |
16852 KB |
Output is correct |
3 |
Correct |
509 ms |
16980 KB |
Output is correct |
4 |
Correct |
534 ms |
16856 KB |
Output is correct |
5 |
Correct |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
52 ms |
2540 KB |
Output is correct |
7 |
Correct |
52 ms |
2544 KB |
Output is correct |
8 |
Correct |
48 ms |
2544 KB |
Output is correct |
9 |
Correct |
51 ms |
2544 KB |
Output is correct |
10 |
Correct |
51 ms |
2600 KB |
Output is correct |
11 |
Correct |
52 ms |
2540 KB |
Output is correct |
12 |
Correct |
50 ms |
2548 KB |
Output is correct |
13 |
Correct |
52 ms |
2544 KB |
Output is correct |
14 |
Correct |
53 ms |
2544 KB |
Output is correct |
15 |
Correct |
56 ms |
2544 KB |
Output is correct |
16 |
Correct |
55 ms |
2544 KB |
Output is correct |
17 |
Correct |
49 ms |
2544 KB |
Output is correct |
18 |
Correct |
515 ms |
16852 KB |
Output is correct |
19 |
Correct |
494 ms |
16852 KB |
Output is correct |
20 |
Correct |
624 ms |
17036 KB |
Output is correct |
21 |
Correct |
90 ms |
2540 KB |
Output is correct |
22 |
Correct |
50 ms |
2544 KB |
Output is correct |
23 |
Correct |
205 ms |
8672 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
384 KB |
Output is correct |
2 |
Correct |
53 ms |
2552 KB |
Output is correct |
3 |
Correct |
55 ms |
2672 KB |
Output is correct |
4 |
Correct |
590 ms |
17116 KB |
Output is correct |
5 |
Correct |
54 ms |
2544 KB |
Output is correct |
6 |
Correct |
56 ms |
2544 KB |
Output is correct |
7 |
Correct |
56 ms |
2672 KB |
Output is correct |
8 |
Correct |
54 ms |
2544 KB |
Output is correct |
9 |
Correct |
53 ms |
2548 KB |
Output is correct |
10 |
Correct |
57 ms |
2544 KB |
Output is correct |
11 |
Correct |
54 ms |
2544 KB |
Output is correct |
12 |
Correct |
50 ms |
2544 KB |
Output is correct |
13 |
Correct |
55 ms |
2544 KB |
Output is correct |
14 |
Correct |
556 ms |
16852 KB |
Output is correct |
15 |
Correct |
56 ms |
2544 KB |
Output is correct |
16 |
Correct |
500 ms |
16916 KB |
Output is correct |
17 |
Correct |
508 ms |
16852 KB |
Output is correct |
18 |
Correct |
0 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 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 |
0 ms |
384 KB |
Output is correct |
6 |
Correct |
1 ms |
384 KB |
Output is correct |
7 |
Correct |
0 ms |
384 KB |
Output is correct |
8 |
Correct |
1 ms |
384 KB |
Output is correct |
9 |
Correct |
0 ms |
384 KB |
Output is correct |
10 |
Correct |
1 ms |
384 KB |
Output is correct |
11 |
Correct |
0 ms |
384 KB |
Output is correct |
12 |
Correct |
0 ms |
384 KB |
Output is correct |
13 |
Correct |
0 ms |
384 KB |
Output is correct |
14 |
Correct |
0 ms |
384 KB |
Output is correct |
15 |
Correct |
1 ms |
384 KB |
Output is correct |
16 |
Correct |
6 ms |
768 KB |
Output is correct |
17 |
Correct |
58 ms |
2544 KB |
Output is correct |
18 |
Correct |
0 ms |
384 KB |
Output is correct |
19 |
Correct |
0 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 |
0 ms |
384 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 |
372 ms |
16956 KB |
Output is correct |
29 |
Correct |
0 ms |
384 KB |
Output is correct |
30 |
Correct |
503 ms |
16852 KB |
Output is correct |
31 |
Correct |
509 ms |
16980 KB |
Output is correct |
32 |
Correct |
534 ms |
16856 KB |
Output is correct |
33 |
Correct |
0 ms |
384 KB |
Output is correct |
34 |
Correct |
497 ms |
16852 KB |
Output is correct |
35 |
Correct |
533 ms |
16852 KB |
Output is correct |
36 |
Correct |
515 ms |
16852 KB |
Output is correct |
37 |
Correct |
568 ms |
16852 KB |
Output is correct |
38 |
Correct |
0 ms |
384 KB |
Output is correct |
39 |
Correct |
52 ms |
2540 KB |
Output is correct |
40 |
Correct |
52 ms |
2544 KB |
Output is correct |
41 |
Correct |
48 ms |
2544 KB |
Output is correct |
42 |
Correct |
51 ms |
2544 KB |
Output is correct |
43 |
Correct |
51 ms |
2600 KB |
Output is correct |
44 |
Correct |
52 ms |
2540 KB |
Output is correct |
45 |
Correct |
50 ms |
2548 KB |
Output is correct |
46 |
Correct |
52 ms |
2544 KB |
Output is correct |
47 |
Correct |
53 ms |
2544 KB |
Output is correct |
48 |
Correct |
56 ms |
2544 KB |
Output is correct |
49 |
Correct |
55 ms |
2544 KB |
Output is correct |
50 |
Correct |
49 ms |
2544 KB |
Output is correct |
51 |
Correct |
515 ms |
16852 KB |
Output is correct |
52 |
Correct |
494 ms |
16852 KB |
Output is correct |
53 |
Correct |
624 ms |
17036 KB |
Output is correct |
54 |
Correct |
90 ms |
2540 KB |
Output is correct |
55 |
Correct |
50 ms |
2544 KB |
Output is correct |
56 |
Correct |
205 ms |
8672 KB |
Output is correct |
57 |
Correct |
1 ms |
384 KB |
Output is correct |
58 |
Correct |
53 ms |
2552 KB |
Output is correct |
59 |
Correct |
55 ms |
2672 KB |
Output is correct |
60 |
Correct |
590 ms |
17116 KB |
Output is correct |
61 |
Correct |
54 ms |
2544 KB |
Output is correct |
62 |
Correct |
56 ms |
2544 KB |
Output is correct |
63 |
Correct |
56 ms |
2672 KB |
Output is correct |
64 |
Correct |
54 ms |
2544 KB |
Output is correct |
65 |
Correct |
53 ms |
2548 KB |
Output is correct |
66 |
Correct |
57 ms |
2544 KB |
Output is correct |
67 |
Correct |
54 ms |
2544 KB |
Output is correct |
68 |
Correct |
50 ms |
2544 KB |
Output is correct |
69 |
Correct |
55 ms |
2544 KB |
Output is correct |
70 |
Correct |
556 ms |
16852 KB |
Output is correct |
71 |
Correct |
56 ms |
2544 KB |
Output is correct |
72 |
Correct |
500 ms |
16916 KB |
Output is correct |
73 |
Correct |
508 ms |
16852 KB |
Output is correct |
74 |
Correct |
0 ms |
384 KB |
Output is correct |
75 |
Correct |
0 ms |
392 KB |
Output is correct |
76 |
Correct |
1 ms |
384 KB |
Output is correct |
77 |
Correct |
1 ms |
384 KB |
Output is correct |
78 |
Correct |
1 ms |
384 KB |
Output is correct |
79 |
Correct |
6 ms |
1280 KB |
Output is correct |
80 |
Correct |
574 ms |
28372 KB |
Output is correct |
81 |
Correct |
569 ms |
28372 KB |
Output is correct |
82 |
Correct |
556 ms |
28380 KB |
Output is correct |
83 |
Correct |
551 ms |
28244 KB |
Output is correct |
84 |
Correct |
561 ms |
28132 KB |
Output is correct |
85 |
Correct |
574 ms |
28124 KB |
Output is correct |
86 |
Correct |
173 ms |
18528 KB |
Output is correct |
87 |
Correct |
1 ms |
384 KB |
Output is correct |