#include<bits/stdc++.h>
using namespace std;
#define all(v) v.begin(), v.end()
#define pii pair<int, int>
#define F first
#define S second
typedef long long ll;
const int N=1e6+5;
ll A, B, l[N], r[N], ans;
map<ll, ll>m;
int n;
void add(ll x, ll y){
if(m.empty()){
m.insert({x, y});
return;
}
auto it=m.upper_bound(x);
if(it!=m.begin()){
--it;
if(it->S>=x){
if(it->S>=y)
return;
x=it->F;
m.erase(it);
}
}
it=m.upper_bound(x);
while(it!=m.end()){
if(it->S<=y)
m.erase(it);
else{
if(it->F<=y){
y=it->S;
m.erase(it);
}
break;
}
it=m.upper_bound(x);
}
m.insert({x, y});
}
int main(){
//ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
cin>>n>>A>>B;
for(int i=0;i<n;++i)
cin>>l[i]>>r[i];
ll x=__gcd(A, B+1);
x=A/x;
ll inf=1e18;
if(inf/x<B){
for(int i=0;i<n;++i)
ans+=r[i]-l[i]+1;
cout<<ans;
return 0;
}
B*=x;
for(int i=0;i<n;++i){
if(l[i]+B-1<=r[i]){
cout<<B;
return 0;
}
l[i]%=B;
r[i]%=B;
if(r[i]>=l[i])
add(l[i], r[i]);
else{
add(l[i], B-1);
add(0, r[i]);
}
}
auto it=m.begin();
while(it!=m.end()){
ans+=it->S-it->F+1;
++it;
}
cout<<ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
19 ms |
952 KB |
Output is correct |
3 |
Correct |
18 ms |
1064 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
296 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
296 KB |
Output is correct |
9 |
Correct |
1 ms |
304 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 |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
17 ms |
1416 KB |
Output is correct |
17 |
Correct |
191 ms |
11716 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 |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
2 ms |
332 KB |
Output is correct |
3 |
Correct |
3 ms |
332 KB |
Output is correct |
4 |
Correct |
3 ms |
316 KB |
Output is correct |
5 |
Correct |
1194 ms |
41080 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1993 ms |
78520 KB |
Output is correct |
3 |
Correct |
1949 ms |
78432 KB |
Output is correct |
4 |
Correct |
2274 ms |
78532 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1993 ms |
78520 KB |
Output is correct |
3 |
Correct |
1949 ms |
78432 KB |
Output is correct |
4 |
Correct |
2274 ms |
78532 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
1984 ms |
78756 KB |
Output is correct |
7 |
Correct |
2017 ms |
78536 KB |
Output is correct |
8 |
Correct |
1992 ms |
78480 KB |
Output is correct |
9 |
Correct |
2260 ms |
78612 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
1993 ms |
78520 KB |
Output is correct |
3 |
Correct |
1949 ms |
78432 KB |
Output is correct |
4 |
Correct |
2274 ms |
78532 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
181 ms |
7988 KB |
Output is correct |
7 |
Correct |
182 ms |
8088 KB |
Output is correct |
8 |
Correct |
177 ms |
8132 KB |
Output is correct |
9 |
Correct |
188 ms |
8000 KB |
Output is correct |
10 |
Correct |
179 ms |
8064 KB |
Output is correct |
11 |
Correct |
176 ms |
8020 KB |
Output is correct |
12 |
Correct |
184 ms |
8164 KB |
Output is correct |
13 |
Correct |
188 ms |
8064 KB |
Output is correct |
14 |
Correct |
181 ms |
8052 KB |
Output is correct |
15 |
Correct |
193 ms |
8240 KB |
Output is correct |
16 |
Correct |
195 ms |
11800 KB |
Output is correct |
17 |
Correct |
197 ms |
11760 KB |
Output is correct |
18 |
Correct |
1887 ms |
90316 KB |
Output is correct |
19 |
Correct |
2062 ms |
90324 KB |
Output is correct |
20 |
Correct |
2179 ms |
90120 KB |
Output is correct |
21 |
Correct |
187 ms |
11720 KB |
Output is correct |
22 |
Correct |
183 ms |
11776 KB |
Output is correct |
23 |
Correct |
485 ms |
18288 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
204 KB |
Output is correct |
2 |
Correct |
189 ms |
7980 KB |
Output is correct |
3 |
Correct |
191 ms |
8096 KB |
Output is correct |
4 |
Correct |
2144 ms |
78616 KB |
Output is correct |
5 |
Correct |
197 ms |
8112 KB |
Output is correct |
6 |
Correct |
194 ms |
8036 KB |
Output is correct |
7 |
Correct |
194 ms |
8004 KB |
Output is correct |
8 |
Correct |
189 ms |
8116 KB |
Output is correct |
9 |
Correct |
190 ms |
8156 KB |
Output is correct |
10 |
Correct |
181 ms |
8040 KB |
Output is correct |
11 |
Correct |
186 ms |
8044 KB |
Output is correct |
12 |
Correct |
197 ms |
8220 KB |
Output is correct |
13 |
Correct |
206 ms |
8032 KB |
Output is correct |
14 |
Correct |
2383 ms |
78484 KB |
Output is correct |
15 |
Correct |
178 ms |
11220 KB |
Output is correct |
16 |
Correct |
1911 ms |
93728 KB |
Output is correct |
17 |
Correct |
1896 ms |
93692 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 |
19 ms |
952 KB |
Output is correct |
3 |
Correct |
18 ms |
1064 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
296 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
1 ms |
204 KB |
Output is correct |
8 |
Correct |
1 ms |
296 KB |
Output is correct |
9 |
Correct |
1 ms |
304 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 |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
204 KB |
Output is correct |
16 |
Correct |
17 ms |
1416 KB |
Output is correct |
17 |
Correct |
191 ms |
11716 KB |
Output is correct |
18 |
Correct |
1 ms |
204 KB |
Output is correct |
19 |
Correct |
1 ms |
204 KB |
Output is correct |
20 |
Correct |
0 ms |
204 KB |
Output is correct |
21 |
Correct |
0 ms |
204 KB |
Output is correct |
22 |
Correct |
0 ms |
204 KB |
Output is correct |
23 |
Correct |
1 ms |
204 KB |
Output is correct |
24 |
Correct |
0 ms |
204 KB |
Output is correct |
25 |
Correct |
2 ms |
332 KB |
Output is correct |
26 |
Correct |
3 ms |
332 KB |
Output is correct |
27 |
Correct |
3 ms |
316 KB |
Output is correct |
28 |
Correct |
1194 ms |
41080 KB |
Output is correct |
29 |
Correct |
0 ms |
204 KB |
Output is correct |
30 |
Correct |
1993 ms |
78520 KB |
Output is correct |
31 |
Correct |
1949 ms |
78432 KB |
Output is correct |
32 |
Correct |
2274 ms |
78532 KB |
Output is correct |
33 |
Correct |
0 ms |
204 KB |
Output is correct |
34 |
Correct |
1984 ms |
78756 KB |
Output is correct |
35 |
Correct |
2017 ms |
78536 KB |
Output is correct |
36 |
Correct |
1992 ms |
78480 KB |
Output is correct |
37 |
Correct |
2260 ms |
78612 KB |
Output is correct |
38 |
Correct |
0 ms |
204 KB |
Output is correct |
39 |
Correct |
181 ms |
7988 KB |
Output is correct |
40 |
Correct |
182 ms |
8088 KB |
Output is correct |
41 |
Correct |
177 ms |
8132 KB |
Output is correct |
42 |
Correct |
188 ms |
8000 KB |
Output is correct |
43 |
Correct |
179 ms |
8064 KB |
Output is correct |
44 |
Correct |
176 ms |
8020 KB |
Output is correct |
45 |
Correct |
184 ms |
8164 KB |
Output is correct |
46 |
Correct |
188 ms |
8064 KB |
Output is correct |
47 |
Correct |
181 ms |
8052 KB |
Output is correct |
48 |
Correct |
193 ms |
8240 KB |
Output is correct |
49 |
Correct |
195 ms |
11800 KB |
Output is correct |
50 |
Correct |
197 ms |
11760 KB |
Output is correct |
51 |
Correct |
1887 ms |
90316 KB |
Output is correct |
52 |
Correct |
2062 ms |
90324 KB |
Output is correct |
53 |
Correct |
2179 ms |
90120 KB |
Output is correct |
54 |
Correct |
187 ms |
11720 KB |
Output is correct |
55 |
Correct |
183 ms |
11776 KB |
Output is correct |
56 |
Correct |
485 ms |
18288 KB |
Output is correct |
57 |
Correct |
0 ms |
204 KB |
Output is correct |
58 |
Correct |
189 ms |
7980 KB |
Output is correct |
59 |
Correct |
191 ms |
8096 KB |
Output is correct |
60 |
Correct |
2144 ms |
78616 KB |
Output is correct |
61 |
Correct |
197 ms |
8112 KB |
Output is correct |
62 |
Correct |
194 ms |
8036 KB |
Output is correct |
63 |
Correct |
194 ms |
8004 KB |
Output is correct |
64 |
Correct |
189 ms |
8116 KB |
Output is correct |
65 |
Correct |
190 ms |
8156 KB |
Output is correct |
66 |
Correct |
181 ms |
8040 KB |
Output is correct |
67 |
Correct |
186 ms |
8044 KB |
Output is correct |
68 |
Correct |
197 ms |
8220 KB |
Output is correct |
69 |
Correct |
206 ms |
8032 KB |
Output is correct |
70 |
Correct |
2383 ms |
78484 KB |
Output is correct |
71 |
Correct |
178 ms |
11220 KB |
Output is correct |
72 |
Correct |
1911 ms |
93728 KB |
Output is correct |
73 |
Correct |
1896 ms |
93692 KB |
Output is correct |
74 |
Correct |
1 ms |
204 KB |
Output is correct |
75 |
Correct |
0 ms |
204 KB |
Output is correct |
76 |
Correct |
0 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 |
18 ms |
1392 KB |
Output is correct |
80 |
Correct |
2238 ms |
90976 KB |
Output is correct |
81 |
Correct |
2156 ms |
93680 KB |
Output is correct |
82 |
Correct |
2460 ms |
93692 KB |
Output is correct |
83 |
Correct |
2229 ms |
93712 KB |
Output is correct |
84 |
Correct |
2423 ms |
93636 KB |
Output is correct |
85 |
Correct |
2148 ms |
93184 KB |
Output is correct |
86 |
Correct |
487 ms |
18116 KB |
Output is correct |
87 |
Correct |
1 ms |
204 KB |
Output is correct |