#include<bits/stdc++.h>
#define int long long
using namespace std;
const int N = 4000010;
long long res = 0;
int n;
long long A, B;
vector <pair <int, int>> opp[N];
long long gdc(long long x, long long y){
if(x < y) swap(x, y);
return (y == 0 ? x : gdc(y, x%y));
}
void corrigir(vector <pair <int, int>> &v){
vector <pair <int, int>> aux;
for(auto [l, r] : v){
if(r-l+1 >= A) {
aux.push_back({0, A-1});
continue;
}
l %= A;
r %= A;
if(l <= r) aux.push_back({l, r});
else{
aux.push_back({l, A-1});
aux.push_back({0,r});
}
}
v = aux;
sort(v.begin(), v.end());
aux.clear();
for(auto [l, r] : v){
//cout << l << ' ' << r << '\n';
if(!aux.empty()){
auto [a, b] = aux.back();
if(b < l) aux.push_back({l, r});
else{
aux.pop_back();
aux.push_back({a, max(b, r)});
}
}
else{
aux.push_back({l, r});
}
}
v = aux;
return;
}
int32_t main(){
ios::sync_with_stdio(false); cin.tie(0);
cin >> n >> A >> B;
A = A/gdc(A, B+1);
vector <pair <int, int>> queries;
vector <array <int, 3>> corners;
for(int i = 1;i <= n;i++){
int l, r;
cin >> l >> r;
if(r-l+1 <= B and r%B >= l%B){
int d = r/B;
corners.push_back({l%B, r%B, (d%A)});
continue;
}
if(l%B != 0){
int d = (l+B-1)/B;
corners.push_back({l%B, B-1, ((d-1)%A+A)%A});
}
int a = (l+B-1)/B;
int b = r/B;
if(r%B == B-1){
queries.push_back({a, b});
continue;
}
if(B*a <= B*b-1){
queries.push_back({a, b-1});
}
if(r%B != B-1){
corners.push_back({0, r%B, b%A});
}
}
corrigir(queries);
//set <int> s;
map <int, int> m;
map <int ,int> cnt;
int add = 0;
//cout << "cte:\n";
for(auto [l, r] : queries){
//cout << l << ' ' << r << '\n';
m[l] = r;
add += r-l+1;
//s.insert(l);
}
//cout << '\n';
set <int> valores;
for(auto [l, r, w] : corners){
//cout << l << ' ' << r << ' ' << w << '\n';
valores.insert(l);
valores.insert(r+1);
}
map <int, int> mapear;
int con = 0;
for(auto x : valores){
mapear[x] = con;
con++;
}
mapear[B] = con;
con++;
for(auto [l, r, w] : corners){
opp[mapear[l]].push_back({w, 0});
opp[mapear[r+1]].push_back({w, 1});
}
int st = 0;
for(auto [pos, valor] : mapear){
res += (pos-st)*add;
st = pos;
for(auto [x, tp] : opp[valor]){
if(tp == 0){
auto it = m.upper_bound(x);
if(it != m.begin()){
it--;
auto [_, p] = *it;
if(p >= x) continue;
}
if(cnt[x] == 0) add++;
cnt[x]++;
}
else{
auto it = cnt.find(x);
if(it == cnt.end()) continue;
auto [_, d] = *it;
if(d == 1){
cnt.erase(it);
add--;
continue;
}
cnt[x]--;
}
}
//cout << add << '\n';
}
cout << res << '\n';
return 0;
}
/*
10 7 1
3 5
8 11
26 33
40 52
65 69
83 95
113 123
135 136
154 166
182 185
*/
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
94280 KB |
Output is correct |
2 |
Correct |
49 ms |
95408 KB |
Output is correct |
3 |
Correct |
49 ms |
95556 KB |
Output is correct |
4 |
Correct |
42 ms |
94356 KB |
Output is correct |
5 |
Correct |
44 ms |
94372 KB |
Output is correct |
6 |
Correct |
48 ms |
94360 KB |
Output is correct |
7 |
Correct |
46 ms |
94280 KB |
Output is correct |
8 |
Correct |
46 ms |
94280 KB |
Output is correct |
9 |
Correct |
49 ms |
94424 KB |
Output is correct |
10 |
Correct |
47 ms |
94296 KB |
Output is correct |
11 |
Correct |
48 ms |
94288 KB |
Output is correct |
12 |
Correct |
46 ms |
94168 KB |
Output is correct |
13 |
Correct |
46 ms |
94392 KB |
Output is correct |
14 |
Correct |
47 ms |
94280 KB |
Output is correct |
15 |
Correct |
52 ms |
94280 KB |
Output is correct |
16 |
Correct |
60 ms |
95044 KB |
Output is correct |
17 |
Correct |
121 ms |
102068 KB |
Output is correct |
18 |
Correct |
65 ms |
94260 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
65 ms |
94280 KB |
Output is correct |
2 |
Correct |
71 ms |
94356 KB |
Output is correct |
3 |
Correct |
63 ms |
94368 KB |
Output is correct |
4 |
Correct |
72 ms |
94228 KB |
Output is correct |
5 |
Correct |
70 ms |
94248 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
66 ms |
94132 KB |
Output is correct |
2 |
Correct |
69 ms |
94552 KB |
Output is correct |
3 |
Correct |
65 ms |
94536 KB |
Output is correct |
4 |
Correct |
50 ms |
94536 KB |
Output is correct |
5 |
Correct |
299 ms |
134528 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
51 ms |
94268 KB |
Output is correct |
2 |
Correct |
753 ms |
221036 KB |
Output is correct |
3 |
Correct |
588 ms |
172468 KB |
Output is correct |
4 |
Correct |
587 ms |
172496 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
51 ms |
94268 KB |
Output is correct |
2 |
Correct |
753 ms |
221036 KB |
Output is correct |
3 |
Correct |
588 ms |
172468 KB |
Output is correct |
4 |
Correct |
587 ms |
172496 KB |
Output is correct |
5 |
Correct |
53 ms |
94280 KB |
Output is correct |
6 |
Correct |
2122 ms |
345340 KB |
Output is correct |
7 |
Correct |
629 ms |
186492 KB |
Output is correct |
8 |
Correct |
2133 ms |
345372 KB |
Output is correct |
9 |
Correct |
2183 ms |
353244 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
51 ms |
94268 KB |
Output is correct |
2 |
Correct |
753 ms |
221036 KB |
Output is correct |
3 |
Correct |
588 ms |
172468 KB |
Output is correct |
4 |
Correct |
587 ms |
172496 KB |
Output is correct |
5 |
Correct |
45 ms |
94288 KB |
Output is correct |
6 |
Correct |
120 ms |
112056 KB |
Output is correct |
7 |
Correct |
201 ms |
121272 KB |
Output is correct |
8 |
Correct |
192 ms |
121272 KB |
Output is correct |
9 |
Correct |
190 ms |
121228 KB |
Output is correct |
10 |
Correct |
134 ms |
113040 KB |
Output is correct |
11 |
Correct |
217 ms |
121372 KB |
Output is correct |
12 |
Correct |
215 ms |
121272 KB |
Output is correct |
13 |
Correct |
218 ms |
119740 KB |
Output is correct |
14 |
Correct |
145 ms |
112308 KB |
Output is correct |
15 |
Correct |
218 ms |
114044 KB |
Output is correct |
16 |
Correct |
192 ms |
112440 KB |
Output is correct |
17 |
Correct |
406 ms |
131804 KB |
Output is correct |
18 |
Correct |
3080 ms |
386200 KB |
Output is correct |
19 |
Correct |
3544 ms |
389524 KB |
Output is correct |
20 |
Correct |
3495 ms |
389764 KB |
Output is correct |
21 |
Correct |
253 ms |
123836 KB |
Output is correct |
22 |
Correct |
254 ms |
123836 KB |
Output is correct |
23 |
Correct |
178 ms |
124484 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
96 ms |
94288 KB |
Output is correct |
2 |
Correct |
188 ms |
112316 KB |
Output is correct |
3 |
Correct |
222 ms |
114672 KB |
Output is correct |
4 |
Correct |
4176 ms |
342608 KB |
Output is correct |
5 |
Correct |
248 ms |
116988 KB |
Output is correct |
6 |
Correct |
212 ms |
116956 KB |
Output is correct |
7 |
Correct |
228 ms |
116916 KB |
Output is correct |
8 |
Correct |
256 ms |
118452 KB |
Output is correct |
9 |
Correct |
255 ms |
118452 KB |
Output is correct |
10 |
Correct |
184 ms |
116148 KB |
Output is correct |
11 |
Correct |
156 ms |
115924 KB |
Output is correct |
12 |
Correct |
173 ms |
115940 KB |
Output is correct |
13 |
Correct |
231 ms |
116544 KB |
Output is correct |
14 |
Correct |
1462 ms |
248940 KB |
Output is correct |
15 |
Correct |
143 ms |
108532 KB |
Output is correct |
16 |
Correct |
1917 ms |
311416 KB |
Output is correct |
17 |
Correct |
1027 ms |
237796 KB |
Output is correct |
18 |
Correct |
47 ms |
94280 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
37 ms |
94280 KB |
Output is correct |
2 |
Correct |
49 ms |
95408 KB |
Output is correct |
3 |
Correct |
49 ms |
95556 KB |
Output is correct |
4 |
Correct |
42 ms |
94356 KB |
Output is correct |
5 |
Correct |
44 ms |
94372 KB |
Output is correct |
6 |
Correct |
48 ms |
94360 KB |
Output is correct |
7 |
Correct |
46 ms |
94280 KB |
Output is correct |
8 |
Correct |
46 ms |
94280 KB |
Output is correct |
9 |
Correct |
49 ms |
94424 KB |
Output is correct |
10 |
Correct |
47 ms |
94296 KB |
Output is correct |
11 |
Correct |
48 ms |
94288 KB |
Output is correct |
12 |
Correct |
46 ms |
94168 KB |
Output is correct |
13 |
Correct |
46 ms |
94392 KB |
Output is correct |
14 |
Correct |
47 ms |
94280 KB |
Output is correct |
15 |
Correct |
52 ms |
94280 KB |
Output is correct |
16 |
Correct |
60 ms |
95044 KB |
Output is correct |
17 |
Correct |
121 ms |
102068 KB |
Output is correct |
18 |
Correct |
65 ms |
94260 KB |
Output is correct |
19 |
Correct |
65 ms |
94280 KB |
Output is correct |
20 |
Correct |
71 ms |
94356 KB |
Output is correct |
21 |
Correct |
63 ms |
94368 KB |
Output is correct |
22 |
Correct |
72 ms |
94228 KB |
Output is correct |
23 |
Correct |
70 ms |
94248 KB |
Output is correct |
24 |
Correct |
66 ms |
94132 KB |
Output is correct |
25 |
Correct |
69 ms |
94552 KB |
Output is correct |
26 |
Correct |
65 ms |
94536 KB |
Output is correct |
27 |
Correct |
50 ms |
94536 KB |
Output is correct |
28 |
Correct |
299 ms |
134528 KB |
Output is correct |
29 |
Correct |
51 ms |
94268 KB |
Output is correct |
30 |
Correct |
753 ms |
221036 KB |
Output is correct |
31 |
Correct |
588 ms |
172468 KB |
Output is correct |
32 |
Correct |
587 ms |
172496 KB |
Output is correct |
33 |
Correct |
53 ms |
94280 KB |
Output is correct |
34 |
Correct |
2122 ms |
345340 KB |
Output is correct |
35 |
Correct |
629 ms |
186492 KB |
Output is correct |
36 |
Correct |
2133 ms |
345372 KB |
Output is correct |
37 |
Correct |
2183 ms |
353244 KB |
Output is correct |
38 |
Correct |
45 ms |
94288 KB |
Output is correct |
39 |
Correct |
120 ms |
112056 KB |
Output is correct |
40 |
Correct |
201 ms |
121272 KB |
Output is correct |
41 |
Correct |
192 ms |
121272 KB |
Output is correct |
42 |
Correct |
190 ms |
121228 KB |
Output is correct |
43 |
Correct |
134 ms |
113040 KB |
Output is correct |
44 |
Correct |
217 ms |
121372 KB |
Output is correct |
45 |
Correct |
215 ms |
121272 KB |
Output is correct |
46 |
Correct |
218 ms |
119740 KB |
Output is correct |
47 |
Correct |
145 ms |
112308 KB |
Output is correct |
48 |
Correct |
218 ms |
114044 KB |
Output is correct |
49 |
Correct |
192 ms |
112440 KB |
Output is correct |
50 |
Correct |
406 ms |
131804 KB |
Output is correct |
51 |
Correct |
3080 ms |
386200 KB |
Output is correct |
52 |
Correct |
3544 ms |
389524 KB |
Output is correct |
53 |
Correct |
3495 ms |
389764 KB |
Output is correct |
54 |
Correct |
253 ms |
123836 KB |
Output is correct |
55 |
Correct |
254 ms |
123836 KB |
Output is correct |
56 |
Correct |
178 ms |
124484 KB |
Output is correct |
57 |
Correct |
96 ms |
94288 KB |
Output is correct |
58 |
Correct |
188 ms |
112316 KB |
Output is correct |
59 |
Correct |
222 ms |
114672 KB |
Output is correct |
60 |
Correct |
4176 ms |
342608 KB |
Output is correct |
61 |
Correct |
248 ms |
116988 KB |
Output is correct |
62 |
Correct |
212 ms |
116956 KB |
Output is correct |
63 |
Correct |
228 ms |
116916 KB |
Output is correct |
64 |
Correct |
256 ms |
118452 KB |
Output is correct |
65 |
Correct |
255 ms |
118452 KB |
Output is correct |
66 |
Correct |
184 ms |
116148 KB |
Output is correct |
67 |
Correct |
156 ms |
115924 KB |
Output is correct |
68 |
Correct |
173 ms |
115940 KB |
Output is correct |
69 |
Correct |
231 ms |
116544 KB |
Output is correct |
70 |
Correct |
1462 ms |
248940 KB |
Output is correct |
71 |
Correct |
143 ms |
108532 KB |
Output is correct |
72 |
Correct |
1917 ms |
311416 KB |
Output is correct |
73 |
Correct |
1027 ms |
237796 KB |
Output is correct |
74 |
Correct |
47 ms |
94280 KB |
Output is correct |
75 |
Correct |
43 ms |
94236 KB |
Output is correct |
76 |
Correct |
44 ms |
94280 KB |
Output is correct |
77 |
Correct |
47 ms |
94328 KB |
Output is correct |
78 |
Correct |
47 ms |
94272 KB |
Output is correct |
79 |
Correct |
61 ms |
96080 KB |
Output is correct |
80 |
Correct |
1369 ms |
214372 KB |
Output is correct |
81 |
Correct |
4121 ms |
358636 KB |
Output is correct |
82 |
Correct |
4802 ms |
409948 KB |
Output is correct |
83 |
Execution timed out |
5091 ms |
507268 KB |
Time limit exceeded |
84 |
Halted |
0 ms |
0 KB |
- |