//#pragma GCC optimize ("O3")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl '\n'
const int DIM = 1E6+7;
pair<ll,ll> M[DIM];
const ll MAXN = 2E18;
ll mult(ll a,ll b){
ll mult = b;
b = 0;
while(a){
if (a&1)
b = min(b+mult,MAXN);
mult = min(MAXN,mult*2);
a/=2;
}
return b;
}
int solve(){
ll n,A,B;
cin>>n>>A>>B;
for(int i = 1;i<=n;++i){
cin>>M[i].first>>M[i].second;
}
ll gcd = __gcd(A,(B+1)%A);
ll d = A/gcd;
B = d*B;
vector<pair<ll,ll> > V;
ll cnt = 0,sum = 0;
for(int i = 1;i<=n;++i){
sum+=M[i].second-M[i].first+1;
if (M[i].first%B!=0) {
ll start = M[i].first % B;
ll fin = B - 1;
if (fin - start > M[i].second - M[i].first) {
V.push_back({M[i].first % B, 1});
V.push_back({M[i].second % B + 1, -1});
continue;
}
V.push_back({start, 1});
V.push_back({fin + 1, -1});
M[i].first += fin - start + 1;
}
if (M[i].second%B!=B-1) {
ll fin = M[i].second % B;
ll start = 0;
if (fin - start > M[i].second - M[i].first) {
V.push_back({M[i].first % B, 1});
V.push_back({M[i].second % B + 1, -1});
continue;
}
V.push_back({start, 1});
V.push_back({fin + 1, -1});
M[i].second -= fin - start + 1;
}
cnt += (M[i].second - M[i].first+1) / B;
}
sort(V.begin(),V.end());
ll last = 0,res = 0;
for(int i = 0;i<int(V.size());++i){
res+=(V[i].first-last)*max(0ll,cnt-1);
last = V[i].first;
int cur = V[i].first;
cnt+=V[i].second;
while(i+1<int(V.size()) && V[i+1].first==cur){
cnt+=V[++i].second;
}
}
res+=(B-last)*max(0ll,cnt-1);
cout<<sum-res<<endl;
return 1;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
assert(solve());
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
7 ms |
1104 KB |
Output is correct |
3 |
Correct |
7 ms |
1104 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
7 ms |
1104 KB |
Output is correct |
17 |
Correct |
89 ms |
6120 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
1 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
204 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
2 ms |
332 KB |
Output is correct |
3 |
Correct |
1 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
332 KB |
Output is correct |
5 |
Correct |
455 ms |
48904 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
679 ms |
48832 KB |
Output is correct |
3 |
Correct |
806 ms |
86024 KB |
Output is correct |
4 |
Correct |
660 ms |
86136 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
679 ms |
48832 KB |
Output is correct |
3 |
Correct |
806 ms |
86024 KB |
Output is correct |
4 |
Correct |
660 ms |
86136 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
658 ms |
86096 KB |
Output is correct |
7 |
Correct |
638 ms |
86116 KB |
Output is correct |
8 |
Correct |
618 ms |
86164 KB |
Output is correct |
9 |
Correct |
760 ms |
86048 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
679 ms |
48832 KB |
Output is correct |
3 |
Correct |
806 ms |
86024 KB |
Output is correct |
4 |
Correct |
660 ms |
86136 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
81 ms |
9808 KB |
Output is correct |
7 |
Correct |
61 ms |
9744 KB |
Output is correct |
8 |
Correct |
61 ms |
9732 KB |
Output is correct |
9 |
Correct |
80 ms |
9756 KB |
Output is correct |
10 |
Correct |
64 ms |
9772 KB |
Output is correct |
11 |
Correct |
62 ms |
9784 KB |
Output is correct |
12 |
Correct |
69 ms |
9788 KB |
Output is correct |
13 |
Correct |
84 ms |
9752 KB |
Output is correct |
14 |
Correct |
68 ms |
9844 KB |
Output is correct |
15 |
Correct |
69 ms |
9836 KB |
Output is correct |
16 |
Correct |
72 ms |
9816 KB |
Output is correct |
17 |
Correct |
61 ms |
9780 KB |
Output is correct |
18 |
Correct |
724 ms |
86064 KB |
Output is correct |
19 |
Correct |
662 ms |
86056 KB |
Output is correct |
20 |
Correct |
848 ms |
86180 KB |
Output is correct |
21 |
Correct |
77 ms |
9788 KB |
Output is correct |
22 |
Correct |
58 ms |
9732 KB |
Output is correct |
23 |
Correct |
181 ms |
34736 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
66 ms |
8796 KB |
Output is correct |
3 |
Correct |
69 ms |
9916 KB |
Output is correct |
4 |
Correct |
838 ms |
86020 KB |
Output is correct |
5 |
Correct |
66 ms |
9788 KB |
Output is correct |
6 |
Correct |
78 ms |
9756 KB |
Output is correct |
7 |
Correct |
88 ms |
9816 KB |
Output is correct |
8 |
Correct |
72 ms |
9800 KB |
Output is correct |
9 |
Correct |
70 ms |
9924 KB |
Output is correct |
10 |
Correct |
74 ms |
9752 KB |
Output is correct |
11 |
Correct |
69 ms |
9780 KB |
Output is correct |
12 |
Correct |
59 ms |
9784 KB |
Output is correct |
13 |
Correct |
68 ms |
9804 KB |
Output is correct |
14 |
Correct |
790 ms |
86052 KB |
Output is correct |
15 |
Correct |
77 ms |
9816 KB |
Output is correct |
16 |
Correct |
600 ms |
86144 KB |
Output is correct |
17 |
Correct |
708 ms |
86108 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
7 ms |
1104 KB |
Output is correct |
3 |
Correct |
7 ms |
1104 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
204 KB |
Output is correct |
10 |
Correct |
1 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
1 ms |
204 KB |
Output is correct |
14 |
Correct |
1 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
7 ms |
1104 KB |
Output is correct |
17 |
Correct |
89 ms |
6120 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
1 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
204 KB |
Output is correct |
22 |
Correct |
1 ms |
204 KB |
Output is correct |
23 |
Correct |
1 ms |
204 KB |
Output is correct |
24 |
Correct |
1 ms |
204 KB |
Output is correct |
25 |
Correct |
2 ms |
332 KB |
Output is correct |
26 |
Correct |
1 ms |
332 KB |
Output is correct |
27 |
Correct |
1 ms |
332 KB |
Output is correct |
28 |
Correct |
455 ms |
48904 KB |
Output is correct |
29 |
Correct |
1 ms |
204 KB |
Output is correct |
30 |
Correct |
679 ms |
48832 KB |
Output is correct |
31 |
Correct |
806 ms |
86024 KB |
Output is correct |
32 |
Correct |
660 ms |
86136 KB |
Output is correct |
33 |
Correct |
1 ms |
204 KB |
Output is correct |
34 |
Correct |
658 ms |
86096 KB |
Output is correct |
35 |
Correct |
638 ms |
86116 KB |
Output is correct |
36 |
Correct |
618 ms |
86164 KB |
Output is correct |
37 |
Correct |
760 ms |
86048 KB |
Output is correct |
38 |
Correct |
1 ms |
204 KB |
Output is correct |
39 |
Correct |
81 ms |
9808 KB |
Output is correct |
40 |
Correct |
61 ms |
9744 KB |
Output is correct |
41 |
Correct |
61 ms |
9732 KB |
Output is correct |
42 |
Correct |
80 ms |
9756 KB |
Output is correct |
43 |
Correct |
64 ms |
9772 KB |
Output is correct |
44 |
Correct |
62 ms |
9784 KB |
Output is correct |
45 |
Correct |
69 ms |
9788 KB |
Output is correct |
46 |
Correct |
84 ms |
9752 KB |
Output is correct |
47 |
Correct |
68 ms |
9844 KB |
Output is correct |
48 |
Correct |
69 ms |
9836 KB |
Output is correct |
49 |
Correct |
72 ms |
9816 KB |
Output is correct |
50 |
Correct |
61 ms |
9780 KB |
Output is correct |
51 |
Correct |
724 ms |
86064 KB |
Output is correct |
52 |
Correct |
662 ms |
86056 KB |
Output is correct |
53 |
Correct |
848 ms |
86180 KB |
Output is correct |
54 |
Correct |
77 ms |
9788 KB |
Output is correct |
55 |
Correct |
58 ms |
9732 KB |
Output is correct |
56 |
Correct |
181 ms |
34736 KB |
Output is correct |
57 |
Correct |
1 ms |
204 KB |
Output is correct |
58 |
Correct |
66 ms |
8796 KB |
Output is correct |
59 |
Correct |
69 ms |
9916 KB |
Output is correct |
60 |
Correct |
838 ms |
86020 KB |
Output is correct |
61 |
Correct |
66 ms |
9788 KB |
Output is correct |
62 |
Correct |
78 ms |
9756 KB |
Output is correct |
63 |
Correct |
88 ms |
9816 KB |
Output is correct |
64 |
Correct |
72 ms |
9800 KB |
Output is correct |
65 |
Correct |
70 ms |
9924 KB |
Output is correct |
66 |
Correct |
74 ms |
9752 KB |
Output is correct |
67 |
Correct |
69 ms |
9780 KB |
Output is correct |
68 |
Correct |
59 ms |
9784 KB |
Output is correct |
69 |
Correct |
68 ms |
9804 KB |
Output is correct |
70 |
Correct |
790 ms |
86052 KB |
Output is correct |
71 |
Correct |
77 ms |
9816 KB |
Output is correct |
72 |
Correct |
600 ms |
86144 KB |
Output is correct |
73 |
Correct |
708 ms |
86108 KB |
Output is correct |
74 |
Correct |
1 ms |
204 KB |
Output is correct |
75 |
Correct |
1 ms |
320 KB |
Output is correct |
76 |
Correct |
1 ms |
204 KB |
Output is correct |
77 |
Correct |
1 ms |
204 KB |
Output is correct |
78 |
Correct |
1 ms |
204 KB |
Output is correct |
79 |
Correct |
6 ms |
1488 KB |
Output is correct |
80 |
Correct |
794 ms |
86176 KB |
Output is correct |
81 |
Correct |
887 ms |
86084 KB |
Output is correct |
82 |
Correct |
855 ms |
86060 KB |
Output is correct |
83 |
Correct |
842 ms |
86116 KB |
Output is correct |
84 |
Correct |
786 ms |
86092 KB |
Output is correct |
85 |
Correct |
838 ms |
86040 KB |
Output is correct |
86 |
Correct |
242 ms |
34740 KB |
Output is correct |
87 |
Correct |
1 ms |
332 KB |
Output is correct |