#include <bits/stdc++.h>
#pragma GCC target ("avx,avx2,fma")
#pragma GCC optimize ("O2")
#pragma GCC optimize ("unroll-loops")
#define X first
#define Y second
#define debug(x) cerr << #x << ": " << x << endl;
#define endl '\n'
#define pb push_back
#define rep(i,l,r) for (int i=l; i<r; i++)
#define repr(i,r,l) for (int i=r; i>=l; i--)
using namespace std;
typedef long long ll;
typedef pair<ll,ll> pll;
const long long int N=5e2+20,mod = 1e9+7,inf=1e18;
inline int mkay(int a,int b){
if (a+b >= mod) return a+b-mod;
return a+b;
}
ll gcd(ll a,ll b){
if (!b) return a;
if (b < a) swap(a,b);
return gcd(a,b%a);
}
int main(){
ios :: sync_with_stdio(0); cin.tie(0);
int n;
ll a,b,z;
cin >> n >> a >> b;
a /= gcd(a,b+1);
debug(gcd(4,6));
if (b >= (inf+a-1)/a) z = inf+1;
else z = a*b;
set<pll> st;
rep(i,0,n){
ll l,r;
cin >> l >> r;
if (r-l+1 >= z){
cout << z;
return 0;
}
if (r%z < l%z){
auto it = st.upper_bound({l%z,0});
while(it != st.end()){
st.erase(it);
it = st.upper_bound({l%z,0});
}
st.insert({l%z,z});
st.insert({0,r%z+1});
}
else{
st.insert({l%z,r%z+1});
}
}
pll p = {1,-1};
ll ans = 0;
for(pll q : st){
if (q.X >= p.X && q.Y <= p.Y)
continue;
if (q.X <= p.Y){
ans += q.Y-p.Y;
p.Y = q.Y;
continue;
}
p = q;
ans += p.Y-p.X;
}
cout << ans;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
204 KB |
Output is correct |
2 |
Correct |
143 ms |
1228 KB |
Output is correct |
3 |
Correct |
10 ms |
1220 KB |
Output is correct |
4 |
Correct |
3 ms |
312 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
3 ms |
312 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
312 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
3 ms |
204 KB |
Output is correct |
14 |
Correct |
3 ms |
204 KB |
Output is correct |
15 |
Correct |
2 ms |
332 KB |
Output is correct |
16 |
Correct |
10 ms |
1200 KB |
Output is correct |
17 |
Correct |
83 ms |
10280 KB |
Output is correct |
18 |
Correct |
2 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
204 KB |
Output is correct |
2 |
Correct |
3 ms |
204 KB |
Output is correct |
3 |
Correct |
1 ms |
316 KB |
Output is correct |
4 |
Correct |
1 ms |
204 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
2 ms |
316 KB |
Output is correct |
2 |
Correct |
13 ms |
324 KB |
Output is correct |
3 |
Correct |
3 ms |
332 KB |
Output is correct |
4 |
Correct |
2 ms |
332 KB |
Output is correct |
5 |
Correct |
309 ms |
25508 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
732 ms |
100112 KB |
Output is correct |
3 |
Correct |
737 ms |
100000 KB |
Output is correct |
4 |
Correct |
774 ms |
100120 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
732 ms |
100112 KB |
Output is correct |
3 |
Correct |
737 ms |
100000 KB |
Output is correct |
4 |
Correct |
774 ms |
100120 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
726 ms |
100028 KB |
Output is correct |
7 |
Correct |
687 ms |
100112 KB |
Output is correct |
8 |
Correct |
678 ms |
100096 KB |
Output is correct |
9 |
Correct |
739 ms |
100072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
732 ms |
100112 KB |
Output is correct |
3 |
Correct |
737 ms |
100000 KB |
Output is correct |
4 |
Correct |
774 ms |
100120 KB |
Output is correct |
5 |
Correct |
2 ms |
204 KB |
Output is correct |
6 |
Correct |
59 ms |
10228 KB |
Output is correct |
7 |
Correct |
63 ms |
10160 KB |
Output is correct |
8 |
Correct |
84 ms |
10308 KB |
Output is correct |
9 |
Correct |
67 ms |
10224 KB |
Output is correct |
10 |
Correct |
57 ms |
10192 KB |
Output is correct |
11 |
Correct |
63 ms |
10156 KB |
Output is correct |
12 |
Correct |
78 ms |
10276 KB |
Output is correct |
13 |
Correct |
74 ms |
10432 KB |
Output is correct |
14 |
Correct |
86 ms |
10224 KB |
Output is correct |
15 |
Correct |
68 ms |
10192 KB |
Output is correct |
16 |
Correct |
65 ms |
10180 KB |
Output is correct |
17 |
Correct |
63 ms |
10312 KB |
Output is correct |
18 |
Correct |
691 ms |
100120 KB |
Output is correct |
19 |
Correct |
702 ms |
100068 KB |
Output is correct |
20 |
Correct |
1157 ms |
100036 KB |
Output is correct |
21 |
Correct |
67 ms |
10180 KB |
Output is correct |
22 |
Correct |
61 ms |
10292 KB |
Output is correct |
23 |
Correct |
123 ms |
12844 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
204 KB |
Output is correct |
2 |
Correct |
72 ms |
10204 KB |
Output is correct |
3 |
Correct |
66 ms |
10276 KB |
Output is correct |
4 |
Correct |
761 ms |
100080 KB |
Output is correct |
5 |
Correct |
64 ms |
10180 KB |
Output is correct |
6 |
Correct |
71 ms |
10360 KB |
Output is correct |
7 |
Correct |
79 ms |
10220 KB |
Output is correct |
8 |
Correct |
74 ms |
10160 KB |
Output is correct |
9 |
Correct |
72 ms |
10160 KB |
Output is correct |
10 |
Correct |
68 ms |
10252 KB |
Output is correct |
11 |
Correct |
61 ms |
10204 KB |
Output is correct |
12 |
Correct |
68 ms |
10320 KB |
Output is correct |
13 |
Correct |
70 ms |
10180 KB |
Output is correct |
14 |
Correct |
731 ms |
100084 KB |
Output is correct |
15 |
Correct |
76 ms |
10308 KB |
Output is correct |
16 |
Correct |
703 ms |
100228 KB |
Output is correct |
17 |
Correct |
693 ms |
100108 KB |
Output is correct |
18 |
Correct |
2 ms |
204 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
204 KB |
Output is correct |
2 |
Correct |
143 ms |
1228 KB |
Output is correct |
3 |
Correct |
10 ms |
1220 KB |
Output is correct |
4 |
Correct |
3 ms |
312 KB |
Output is correct |
5 |
Correct |
1 ms |
204 KB |
Output is correct |
6 |
Correct |
3 ms |
312 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
204 KB |
Output is correct |
9 |
Correct |
1 ms |
332 KB |
Output is correct |
10 |
Correct |
1 ms |
312 KB |
Output is correct |
11 |
Correct |
1 ms |
204 KB |
Output is correct |
12 |
Correct |
1 ms |
204 KB |
Output is correct |
13 |
Correct |
3 ms |
204 KB |
Output is correct |
14 |
Correct |
3 ms |
204 KB |
Output is correct |
15 |
Correct |
2 ms |
332 KB |
Output is correct |
16 |
Correct |
10 ms |
1200 KB |
Output is correct |
17 |
Correct |
83 ms |
10280 KB |
Output is correct |
18 |
Correct |
2 ms |
204 KB |
Output is correct |
19 |
Correct |
2 ms |
204 KB |
Output is correct |
20 |
Correct |
3 ms |
204 KB |
Output is correct |
21 |
Correct |
1 ms |
316 KB |
Output is correct |
22 |
Correct |
1 ms |
204 KB |
Output is correct |
23 |
Correct |
1 ms |
204 KB |
Output is correct |
24 |
Correct |
2 ms |
316 KB |
Output is correct |
25 |
Correct |
13 ms |
324 KB |
Output is correct |
26 |
Correct |
3 ms |
332 KB |
Output is correct |
27 |
Correct |
2 ms |
332 KB |
Output is correct |
28 |
Correct |
309 ms |
25508 KB |
Output is correct |
29 |
Correct |
1 ms |
204 KB |
Output is correct |
30 |
Correct |
732 ms |
100112 KB |
Output is correct |
31 |
Correct |
737 ms |
100000 KB |
Output is correct |
32 |
Correct |
774 ms |
100120 KB |
Output is correct |
33 |
Correct |
1 ms |
204 KB |
Output is correct |
34 |
Correct |
726 ms |
100028 KB |
Output is correct |
35 |
Correct |
687 ms |
100112 KB |
Output is correct |
36 |
Correct |
678 ms |
100096 KB |
Output is correct |
37 |
Correct |
739 ms |
100072 KB |
Output is correct |
38 |
Correct |
2 ms |
204 KB |
Output is correct |
39 |
Correct |
59 ms |
10228 KB |
Output is correct |
40 |
Correct |
63 ms |
10160 KB |
Output is correct |
41 |
Correct |
84 ms |
10308 KB |
Output is correct |
42 |
Correct |
67 ms |
10224 KB |
Output is correct |
43 |
Correct |
57 ms |
10192 KB |
Output is correct |
44 |
Correct |
63 ms |
10156 KB |
Output is correct |
45 |
Correct |
78 ms |
10276 KB |
Output is correct |
46 |
Correct |
74 ms |
10432 KB |
Output is correct |
47 |
Correct |
86 ms |
10224 KB |
Output is correct |
48 |
Correct |
68 ms |
10192 KB |
Output is correct |
49 |
Correct |
65 ms |
10180 KB |
Output is correct |
50 |
Correct |
63 ms |
10312 KB |
Output is correct |
51 |
Correct |
691 ms |
100120 KB |
Output is correct |
52 |
Correct |
702 ms |
100068 KB |
Output is correct |
53 |
Correct |
1157 ms |
100036 KB |
Output is correct |
54 |
Correct |
67 ms |
10180 KB |
Output is correct |
55 |
Correct |
61 ms |
10292 KB |
Output is correct |
56 |
Correct |
123 ms |
12844 KB |
Output is correct |
57 |
Correct |
1 ms |
204 KB |
Output is correct |
58 |
Correct |
72 ms |
10204 KB |
Output is correct |
59 |
Correct |
66 ms |
10276 KB |
Output is correct |
60 |
Correct |
761 ms |
100080 KB |
Output is correct |
61 |
Correct |
64 ms |
10180 KB |
Output is correct |
62 |
Correct |
71 ms |
10360 KB |
Output is correct |
63 |
Correct |
79 ms |
10220 KB |
Output is correct |
64 |
Correct |
74 ms |
10160 KB |
Output is correct |
65 |
Correct |
72 ms |
10160 KB |
Output is correct |
66 |
Correct |
68 ms |
10252 KB |
Output is correct |
67 |
Correct |
61 ms |
10204 KB |
Output is correct |
68 |
Correct |
68 ms |
10320 KB |
Output is correct |
69 |
Correct |
70 ms |
10180 KB |
Output is correct |
70 |
Correct |
731 ms |
100084 KB |
Output is correct |
71 |
Correct |
76 ms |
10308 KB |
Output is correct |
72 |
Correct |
703 ms |
100228 KB |
Output is correct |
73 |
Correct |
693 ms |
100108 KB |
Output is correct |
74 |
Correct |
2 ms |
204 KB |
Output is correct |
75 |
Correct |
2 ms |
308 KB |
Output is correct |
76 |
Correct |
1 ms |
204 KB |
Output is correct |
77 |
Correct |
2 ms |
204 KB |
Output is correct |
78 |
Correct |
1 ms |
204 KB |
Output is correct |
79 |
Correct |
7 ms |
1228 KB |
Output is correct |
80 |
Correct |
706 ms |
99960 KB |
Output is correct |
81 |
Correct |
756 ms |
99960 KB |
Output is correct |
82 |
Correct |
752 ms |
99984 KB |
Output is correct |
83 |
Correct |
727 ms |
99660 KB |
Output is correct |
84 |
Correct |
880 ms |
99300 KB |
Output is correct |
85 |
Correct |
805 ms |
99308 KB |
Output is correct |
86 |
Correct |
147 ms |
12772 KB |
Output is correct |
87 |
Correct |
2 ms |
204 KB |
Output is correct |