#include <bits/stdc++.h>
using namespace std;
using ll = long long;
#define pb push_back
const ll mod = 1e9 + 7;
const ll N = 2e5 + 37;
template<typename T, size_t N>
std::ostream& operator<<(std::ostream& os, const std::array<T, N>& a);
template<typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& v);
template<typename T1, typename T2>
std::ostream& operator<<(std::ostream& os, const std::pair<T1, T2>& p);
template<typename T>
std::ostream& operator<<(std::ostream& os, const std::set<T>& s);
template<typename T, typename cmp>
std::ostream& operator<<(std::ostream& os, const std::set<T, cmp>& s);
const ll inf = (ll)2e18 + 37;
ll get_period(ll a, ll b) {
ll ans = a / gcd(a, b + 1);
if(inf / ans < b) {
return -1;
}
return ans * b;
}
void solve_inf_case(ll n) {
ll ans = 0;
for(ll i = 0; i < n; ++i) {
ll l, r;
cin >> l >> r;
ans += r - l + 1;
}
cout << ans << endl;
}
void solve() {
ll n, A, B;
cin >> n >> A >> B;
ll period = get_period(A, B);
if(period == -1) {
solve_inf_case(n);
return;
}
set<pair<ll, ll> > s; // {r, l}
auto insert_p = [&](pair<ll, ll> p) -> void
{
assert(p.first >= p.second);
// cout << "Hey : " << p.second << ' ' << p.first << endl;
while(!s.empty() and p.second <= s.rbegin()->first) {
auto it = s.end();
--it;
p = {max(p.first, it->first), min(p.second, it->second)};
s.erase(it);
}
s.insert(p);
};
vector<pair<ll, ll> > a;
for(ll i = 0; i < n; ++i) {
pair<ll, ll> p; // {r, l}
cin >> p.second >> p.first;
bool twice = false;
if(p.first - p.second >= period - (p.second % period)) {
twice = true;
}
p.second %= period;
p.first %= period;
// cout << "Hi : " << p.second << ' ' << p.first << endl;
if(twice) {
a.push_back({period - 1, p.second});
a.push_back({p.first, 0});
}
else {
a.push_back(p);
}
}
sort(a.begin(), a.end());
for(const auto& p : a)
insert_p(p);
// cout << period << endl;
ll ans = 0;
for(auto p : s){
// cout << p.second << " " << p.first << endl;
ans += p.first - p.second + 1;
}
cout << ans << endl;
}
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(NULL);
// signed t; cin >> t; while(t--)
solve();
}
template<typename T, size_t N>
std::ostream& operator<<(std::ostream& os, const std::array<T, N>& a) {
os << "[";
for(size_t i = 0; i + 1 < N; ++i) {
os << a[i] << ", ";
}
if(N > 0)
os << a[N - 1];
os << "]";
return os;
}
template<typename T1, typename T2>
std::ostream& operator<<(std::ostream& os, const std::pair<T1, T2>& p) {
os << "(" << p.first << ", " << p.second << ") ";
return os;
}
template<typename T>
std::ostream& operator<<(std::ostream& os, const std::vector<T>& v) {
os << '[';
for(auto x : v)
os << x << ", ";
os << "] ";
return os;
}
template<typename T>
std::ostream& operator<<(std::ostream& os, const std::set<T>& s) {
os << "{";
for(auto x : s)
os << x << ", ";
os << "} ";
return os;
}
template<typename T, typename cmp>
std::ostream& operator<<(std::ostream& os, const std::set<T, cmp>& s) {
os << "{";
for(auto x : s)
os << x << ", ";
os << "} ";
return os;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
4 ms |
992 KB |
Output is correct |
3 |
Correct |
3 ms |
1248 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
3 ms |
992 KB |
Output is correct |
17 |
Correct |
40 ms |
8152 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
1 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
174 ms |
18476 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
391 ms |
83632 KB |
Output is correct |
3 |
Correct |
403 ms |
97832 KB |
Output is correct |
4 |
Correct |
408 ms |
98400 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
391 ms |
83632 KB |
Output is correct |
3 |
Correct |
403 ms |
97832 KB |
Output is correct |
4 |
Correct |
408 ms |
98400 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
403 ms |
97760 KB |
Output is correct |
7 |
Correct |
390 ms |
97960 KB |
Output is correct |
8 |
Correct |
391 ms |
98400 KB |
Output is correct |
9 |
Correct |
437 ms |
98580 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
391 ms |
83632 KB |
Output is correct |
3 |
Correct |
403 ms |
97832 KB |
Output is correct |
4 |
Correct |
408 ms |
98400 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
35 ms |
10440 KB |
Output is correct |
7 |
Correct |
38 ms |
10304 KB |
Output is correct |
8 |
Correct |
36 ms |
10440 KB |
Output is correct |
9 |
Correct |
35 ms |
10440 KB |
Output is correct |
10 |
Correct |
41 ms |
10364 KB |
Output is correct |
11 |
Correct |
38 ms |
10500 KB |
Output is correct |
12 |
Correct |
35 ms |
10440 KB |
Output is correct |
13 |
Correct |
36 ms |
10692 KB |
Output is correct |
14 |
Correct |
33 ms |
10332 KB |
Output is correct |
15 |
Correct |
42 ms |
10572 KB |
Output is correct |
16 |
Correct |
36 ms |
10436 KB |
Output is correct |
17 |
Correct |
35 ms |
10420 KB |
Output is correct |
18 |
Correct |
402 ms |
97788 KB |
Output is correct |
19 |
Correct |
394 ms |
97668 KB |
Output is correct |
20 |
Correct |
418 ms |
97744 KB |
Output is correct |
21 |
Correct |
36 ms |
10436 KB |
Output is correct |
22 |
Correct |
35 ms |
10496 KB |
Output is correct |
23 |
Correct |
74 ms |
14896 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
36 ms |
8148 KB |
Output is correct |
3 |
Correct |
36 ms |
8148 KB |
Output is correct |
4 |
Correct |
417 ms |
98024 KB |
Output is correct |
5 |
Correct |
37 ms |
10448 KB |
Output is correct |
6 |
Correct |
36 ms |
10444 KB |
Output is correct |
7 |
Correct |
38 ms |
10528 KB |
Output is correct |
8 |
Correct |
40 ms |
10472 KB |
Output is correct |
9 |
Correct |
36 ms |
10556 KB |
Output is correct |
10 |
Correct |
37 ms |
10436 KB |
Output is correct |
11 |
Correct |
37 ms |
10488 KB |
Output is correct |
12 |
Correct |
37 ms |
10580 KB |
Output is correct |
13 |
Correct |
37 ms |
10560 KB |
Output is correct |
14 |
Correct |
412 ms |
98348 KB |
Output is correct |
15 |
Correct |
38 ms |
9680 KB |
Output is correct |
16 |
Correct |
406 ms |
98024 KB |
Output is correct |
17 |
Correct |
416 ms |
97708 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
4 ms |
992 KB |
Output is correct |
3 |
Correct |
3 ms |
1248 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
344 KB |
Output is correct |
8 |
Correct |
0 ms |
348 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
3 ms |
992 KB |
Output is correct |
17 |
Correct |
40 ms |
8152 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
348 KB |
Output is correct |
22 |
Correct |
0 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
0 ms |
348 KB |
Output is correct |
25 |
Correct |
1 ms |
348 KB |
Output is correct |
26 |
Correct |
0 ms |
348 KB |
Output is correct |
27 |
Correct |
0 ms |
348 KB |
Output is correct |
28 |
Correct |
174 ms |
18476 KB |
Output is correct |
29 |
Correct |
1 ms |
344 KB |
Output is correct |
30 |
Correct |
391 ms |
83632 KB |
Output is correct |
31 |
Correct |
403 ms |
97832 KB |
Output is correct |
32 |
Correct |
408 ms |
98400 KB |
Output is correct |
33 |
Correct |
0 ms |
348 KB |
Output is correct |
34 |
Correct |
403 ms |
97760 KB |
Output is correct |
35 |
Correct |
390 ms |
97960 KB |
Output is correct |
36 |
Correct |
391 ms |
98400 KB |
Output is correct |
37 |
Correct |
437 ms |
98580 KB |
Output is correct |
38 |
Correct |
0 ms |
344 KB |
Output is correct |
39 |
Correct |
35 ms |
10440 KB |
Output is correct |
40 |
Correct |
38 ms |
10304 KB |
Output is correct |
41 |
Correct |
36 ms |
10440 KB |
Output is correct |
42 |
Correct |
35 ms |
10440 KB |
Output is correct |
43 |
Correct |
41 ms |
10364 KB |
Output is correct |
44 |
Correct |
38 ms |
10500 KB |
Output is correct |
45 |
Correct |
35 ms |
10440 KB |
Output is correct |
46 |
Correct |
36 ms |
10692 KB |
Output is correct |
47 |
Correct |
33 ms |
10332 KB |
Output is correct |
48 |
Correct |
42 ms |
10572 KB |
Output is correct |
49 |
Correct |
36 ms |
10436 KB |
Output is correct |
50 |
Correct |
35 ms |
10420 KB |
Output is correct |
51 |
Correct |
402 ms |
97788 KB |
Output is correct |
52 |
Correct |
394 ms |
97668 KB |
Output is correct |
53 |
Correct |
418 ms |
97744 KB |
Output is correct |
54 |
Correct |
36 ms |
10436 KB |
Output is correct |
55 |
Correct |
35 ms |
10496 KB |
Output is correct |
56 |
Correct |
74 ms |
14896 KB |
Output is correct |
57 |
Correct |
0 ms |
348 KB |
Output is correct |
58 |
Correct |
36 ms |
8148 KB |
Output is correct |
59 |
Correct |
36 ms |
8148 KB |
Output is correct |
60 |
Correct |
417 ms |
98024 KB |
Output is correct |
61 |
Correct |
37 ms |
10448 KB |
Output is correct |
62 |
Correct |
36 ms |
10444 KB |
Output is correct |
63 |
Correct |
38 ms |
10528 KB |
Output is correct |
64 |
Correct |
40 ms |
10472 KB |
Output is correct |
65 |
Correct |
36 ms |
10556 KB |
Output is correct |
66 |
Correct |
37 ms |
10436 KB |
Output is correct |
67 |
Correct |
37 ms |
10488 KB |
Output is correct |
68 |
Correct |
37 ms |
10580 KB |
Output is correct |
69 |
Correct |
37 ms |
10560 KB |
Output is correct |
70 |
Correct |
412 ms |
98348 KB |
Output is correct |
71 |
Correct |
38 ms |
9680 KB |
Output is correct |
72 |
Correct |
406 ms |
98024 KB |
Output is correct |
73 |
Correct |
416 ms |
97708 KB |
Output is correct |
74 |
Correct |
0 ms |
348 KB |
Output is correct |
75 |
Correct |
1 ms |
344 KB |
Output is correct |
76 |
Correct |
0 ms |
348 KB |
Output is correct |
77 |
Correct |
0 ms |
348 KB |
Output is correct |
78 |
Correct |
0 ms |
348 KB |
Output is correct |
79 |
Correct |
4 ms |
1500 KB |
Output is correct |
80 |
Correct |
446 ms |
97860 KB |
Output is correct |
81 |
Correct |
425 ms |
97852 KB |
Output is correct |
82 |
Correct |
435 ms |
97836 KB |
Output is correct |
83 |
Correct |
424 ms |
97456 KB |
Output is correct |
84 |
Correct |
405 ms |
97456 KB |
Output is correct |
85 |
Correct |
435 ms |
97632 KB |
Output is correct |
86 |
Correct |
73 ms |
14272 KB |
Output is correct |
87 |
Correct |
0 ms |
344 KB |
Output is correct |