#include <bits/stdc++.h>
#define pb push_back
#define F first
#define S second
#define debug(x) cout << #x << "= " << x << ", "
#define ll long long
#define fast ios::sync_with_stdio(false), cin.tie(0), cout.tie(0)
#define SZ(x) (int) x.size()
#define int long long
using namespace std;
mt19937 rng(chrono::steady_clock::now().time_since_epoch().count());
const int maxn = 1e6 + 10 , inf = 1e18;
int n , A , B , l[maxn] , r[maxn];
set <pair <int , int> > st;
int gcd(int a , int b)
{
if(b == 0)
return a;
return gcd(b , a % b);
}
void add (int L , int R)
{
if(st.empty())
{
st.insert(make_pair(L , R));
return;
}
auto it = st.upper_bound({L , L});
if(it != st.begin())
it--;
auto v = *it;
if(v.S < L)
it++;
if(it == st.end())
{
st.insert({L , R});
return;
}
auto u = *it;
L = min(u.F , L);
vector <pair <int, int>> vec;
while(u.F <= R)
{
vec.pb(u);
it++;
if(it == st.end())
break;
u = *it;
}
if(!vec.empty())
{
R = max(R , vec.back().S);
}
for(auto u : vec)
st.erase(u);
st.insert({L , R});
}
int32_t main()
{
fast;
cin >> n >> A >> B;
int S = 0;
for(int i = 0 ; i < n ; i++)
{
cin >> l[i] >> r[i];
S += r[i] - l[i] + 1;
}
int g = gcd(A , B + 1);
A /= g;
if(inf / A < B)
{
cout << S << endl;
return 0;
}
A *= B;
for(int i = 0 ; i < n ; i++)
{
int tmp = l[i] / A;
tmp = tmp * A;
l[i] -= tmp;
r[i] -= tmp;
add(l[i] , min(r[i] , A - 1));
if(r[i] >= A)
{
r[i] -= A;
add(0 , min(A - 1 , r[i]));
}
}
int ans = 0;
for(auto u : st)
{
ans += u.S - u.F + 1;
}
cout << ans << endl;
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
6 ms |
1392 KB |
Output is correct |
3 |
Correct |
7 ms |
1420 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
6 ms |
1364 KB |
Output is correct |
17 |
Correct |
63 ms |
11872 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
340 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
1 ms |
340 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
1 ms |
388 KB |
Output is correct |
5 |
Correct |
289 ms |
16692 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
769 ms |
81680 KB |
Output is correct |
3 |
Correct |
704 ms |
93524 KB |
Output is correct |
4 |
Correct |
837 ms |
101744 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
769 ms |
81680 KB |
Output is correct |
3 |
Correct |
704 ms |
93524 KB |
Output is correct |
4 |
Correct |
837 ms |
101744 KB |
Output is correct |
5 |
Correct |
1 ms |
340 KB |
Output is correct |
6 |
Correct |
675 ms |
101684 KB |
Output is correct |
7 |
Correct |
736 ms |
101964 KB |
Output is correct |
8 |
Correct |
719 ms |
101800 KB |
Output is correct |
9 |
Correct |
854 ms |
115728 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
769 ms |
81680 KB |
Output is correct |
3 |
Correct |
704 ms |
93524 KB |
Output is correct |
4 |
Correct |
837 ms |
101744 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Correct |
57 ms |
11748 KB |
Output is correct |
7 |
Correct |
61 ms |
11808 KB |
Output is correct |
8 |
Correct |
62 ms |
11788 KB |
Output is correct |
9 |
Correct |
68 ms |
11860 KB |
Output is correct |
10 |
Correct |
67 ms |
11772 KB |
Output is correct |
11 |
Correct |
58 ms |
11824 KB |
Output is correct |
12 |
Correct |
67 ms |
11840 KB |
Output is correct |
13 |
Correct |
69 ms |
11764 KB |
Output is correct |
14 |
Correct |
70 ms |
11780 KB |
Output is correct |
15 |
Correct |
76 ms |
11768 KB |
Output is correct |
16 |
Correct |
88 ms |
11864 KB |
Output is correct |
17 |
Correct |
68 ms |
11764 KB |
Output is correct |
18 |
Correct |
658 ms |
115580 KB |
Output is correct |
19 |
Correct |
735 ms |
102380 KB |
Output is correct |
20 |
Correct |
821 ms |
115688 KB |
Output is correct |
21 |
Correct |
59 ms |
11820 KB |
Output is correct |
22 |
Correct |
67 ms |
11744 KB |
Output is correct |
23 |
Correct |
127 ms |
18304 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
340 KB |
Output is correct |
2 |
Correct |
76 ms |
11712 KB |
Output is correct |
3 |
Correct |
69 ms |
11828 KB |
Output is correct |
4 |
Correct |
767 ms |
115624 KB |
Output is correct |
5 |
Correct |
72 ms |
11848 KB |
Output is correct |
6 |
Correct |
75 ms |
11788 KB |
Output is correct |
7 |
Correct |
90 ms |
11836 KB |
Output is correct |
8 |
Correct |
73 ms |
11852 KB |
Output is correct |
9 |
Correct |
70 ms |
11864 KB |
Output is correct |
10 |
Correct |
63 ms |
11932 KB |
Output is correct |
11 |
Correct |
66 ms |
11832 KB |
Output is correct |
12 |
Correct |
80 ms |
11772 KB |
Output is correct |
13 |
Correct |
74 ms |
11752 KB |
Output is correct |
14 |
Correct |
778 ms |
115616 KB |
Output is correct |
15 |
Correct |
56 ms |
11192 KB |
Output is correct |
16 |
Correct |
698 ms |
115656 KB |
Output is correct |
17 |
Correct |
689 ms |
115596 KB |
Output is correct |
18 |
Correct |
0 ms |
340 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Correct |
6 ms |
1392 KB |
Output is correct |
3 |
Correct |
7 ms |
1420 KB |
Output is correct |
4 |
Correct |
1 ms |
340 KB |
Output is correct |
5 |
Correct |
0 ms |
340 KB |
Output is correct |
6 |
Correct |
0 ms |
340 KB |
Output is correct |
7 |
Correct |
1 ms |
340 KB |
Output is correct |
8 |
Correct |
0 ms |
340 KB |
Output is correct |
9 |
Correct |
1 ms |
340 KB |
Output is correct |
10 |
Correct |
1 ms |
340 KB |
Output is correct |
11 |
Correct |
0 ms |
340 KB |
Output is correct |
12 |
Correct |
0 ms |
340 KB |
Output is correct |
13 |
Correct |
1 ms |
344 KB |
Output is correct |
14 |
Correct |
1 ms |
340 KB |
Output is correct |
15 |
Correct |
1 ms |
340 KB |
Output is correct |
16 |
Correct |
6 ms |
1364 KB |
Output is correct |
17 |
Correct |
63 ms |
11872 KB |
Output is correct |
18 |
Correct |
1 ms |
336 KB |
Output is correct |
19 |
Correct |
1 ms |
340 KB |
Output is correct |
20 |
Correct |
1 ms |
340 KB |
Output is correct |
21 |
Correct |
1 ms |
340 KB |
Output is correct |
22 |
Correct |
1 ms |
340 KB |
Output is correct |
23 |
Correct |
1 ms |
340 KB |
Output is correct |
24 |
Correct |
1 ms |
340 KB |
Output is correct |
25 |
Correct |
1 ms |
340 KB |
Output is correct |
26 |
Correct |
1 ms |
348 KB |
Output is correct |
27 |
Correct |
1 ms |
388 KB |
Output is correct |
28 |
Correct |
289 ms |
16692 KB |
Output is correct |
29 |
Correct |
1 ms |
332 KB |
Output is correct |
30 |
Correct |
769 ms |
81680 KB |
Output is correct |
31 |
Correct |
704 ms |
93524 KB |
Output is correct |
32 |
Correct |
837 ms |
101744 KB |
Output is correct |
33 |
Correct |
1 ms |
340 KB |
Output is correct |
34 |
Correct |
675 ms |
101684 KB |
Output is correct |
35 |
Correct |
736 ms |
101964 KB |
Output is correct |
36 |
Correct |
719 ms |
101800 KB |
Output is correct |
37 |
Correct |
854 ms |
115728 KB |
Output is correct |
38 |
Correct |
1 ms |
336 KB |
Output is correct |
39 |
Correct |
57 ms |
11748 KB |
Output is correct |
40 |
Correct |
61 ms |
11808 KB |
Output is correct |
41 |
Correct |
62 ms |
11788 KB |
Output is correct |
42 |
Correct |
68 ms |
11860 KB |
Output is correct |
43 |
Correct |
67 ms |
11772 KB |
Output is correct |
44 |
Correct |
58 ms |
11824 KB |
Output is correct |
45 |
Correct |
67 ms |
11840 KB |
Output is correct |
46 |
Correct |
69 ms |
11764 KB |
Output is correct |
47 |
Correct |
70 ms |
11780 KB |
Output is correct |
48 |
Correct |
76 ms |
11768 KB |
Output is correct |
49 |
Correct |
88 ms |
11864 KB |
Output is correct |
50 |
Correct |
68 ms |
11764 KB |
Output is correct |
51 |
Correct |
658 ms |
115580 KB |
Output is correct |
52 |
Correct |
735 ms |
102380 KB |
Output is correct |
53 |
Correct |
821 ms |
115688 KB |
Output is correct |
54 |
Correct |
59 ms |
11820 KB |
Output is correct |
55 |
Correct |
67 ms |
11744 KB |
Output is correct |
56 |
Correct |
127 ms |
18304 KB |
Output is correct |
57 |
Correct |
1 ms |
340 KB |
Output is correct |
58 |
Correct |
76 ms |
11712 KB |
Output is correct |
59 |
Correct |
69 ms |
11828 KB |
Output is correct |
60 |
Correct |
767 ms |
115624 KB |
Output is correct |
61 |
Correct |
72 ms |
11848 KB |
Output is correct |
62 |
Correct |
75 ms |
11788 KB |
Output is correct |
63 |
Correct |
90 ms |
11836 KB |
Output is correct |
64 |
Correct |
73 ms |
11852 KB |
Output is correct |
65 |
Correct |
70 ms |
11864 KB |
Output is correct |
66 |
Correct |
63 ms |
11932 KB |
Output is correct |
67 |
Correct |
66 ms |
11832 KB |
Output is correct |
68 |
Correct |
80 ms |
11772 KB |
Output is correct |
69 |
Correct |
74 ms |
11752 KB |
Output is correct |
70 |
Correct |
778 ms |
115616 KB |
Output is correct |
71 |
Correct |
56 ms |
11192 KB |
Output is correct |
72 |
Correct |
698 ms |
115656 KB |
Output is correct |
73 |
Correct |
689 ms |
115596 KB |
Output is correct |
74 |
Correct |
0 ms |
340 KB |
Output is correct |
75 |
Correct |
1 ms |
340 KB |
Output is correct |
76 |
Correct |
0 ms |
340 KB |
Output is correct |
77 |
Correct |
1 ms |
340 KB |
Output is correct |
78 |
Correct |
0 ms |
332 KB |
Output is correct |
79 |
Correct |
6 ms |
1380 KB |
Output is correct |
80 |
Correct |
764 ms |
115676 KB |
Output is correct |
81 |
Correct |
769 ms |
115640 KB |
Output is correct |
82 |
Correct |
841 ms |
115796 KB |
Output is correct |
83 |
Correct |
843 ms |
115476 KB |
Output is correct |
84 |
Correct |
864 ms |
115676 KB |
Output is correct |
85 |
Correct |
760 ms |
108460 KB |
Output is correct |
86 |
Correct |
142 ms |
18084 KB |
Output is correct |
87 |
Correct |
0 ms |
340 KB |
Output is correct |