#pragma GCC optimize("O3")
//#pragma GCC optimize("unroll-loops")
#pragma GCC optimize("fast-math")
//#pragma GCC optimize("Ofast")
#include<bits/stdc++.h>
#define ll long long
using namespace std;
const long long N = 4000010;
long long res = 0;
long long n;
long long A, B;
vector <pair <long long, long long>> 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 <long long, long long>> &v){
vector <pair <long long, long long>> 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 <long long, long long>> queries;
vector <array <long long, 3>> corners;
if(log2(A) + log2(B) > 60){
ll tot = 0;
for(int i = 1; i <= n; ++i){
ll t1, t2; cin >> t1 >> t2;
tot += t2 - t1 + 1;
}
cout << tot;
return 0;
}
for(int i = 1;i <= n;i++){
long long l, r;
cin >> l >> r;
if(r-l+1 <= B and r%B >= l%B){
long long d = r/B;
corners.push_back({l%B, r%B, (d%A)});
continue;
}
if(l%B != 0){
long long d = (l+B-1)/B;
corners.push_back({l%B, B-1, ((d-1)%A+A)%A});
}
long long a = (l+B-1)/B;
long long 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 <long long> s;
//map <long long, long long> m;
vector <pair <long long, long long>> v;
map <long long ,int> cnt;
long long add = 0;
//cout << "cte:\n";
for(auto [l, r] : queries){
//cout << l << ' ' << r << '\n';
v.push_back({l, r});
add += r-l+1;
//s.insert(l);
}
//cout << '\n';
set <long long> valores;
for(auto [l, r, w] : corners){
//cout << l << ' ' << r << ' ' << w << '\n';
valores.insert(l);
valores.insert(r+1);
}
map <long long, 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});
}
long long st = 0;
for(auto [pos, valor] : mapear){
res += (pos-st)*add;
st = pos;
for(auto [x, tp] : opp[valor]){
if(tp == 0){
long long d = upper_bound(v.begin(), v.end(), make_pair(x, (long long)1e18+1))-v.begin();
if(d != 0) {
d--;
if(v[d].second >= 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;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
94288 KB |
Output is correct |
2 |
Correct |
43 ms |
95432 KB |
Output is correct |
3 |
Correct |
39 ms |
95580 KB |
Output is correct |
4 |
Correct |
30 ms |
94288 KB |
Output is correct |
5 |
Correct |
33 ms |
94288 KB |
Output is correct |
6 |
Correct |
33 ms |
94288 KB |
Output is correct |
7 |
Correct |
30 ms |
94288 KB |
Output is correct |
8 |
Correct |
31 ms |
94288 KB |
Output is correct |
9 |
Correct |
31 ms |
94288 KB |
Output is correct |
10 |
Correct |
30 ms |
94384 KB |
Output is correct |
11 |
Correct |
29 ms |
94288 KB |
Output is correct |
12 |
Correct |
31 ms |
94288 KB |
Output is correct |
13 |
Correct |
28 ms |
94200 KB |
Output is correct |
14 |
Correct |
31 ms |
94400 KB |
Output is correct |
15 |
Correct |
30 ms |
94288 KB |
Output is correct |
16 |
Correct |
35 ms |
95444 KB |
Output is correct |
17 |
Correct |
106 ms |
105900 KB |
Output is correct |
18 |
Correct |
31 ms |
94288 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
94280 KB |
Output is correct |
2 |
Correct |
31 ms |
94308 KB |
Output is correct |
3 |
Correct |
31 ms |
94288 KB |
Output is correct |
4 |
Correct |
32 ms |
94396 KB |
Output is correct |
5 |
Correct |
29 ms |
94288 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
29 ms |
94208 KB |
Output is correct |
2 |
Correct |
33 ms |
94544 KB |
Output is correct |
3 |
Correct |
33 ms |
94800 KB |
Output is correct |
4 |
Correct |
33 ms |
94676 KB |
Output is correct |
5 |
Correct |
294 ms |
162656 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
94400 KB |
Output is correct |
2 |
Correct |
728 ms |
251376 KB |
Output is correct |
3 |
Correct |
333 ms |
180404 KB |
Output is correct |
4 |
Correct |
346 ms |
179824 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
94400 KB |
Output is correct |
2 |
Correct |
728 ms |
251376 KB |
Output is correct |
3 |
Correct |
333 ms |
180404 KB |
Output is correct |
4 |
Correct |
346 ms |
179824 KB |
Output is correct |
5 |
Correct |
27 ms |
94292 KB |
Output is correct |
6 |
Correct |
1685 ms |
338268 KB |
Output is correct |
7 |
Correct |
745 ms |
228908 KB |
Output is correct |
8 |
Correct |
1843 ms |
338164 KB |
Output is correct |
9 |
Correct |
1934 ms |
338316 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
35 ms |
94400 KB |
Output is correct |
2 |
Correct |
728 ms |
251376 KB |
Output is correct |
3 |
Correct |
333 ms |
180404 KB |
Output is correct |
4 |
Correct |
346 ms |
179824 KB |
Output is correct |
5 |
Correct |
33 ms |
94292 KB |
Output is correct |
6 |
Correct |
129 ms |
118232 KB |
Output is correct |
7 |
Correct |
163 ms |
121260 KB |
Output is correct |
8 |
Correct |
157 ms |
121292 KB |
Output is correct |
9 |
Correct |
145 ms |
121396 KB |
Output is correct |
10 |
Correct |
127 ms |
116416 KB |
Output is correct |
11 |
Correct |
194 ms |
122364 KB |
Output is correct |
12 |
Correct |
138 ms |
121280 KB |
Output is correct |
13 |
Correct |
188 ms |
121796 KB |
Output is correct |
14 |
Correct |
141 ms |
116144 KB |
Output is correct |
15 |
Correct |
227 ms |
118880 KB |
Output is correct |
16 |
Correct |
234 ms |
116212 KB |
Output is correct |
17 |
Correct |
431 ms |
132452 KB |
Output is correct |
18 |
Correct |
2362 ms |
359224 KB |
Output is correct |
19 |
Correct |
2884 ms |
343844 KB |
Output is correct |
20 |
Correct |
2896 ms |
342956 KB |
Output is correct |
21 |
Correct |
195 ms |
121612 KB |
Output is correct |
22 |
Correct |
186 ms |
121096 KB |
Output is correct |
23 |
Correct |
145 ms |
127268 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
33 ms |
94384 KB |
Output is correct |
2 |
Correct |
173 ms |
115892 KB |
Output is correct |
3 |
Correct |
225 ms |
116260 KB |
Output is correct |
4 |
Correct |
4440 ms |
363044 KB |
Output is correct |
5 |
Correct |
259 ms |
116920 KB |
Output is correct |
6 |
Correct |
259 ms |
116980 KB |
Output is correct |
7 |
Correct |
235 ms |
116920 KB |
Output is correct |
8 |
Correct |
288 ms |
118344 KB |
Output is correct |
9 |
Correct |
278 ms |
118620 KB |
Output is correct |
10 |
Correct |
183 ms |
116060 KB |
Output is correct |
11 |
Correct |
158 ms |
116136 KB |
Output is correct |
12 |
Correct |
197 ms |
115996 KB |
Output is correct |
13 |
Correct |
269 ms |
116664 KB |
Output is correct |
14 |
Correct |
1824 ms |
250012 KB |
Output is correct |
15 |
Correct |
135 ms |
108728 KB |
Output is correct |
16 |
Correct |
2183 ms |
312768 KB |
Output is correct |
17 |
Correct |
1062 ms |
238784 KB |
Output is correct |
18 |
Correct |
28 ms |
94292 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
31 ms |
94288 KB |
Output is correct |
2 |
Correct |
43 ms |
95432 KB |
Output is correct |
3 |
Correct |
39 ms |
95580 KB |
Output is correct |
4 |
Correct |
30 ms |
94288 KB |
Output is correct |
5 |
Correct |
33 ms |
94288 KB |
Output is correct |
6 |
Correct |
33 ms |
94288 KB |
Output is correct |
7 |
Correct |
30 ms |
94288 KB |
Output is correct |
8 |
Correct |
31 ms |
94288 KB |
Output is correct |
9 |
Correct |
31 ms |
94288 KB |
Output is correct |
10 |
Correct |
30 ms |
94384 KB |
Output is correct |
11 |
Correct |
29 ms |
94288 KB |
Output is correct |
12 |
Correct |
31 ms |
94288 KB |
Output is correct |
13 |
Correct |
28 ms |
94200 KB |
Output is correct |
14 |
Correct |
31 ms |
94400 KB |
Output is correct |
15 |
Correct |
30 ms |
94288 KB |
Output is correct |
16 |
Correct |
35 ms |
95444 KB |
Output is correct |
17 |
Correct |
106 ms |
105900 KB |
Output is correct |
18 |
Correct |
31 ms |
94288 KB |
Output is correct |
19 |
Correct |
31 ms |
94280 KB |
Output is correct |
20 |
Correct |
31 ms |
94308 KB |
Output is correct |
21 |
Correct |
31 ms |
94288 KB |
Output is correct |
22 |
Correct |
32 ms |
94396 KB |
Output is correct |
23 |
Correct |
29 ms |
94288 KB |
Output is correct |
24 |
Correct |
29 ms |
94208 KB |
Output is correct |
25 |
Correct |
33 ms |
94544 KB |
Output is correct |
26 |
Correct |
33 ms |
94800 KB |
Output is correct |
27 |
Correct |
33 ms |
94676 KB |
Output is correct |
28 |
Correct |
294 ms |
162656 KB |
Output is correct |
29 |
Correct |
35 ms |
94400 KB |
Output is correct |
30 |
Correct |
728 ms |
251376 KB |
Output is correct |
31 |
Correct |
333 ms |
180404 KB |
Output is correct |
32 |
Correct |
346 ms |
179824 KB |
Output is correct |
33 |
Correct |
27 ms |
94292 KB |
Output is correct |
34 |
Correct |
1685 ms |
338268 KB |
Output is correct |
35 |
Correct |
745 ms |
228908 KB |
Output is correct |
36 |
Correct |
1843 ms |
338164 KB |
Output is correct |
37 |
Correct |
1934 ms |
338316 KB |
Output is correct |
38 |
Correct |
33 ms |
94292 KB |
Output is correct |
39 |
Correct |
129 ms |
118232 KB |
Output is correct |
40 |
Correct |
163 ms |
121260 KB |
Output is correct |
41 |
Correct |
157 ms |
121292 KB |
Output is correct |
42 |
Correct |
145 ms |
121396 KB |
Output is correct |
43 |
Correct |
127 ms |
116416 KB |
Output is correct |
44 |
Correct |
194 ms |
122364 KB |
Output is correct |
45 |
Correct |
138 ms |
121280 KB |
Output is correct |
46 |
Correct |
188 ms |
121796 KB |
Output is correct |
47 |
Correct |
141 ms |
116144 KB |
Output is correct |
48 |
Correct |
227 ms |
118880 KB |
Output is correct |
49 |
Correct |
234 ms |
116212 KB |
Output is correct |
50 |
Correct |
431 ms |
132452 KB |
Output is correct |
51 |
Correct |
2362 ms |
359224 KB |
Output is correct |
52 |
Correct |
2884 ms |
343844 KB |
Output is correct |
53 |
Correct |
2896 ms |
342956 KB |
Output is correct |
54 |
Correct |
195 ms |
121612 KB |
Output is correct |
55 |
Correct |
186 ms |
121096 KB |
Output is correct |
56 |
Correct |
145 ms |
127268 KB |
Output is correct |
57 |
Correct |
33 ms |
94384 KB |
Output is correct |
58 |
Correct |
173 ms |
115892 KB |
Output is correct |
59 |
Correct |
225 ms |
116260 KB |
Output is correct |
60 |
Correct |
4440 ms |
363044 KB |
Output is correct |
61 |
Correct |
259 ms |
116920 KB |
Output is correct |
62 |
Correct |
259 ms |
116980 KB |
Output is correct |
63 |
Correct |
235 ms |
116920 KB |
Output is correct |
64 |
Correct |
288 ms |
118344 KB |
Output is correct |
65 |
Correct |
278 ms |
118620 KB |
Output is correct |
66 |
Correct |
183 ms |
116060 KB |
Output is correct |
67 |
Correct |
158 ms |
116136 KB |
Output is correct |
68 |
Correct |
197 ms |
115996 KB |
Output is correct |
69 |
Correct |
269 ms |
116664 KB |
Output is correct |
70 |
Correct |
1824 ms |
250012 KB |
Output is correct |
71 |
Correct |
135 ms |
108728 KB |
Output is correct |
72 |
Correct |
2183 ms |
312768 KB |
Output is correct |
73 |
Correct |
1062 ms |
238784 KB |
Output is correct |
74 |
Correct |
28 ms |
94292 KB |
Output is correct |
75 |
Correct |
25 ms |
94292 KB |
Output is correct |
76 |
Correct |
28 ms |
94292 KB |
Output is correct |
77 |
Correct |
30 ms |
94460 KB |
Output is correct |
78 |
Correct |
24 ms |
94320 KB |
Output is correct |
79 |
Correct |
34 ms |
96204 KB |
Output is correct |
80 |
Correct |
1920 ms |
214492 KB |
Output is correct |
81 |
Correct |
4689 ms |
362552 KB |
Output is correct |
82 |
Correct |
3865 ms |
363320 KB |
Output is correct |
83 |
Execution timed out |
5061 ms |
460972 KB |
Time limit exceeded |
84 |
Halted |
0 ms |
0 KB |
- |