//#pragma GCC optimize("Ofast,unroll-loops,O3")
//#pragma GCC optimize("avx,avx2,sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,fma,tune=native")
#include<bits/stdc++.h>
//#include<bits/extc++.h>
//#pragma pack(1)
#define fast ios::sync_with_stdio(0); cin.tie(0);
#define int long long
#define pii pair<int,int>
#define x first
#define y second
using namespace std;
//using namespace __gnu_pbds;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
//typedef tree<int, null_type,less_equal<int>, rb_tree_tag,tree_order_statistics_node_update> order_multiset;
//typedef tree<int, null_type,less<int>, rb_tree_tag,tree_order_statistics_node_update> order_set;
int n,a,b,t;
int findt(){
int go=b+1,g=__gcd(go,a);
// cout<<go<<" "<<a<<' '<<b<<" "<<g<<'\n';
// lcm(go,a)/go*b
if ((double)(log10(a/g)+log10(b))>=18.01) return -1;
else return a/g*b;
}
set<pii>st;
void add(int l,int r){
// cout<<"add "<<l<<" "<<r<<'\n';
if (st.empty()){
st.insert(pii(l,r-l+1));
return;
}
auto it=st.insert(pii(l,r-l+1)).x;
pii now={l,r-l+1};
while (it!=st.begin()){
auto it1=prev(it);
pii last=*it1;
if (last.x+last.y>=now.x+now.y){
st.erase(it);
return;
}
if (last.x+last.y>=now.x){
st.erase(it1);
it=st.erase(it);
now.y=(now.x+now.y-1)-last.x+1;
now.x=last.x;
it=st.insert(now).x;
}
else break;
}
while (it!=st.end()){
auto it1=next(it);
if (it1==st.end()) break;
pii nxt=*it1;
if (now.x+now.y>=nxt.x+nxt.y){
st.erase(nxt);
continue;
}
if (now.x+now.y>=nxt.x){
st.erase(it1);
it=st.erase(it);
now.y=(nxt.x+nxt.y-1)-now.x+1;
it=st.insert(now).x;
}
else break;
}
}
signed main(){
fast
cin>>n>>a>>b;
t=findt();
if (n==1){
int l,r; cin>>l>>r;
if (t<0) cout<<r-l+1<<'\n';
else if (r-l+1>=t) cout<<t<<'\n';
else cout<<r-l+1<<'\n';
}
else if (t<0){
int ans=0;
for (int i=1;i<=n;i++){
int l,r; cin>>l>>r;
ans+=(r-l+1);
}
cout<<ans<<'\n';
}
else {
for (int i=1;i<=n;i++){
int l,r; cin>>l>>r;
l%=t; r%=t;
if (l>r){
add(l,t-1);
add(0,r);
}
else add(l,r);
}
int ans=0;
for (auto i:st){
ans+=i.y;
// cout<<i.x<<" ~ "<<i.x+i.y-1<<'\n';
}
cout<<ans<<'\n';
}
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
8 ms |
340 KB |
Output is correct |
3 |
Correct |
8 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
7 ms |
340 KB |
Output is correct |
17 |
Correct |
48 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
0 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
268 ms |
304 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
471 ms |
340 KB |
Output is correct |
3 |
Correct |
452 ms |
320 KB |
Output is correct |
4 |
Correct |
467 ms |
312 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
471 ms |
340 KB |
Output is correct |
3 |
Correct |
452 ms |
320 KB |
Output is correct |
4 |
Correct |
467 ms |
312 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
458 ms |
320 KB |
Output is correct |
7 |
Correct |
460 ms |
340 KB |
Output is correct |
8 |
Correct |
517 ms |
320 KB |
Output is correct |
9 |
Correct |
505 ms |
328 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
471 ms |
340 KB |
Output is correct |
3 |
Correct |
452 ms |
320 KB |
Output is correct |
4 |
Correct |
467 ms |
312 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
48 ms |
340 KB |
Output is correct |
7 |
Correct |
47 ms |
340 KB |
Output is correct |
8 |
Correct |
52 ms |
332 KB |
Output is correct |
9 |
Correct |
48 ms |
308 KB |
Output is correct |
10 |
Correct |
43 ms |
212 KB |
Output is correct |
11 |
Correct |
49 ms |
340 KB |
Output is correct |
12 |
Correct |
51 ms |
316 KB |
Output is correct |
13 |
Correct |
54 ms |
316 KB |
Output is correct |
14 |
Correct |
49 ms |
340 KB |
Output is correct |
15 |
Correct |
48 ms |
320 KB |
Output is correct |
16 |
Correct |
67 ms |
320 KB |
Output is correct |
17 |
Correct |
50 ms |
316 KB |
Output is correct |
18 |
Correct |
501 ms |
340 KB |
Output is correct |
19 |
Correct |
501 ms |
340 KB |
Output is correct |
20 |
Correct |
687 ms |
316 KB |
Output is correct |
21 |
Correct |
54 ms |
340 KB |
Output is correct |
22 |
Correct |
57 ms |
212 KB |
Output is correct |
23 |
Correct |
159 ms |
256 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
49 ms |
320 KB |
Output is correct |
3 |
Correct |
51 ms |
340 KB |
Output is correct |
4 |
Correct |
530 ms |
320 KB |
Output is correct |
5 |
Correct |
54 ms |
456 KB |
Output is correct |
6 |
Correct |
53 ms |
560 KB |
Output is correct |
7 |
Correct |
52 ms |
572 KB |
Output is correct |
8 |
Correct |
60 ms |
540 KB |
Output is correct |
9 |
Correct |
56 ms |
572 KB |
Output is correct |
10 |
Correct |
50 ms |
520 KB |
Output is correct |
11 |
Correct |
51 ms |
596 KB |
Output is correct |
12 |
Correct |
53 ms |
568 KB |
Output is correct |
13 |
Correct |
75 ms |
468 KB |
Output is correct |
14 |
Correct |
512 ms |
396 KB |
Output is correct |
15 |
Correct |
61 ms |
568 KB |
Output is correct |
16 |
Correct |
542 ms |
596 KB |
Output is correct |
17 |
Correct |
534 ms |
596 KB |
Output is correct |
18 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
8 ms |
340 KB |
Output is correct |
3 |
Correct |
8 ms |
340 KB |
Output is correct |
4 |
Correct |
0 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
6 |
Correct |
0 ms |
212 KB |
Output is correct |
7 |
Correct |
1 ms |
212 KB |
Output is correct |
8 |
Correct |
1 ms |
212 KB |
Output is correct |
9 |
Correct |
1 ms |
212 KB |
Output is correct |
10 |
Correct |
1 ms |
212 KB |
Output is correct |
11 |
Correct |
1 ms |
212 KB |
Output is correct |
12 |
Correct |
0 ms |
212 KB |
Output is correct |
13 |
Correct |
1 ms |
212 KB |
Output is correct |
14 |
Correct |
0 ms |
212 KB |
Output is correct |
15 |
Correct |
1 ms |
212 KB |
Output is correct |
16 |
Correct |
7 ms |
340 KB |
Output is correct |
17 |
Correct |
48 ms |
340 KB |
Output is correct |
18 |
Correct |
0 ms |
212 KB |
Output is correct |
19 |
Correct |
1 ms |
212 KB |
Output is correct |
20 |
Correct |
0 ms |
212 KB |
Output is correct |
21 |
Correct |
1 ms |
212 KB |
Output is correct |
22 |
Correct |
0 ms |
212 KB |
Output is correct |
23 |
Correct |
1 ms |
212 KB |
Output is correct |
24 |
Correct |
0 ms |
212 KB |
Output is correct |
25 |
Correct |
1 ms |
212 KB |
Output is correct |
26 |
Correct |
1 ms |
340 KB |
Output is correct |
27 |
Correct |
1 ms |
340 KB |
Output is correct |
28 |
Correct |
268 ms |
304 KB |
Output is correct |
29 |
Correct |
1 ms |
212 KB |
Output is correct |
30 |
Correct |
471 ms |
340 KB |
Output is correct |
31 |
Correct |
452 ms |
320 KB |
Output is correct |
32 |
Correct |
467 ms |
312 KB |
Output is correct |
33 |
Correct |
1 ms |
212 KB |
Output is correct |
34 |
Correct |
458 ms |
320 KB |
Output is correct |
35 |
Correct |
460 ms |
340 KB |
Output is correct |
36 |
Correct |
517 ms |
320 KB |
Output is correct |
37 |
Correct |
505 ms |
328 KB |
Output is correct |
38 |
Correct |
1 ms |
212 KB |
Output is correct |
39 |
Correct |
48 ms |
340 KB |
Output is correct |
40 |
Correct |
47 ms |
340 KB |
Output is correct |
41 |
Correct |
52 ms |
332 KB |
Output is correct |
42 |
Correct |
48 ms |
308 KB |
Output is correct |
43 |
Correct |
43 ms |
212 KB |
Output is correct |
44 |
Correct |
49 ms |
340 KB |
Output is correct |
45 |
Correct |
51 ms |
316 KB |
Output is correct |
46 |
Correct |
54 ms |
316 KB |
Output is correct |
47 |
Correct |
49 ms |
340 KB |
Output is correct |
48 |
Correct |
48 ms |
320 KB |
Output is correct |
49 |
Correct |
67 ms |
320 KB |
Output is correct |
50 |
Correct |
50 ms |
316 KB |
Output is correct |
51 |
Correct |
501 ms |
340 KB |
Output is correct |
52 |
Correct |
501 ms |
340 KB |
Output is correct |
53 |
Correct |
687 ms |
316 KB |
Output is correct |
54 |
Correct |
54 ms |
340 KB |
Output is correct |
55 |
Correct |
57 ms |
212 KB |
Output is correct |
56 |
Correct |
159 ms |
256 KB |
Output is correct |
57 |
Correct |
1 ms |
212 KB |
Output is correct |
58 |
Correct |
49 ms |
320 KB |
Output is correct |
59 |
Correct |
51 ms |
340 KB |
Output is correct |
60 |
Correct |
530 ms |
320 KB |
Output is correct |
61 |
Correct |
54 ms |
456 KB |
Output is correct |
62 |
Correct |
53 ms |
560 KB |
Output is correct |
63 |
Correct |
52 ms |
572 KB |
Output is correct |
64 |
Correct |
60 ms |
540 KB |
Output is correct |
65 |
Correct |
56 ms |
572 KB |
Output is correct |
66 |
Correct |
50 ms |
520 KB |
Output is correct |
67 |
Correct |
51 ms |
596 KB |
Output is correct |
68 |
Correct |
53 ms |
568 KB |
Output is correct |
69 |
Correct |
75 ms |
468 KB |
Output is correct |
70 |
Correct |
512 ms |
396 KB |
Output is correct |
71 |
Correct |
61 ms |
568 KB |
Output is correct |
72 |
Correct |
542 ms |
596 KB |
Output is correct |
73 |
Correct |
534 ms |
596 KB |
Output is correct |
74 |
Correct |
1 ms |
212 KB |
Output is correct |
75 |
Correct |
1 ms |
212 KB |
Output is correct |
76 |
Correct |
1 ms |
212 KB |
Output is correct |
77 |
Correct |
1 ms |
212 KB |
Output is correct |
78 |
Correct |
1 ms |
212 KB |
Output is correct |
79 |
Correct |
6 ms |
472 KB |
Output is correct |
80 |
Correct |
496 ms |
592 KB |
Output is correct |
81 |
Correct |
477 ms |
596 KB |
Output is correct |
82 |
Correct |
528 ms |
584 KB |
Output is correct |
83 |
Correct |
511 ms |
584 KB |
Output is correct |
84 |
Correct |
485 ms |
588 KB |
Output is correct |
85 |
Correct |
535 ms |
604 KB |
Output is correct |
86 |
Correct |
125 ms |
820 KB |
Output is correct |
87 |
Correct |
1 ms |
320 KB |
Output is correct |