#include<bits/stdc++.h>
using namespace std;
#define f first
#define s second
typedef long long ll;
const int maxn = 2e6+5;
int n, sz;
ll a, b;
pair<ll, ll> A[maxn];
vector<pair<int, int>> qq[maxn];
map<ll, int> mp;
vector<pair<ll, ll>> rng, vec;
vector<ll> pos;
int main(){
ios::sync_with_stdio(false);
cin.tie(0);
cin >> n >> a >> b;
ll cyc = a/__gcd(a, b+1);
for(int i = 0; i < n; i++){
cin >> A[i].f >> A[i].s;
ll lb = A[i].f/b+1, rb = A[i].s/b-1;
if(rb >= lb){
if(rb - lb+1 >= cyc)rng.push_back({0, cyc-1});
else{
ll lm = lb%cyc, rm = rb%cyc;
if(lm <= rm)vec.push_back({lm, rm});
else{
vec.push_back({lm, cyc-1});
vec.push_back({0, rm});
}
}
}
pos.push_back(A[i].f%b);
pos.push_back(A[i].s%b + 1);
}
pos.push_back(-1);
pos.push_back(0);
pos.push_back(b);
sort(pos.begin(), pos.end());
pos.erase(unique(pos.begin(), pos.end()), pos.end());
sort(vec.begin(), vec.end());
for(auto v: vec){
if(rng.empty() || rng.back().s < v.f-1)rng.push_back(v);
else rng.back().s = max(rng.back().s, v.s);
}
sz = pos.size()-1;
for(int i = 0; i < n; i++){
ll lb = A[i].f/b, rb = A[i].s/b;
if(lb == rb){
int pp = lower_bound(rng.begin(), rng.end(), make_pair(lb%cyc, cyc+1))-rng.begin()-1;
if(pp < 0 || rng[pp].s < lb%cyc){
int ql = lower_bound(pos.begin(), pos.end(), A[i].f%b)-pos.begin(), qr = upper_bound(pos.begin(), pos.end(), A[i].s%b) - pos.begin();
qq[ql].push_back({lb%cyc, 0});
qq[qr].push_back({lb%cyc, 1});
}
}
else{
int pp = lower_bound(rng.begin(), rng.end(), make_pair(lb%cyc, cyc+1))-rng.begin()-1;
if(pp < 0 || rng[pp].s < lb%cyc){
int ql = lower_bound(pos.begin(), pos.end(), A[i].f%b)-pos.begin();
qq[ql].push_back({lb%cyc, 0});
}
pp = lower_bound(rng.begin(), rng.end(), make_pair(rb%cyc, cyc+1))-rng.begin()-1;
if(pp < 0 || rng[pp].s < rb%cyc){
int qr = upper_bound(pos.begin(), pos.end(), A[i].s%b) - pos.begin();
qq[1].push_back({rb%cyc, 0});
qq[qr].push_back({rb%cyc, 1});
}
}
}
int crr = 0;
ll ans = 0;
for(int i = 1; i < sz; i++){
for(auto v: qq[i]){
if(v.s){
mp[v.f]--;
if(!mp[v.f])crr--;
}
else{
if(!mp[v.f])crr++;
mp[v.f]++;
}
}
ans += (pos[i+1]-pos[i])*crr;
}
for(auto v: rng){
ans+=b*(v.s-v.f+1);
}
cout << ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
47264 KB |
Output is correct |
2 |
Correct |
34 ms |
47832 KB |
Output is correct |
3 |
Correct |
36 ms |
47828 KB |
Output is correct |
4 |
Correct |
30 ms |
47260 KB |
Output is correct |
5 |
Correct |
26 ms |
47188 KB |
Output is correct |
6 |
Correct |
25 ms |
47288 KB |
Output is correct |
7 |
Correct |
26 ms |
47188 KB |
Output is correct |
8 |
Correct |
26 ms |
47228 KB |
Output is correct |
9 |
Correct |
24 ms |
47212 KB |
Output is correct |
10 |
Correct |
25 ms |
47248 KB |
Output is correct |
11 |
Correct |
25 ms |
47284 KB |
Output is correct |
12 |
Correct |
31 ms |
47248 KB |
Output is correct |
13 |
Correct |
24 ms |
47200 KB |
Output is correct |
14 |
Correct |
26 ms |
47188 KB |
Output is correct |
15 |
Correct |
29 ms |
47296 KB |
Output is correct |
16 |
Correct |
30 ms |
47912 KB |
Output is correct |
17 |
Correct |
82 ms |
52672 KB |
Output is correct |
18 |
Correct |
25 ms |
47188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
47264 KB |
Output is correct |
2 |
Correct |
24 ms |
47188 KB |
Output is correct |
3 |
Correct |
24 ms |
47204 KB |
Output is correct |
4 |
Correct |
26 ms |
47212 KB |
Output is correct |
5 |
Correct |
29 ms |
47216 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
22 ms |
47188 KB |
Output is correct |
2 |
Correct |
23 ms |
47444 KB |
Output is correct |
3 |
Correct |
23 ms |
47272 KB |
Output is correct |
4 |
Correct |
24 ms |
47332 KB |
Output is correct |
5 |
Correct |
463 ms |
126300 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
47248 KB |
Output is correct |
2 |
Correct |
813 ms |
157180 KB |
Output is correct |
3 |
Correct |
1172 ms |
259124 KB |
Output is correct |
4 |
Correct |
1898 ms |
259116 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
47248 KB |
Output is correct |
2 |
Correct |
813 ms |
157180 KB |
Output is correct |
3 |
Correct |
1172 ms |
259124 KB |
Output is correct |
4 |
Correct |
1898 ms |
259116 KB |
Output is correct |
5 |
Correct |
22 ms |
47236 KB |
Output is correct |
6 |
Correct |
1459 ms |
195952 KB |
Output is correct |
7 |
Correct |
838 ms |
118188 KB |
Output is correct |
8 |
Correct |
1135 ms |
195960 KB |
Output is correct |
9 |
Correct |
1763 ms |
196024 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
24 ms |
47248 KB |
Output is correct |
2 |
Correct |
813 ms |
157180 KB |
Output is correct |
3 |
Correct |
1172 ms |
259124 KB |
Output is correct |
4 |
Correct |
1898 ms |
259116 KB |
Output is correct |
5 |
Correct |
28 ms |
47188 KB |
Output is correct |
6 |
Correct |
123 ms |
59076 KB |
Output is correct |
7 |
Correct |
154 ms |
62748 KB |
Output is correct |
8 |
Correct |
141 ms |
63144 KB |
Output is correct |
9 |
Correct |
170 ms |
63200 KB |
Output is correct |
10 |
Correct |
137 ms |
58088 KB |
Output is correct |
11 |
Correct |
178 ms |
62940 KB |
Output is correct |
12 |
Correct |
150 ms |
62692 KB |
Output is correct |
13 |
Correct |
237 ms |
62412 KB |
Output is correct |
14 |
Correct |
103 ms |
53588 KB |
Output is correct |
15 |
Correct |
256 ms |
60116 KB |
Output is correct |
16 |
Correct |
136 ms |
53564 KB |
Output is correct |
17 |
Correct |
251 ms |
63732 KB |
Output is correct |
18 |
Correct |
2220 ms |
204856 KB |
Output is correct |
19 |
Correct |
2709 ms |
199472 KB |
Output is correct |
20 |
Correct |
2316 ms |
199556 KB |
Output is correct |
21 |
Correct |
224 ms |
62572 KB |
Output is correct |
22 |
Correct |
176 ms |
62448 KB |
Output is correct |
23 |
Correct |
220 ms |
76868 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
32 ms |
47256 KB |
Output is correct |
2 |
Correct |
121 ms |
53544 KB |
Output is correct |
3 |
Correct |
151 ms |
53576 KB |
Output is correct |
4 |
Correct |
2867 ms |
138412 KB |
Output is correct |
5 |
Correct |
153 ms |
54152 KB |
Output is correct |
6 |
Correct |
156 ms |
53988 KB |
Output is correct |
7 |
Correct |
163 ms |
54264 KB |
Output is correct |
8 |
Correct |
205 ms |
55036 KB |
Output is correct |
9 |
Correct |
193 ms |
55104 KB |
Output is correct |
10 |
Correct |
114 ms |
53592 KB |
Output is correct |
11 |
Correct |
119 ms |
53508 KB |
Output is correct |
12 |
Correct |
115 ms |
53500 KB |
Output is correct |
13 |
Correct |
155 ms |
54160 KB |
Output is correct |
14 |
Correct |
1163 ms |
102372 KB |
Output is correct |
15 |
Correct |
305 ms |
52936 KB |
Output is correct |
16 |
Correct |
1012 ms |
109896 KB |
Output is correct |
17 |
Correct |
1634 ms |
123968 KB |
Output is correct |
18 |
Correct |
24 ms |
47188 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
25 ms |
47264 KB |
Output is correct |
2 |
Correct |
34 ms |
47832 KB |
Output is correct |
3 |
Correct |
36 ms |
47828 KB |
Output is correct |
4 |
Correct |
30 ms |
47260 KB |
Output is correct |
5 |
Correct |
26 ms |
47188 KB |
Output is correct |
6 |
Correct |
25 ms |
47288 KB |
Output is correct |
7 |
Correct |
26 ms |
47188 KB |
Output is correct |
8 |
Correct |
26 ms |
47228 KB |
Output is correct |
9 |
Correct |
24 ms |
47212 KB |
Output is correct |
10 |
Correct |
25 ms |
47248 KB |
Output is correct |
11 |
Correct |
25 ms |
47284 KB |
Output is correct |
12 |
Correct |
31 ms |
47248 KB |
Output is correct |
13 |
Correct |
24 ms |
47200 KB |
Output is correct |
14 |
Correct |
26 ms |
47188 KB |
Output is correct |
15 |
Correct |
29 ms |
47296 KB |
Output is correct |
16 |
Correct |
30 ms |
47912 KB |
Output is correct |
17 |
Correct |
82 ms |
52672 KB |
Output is correct |
18 |
Correct |
25 ms |
47188 KB |
Output is correct |
19 |
Correct |
25 ms |
47264 KB |
Output is correct |
20 |
Correct |
24 ms |
47188 KB |
Output is correct |
21 |
Correct |
24 ms |
47204 KB |
Output is correct |
22 |
Correct |
26 ms |
47212 KB |
Output is correct |
23 |
Correct |
29 ms |
47216 KB |
Output is correct |
24 |
Correct |
22 ms |
47188 KB |
Output is correct |
25 |
Correct |
23 ms |
47444 KB |
Output is correct |
26 |
Correct |
23 ms |
47272 KB |
Output is correct |
27 |
Correct |
24 ms |
47332 KB |
Output is correct |
28 |
Correct |
463 ms |
126300 KB |
Output is correct |
29 |
Correct |
24 ms |
47248 KB |
Output is correct |
30 |
Correct |
813 ms |
157180 KB |
Output is correct |
31 |
Correct |
1172 ms |
259124 KB |
Output is correct |
32 |
Correct |
1898 ms |
259116 KB |
Output is correct |
33 |
Correct |
22 ms |
47236 KB |
Output is correct |
34 |
Correct |
1459 ms |
195952 KB |
Output is correct |
35 |
Correct |
838 ms |
118188 KB |
Output is correct |
36 |
Correct |
1135 ms |
195960 KB |
Output is correct |
37 |
Correct |
1763 ms |
196024 KB |
Output is correct |
38 |
Correct |
28 ms |
47188 KB |
Output is correct |
39 |
Correct |
123 ms |
59076 KB |
Output is correct |
40 |
Correct |
154 ms |
62748 KB |
Output is correct |
41 |
Correct |
141 ms |
63144 KB |
Output is correct |
42 |
Correct |
170 ms |
63200 KB |
Output is correct |
43 |
Correct |
137 ms |
58088 KB |
Output is correct |
44 |
Correct |
178 ms |
62940 KB |
Output is correct |
45 |
Correct |
150 ms |
62692 KB |
Output is correct |
46 |
Correct |
237 ms |
62412 KB |
Output is correct |
47 |
Correct |
103 ms |
53588 KB |
Output is correct |
48 |
Correct |
256 ms |
60116 KB |
Output is correct |
49 |
Correct |
136 ms |
53564 KB |
Output is correct |
50 |
Correct |
251 ms |
63732 KB |
Output is correct |
51 |
Correct |
2220 ms |
204856 KB |
Output is correct |
52 |
Correct |
2709 ms |
199472 KB |
Output is correct |
53 |
Correct |
2316 ms |
199556 KB |
Output is correct |
54 |
Correct |
224 ms |
62572 KB |
Output is correct |
55 |
Correct |
176 ms |
62448 KB |
Output is correct |
56 |
Correct |
220 ms |
76868 KB |
Output is correct |
57 |
Correct |
32 ms |
47256 KB |
Output is correct |
58 |
Correct |
121 ms |
53544 KB |
Output is correct |
59 |
Correct |
151 ms |
53576 KB |
Output is correct |
60 |
Correct |
2867 ms |
138412 KB |
Output is correct |
61 |
Correct |
153 ms |
54152 KB |
Output is correct |
62 |
Correct |
156 ms |
53988 KB |
Output is correct |
63 |
Correct |
163 ms |
54264 KB |
Output is correct |
64 |
Correct |
205 ms |
55036 KB |
Output is correct |
65 |
Correct |
193 ms |
55104 KB |
Output is correct |
66 |
Correct |
114 ms |
53592 KB |
Output is correct |
67 |
Correct |
119 ms |
53508 KB |
Output is correct |
68 |
Correct |
115 ms |
53500 KB |
Output is correct |
69 |
Correct |
155 ms |
54160 KB |
Output is correct |
70 |
Correct |
1163 ms |
102372 KB |
Output is correct |
71 |
Correct |
305 ms |
52936 KB |
Output is correct |
72 |
Correct |
1012 ms |
109896 KB |
Output is correct |
73 |
Correct |
1634 ms |
123968 KB |
Output is correct |
74 |
Correct |
24 ms |
47188 KB |
Output is correct |
75 |
Correct |
25 ms |
47188 KB |
Output is correct |
76 |
Correct |
24 ms |
47288 KB |
Output is correct |
77 |
Correct |
27 ms |
47236 KB |
Output is correct |
78 |
Correct |
28 ms |
47248 KB |
Output is correct |
79 |
Correct |
39 ms |
48288 KB |
Output is correct |
80 |
Correct |
1347 ms |
100752 KB |
Output is correct |
81 |
Correct |
3270 ms |
138528 KB |
Output is correct |
82 |
Correct |
2884 ms |
202228 KB |
Output is correct |
83 |
Correct |
3795 ms |
211796 KB |
Output is correct |
84 |
Correct |
3924 ms |
248904 KB |
Output is correct |
85 |
Correct |
3346 ms |
241316 KB |
Output is correct |
86 |
Correct |
218 ms |
89236 KB |
Output is correct |
87 |
Correct |
26 ms |
47292 KB |
Output is correct |