#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define rep(a) rep1(i,a)
#define rep1(i,a) rep2(i,0,a)
#define rep2(i,b,a) for(int i=(b); i<((int)(a)); i++)
#define rep3(i,b,a) for(int i=(b); i>=((int)(a)); i--)
#define all(a) a.begin(),a.end()
#define pii pair<int,int>
#define pll pair<ll,ll>
#define pb push_back
#define mp make_pair
#define sz(a) ((int)a.size())
#define pow2(x) (1ll<<(x))
#define ceiling(a,b) (((a)+(b)-1)/(b))
#define eps 1e-9
#define print0(a) cout << (a) << ' '
#define print1(a) cout << (a) << '\n'
#define print2(a,b) cout << (a) << ' ',print1(b)
#define print3(a,b,c) cout << (a) << ' ',print2(b,c)
#define print4(a,b,c,d) cout << (a) << ' ',print3(b,c,d)
using namespace std;
const int maxn=1000005;
int n;
ll x,y,inf=1e18,mod=-1,minl=4e18,maxr=-1,ans=0;
struct segment{ll l,r;}seg[maxn],seg1;
bool comp(segment i, segment j){return i.l<j.l;}
vector<segment> vec;
int main(){
ios_base::sync_with_stdio(0),cin.tie(0);
cin >> n >> x >> y;
if(x/__gcd(y+1,x)<=inf/y) mod=x/__gcd(y+1,x)*y;
rep(n) cin >> seg[i].l >> seg[i].r;
rep(n){
if(mod>=0){
if(seg[i].r/mod==seg[i].l/mod){
seg[i].l%=mod;
seg[i].r%=mod;
vec.pb(seg[i]);
}
else{
seg[i].l%=mod;
seg[i].r%=mod;
if(seg[i].r>=seg[i].l){
print1(mod);
return 0;
}
seg1.l=seg[i].l,seg1.r=mod-1;
vec.pb(seg1);
seg1.l=0,seg1.r=seg[i].r;
vec.pb(seg1);
}
}
else{
vec.pb(seg[i]);
}
}
sort(all(vec),comp);
for(auto seg2: vec){
if(maxr>=0){
if(seg2.l>maxr){
ans+=maxr-minl+1;
minl=seg2.l,maxr=seg2.r;
}
}
minl=min(minl,seg2.l),maxr=max(maxr,seg2.r);
}
ans+=maxr-minl+1;
print1(ans);
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
11 ms |
1280 KB |
Output is correct |
3 |
Correct |
11 ms |
1280 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
4 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
4 ms |
384 KB |
Output is correct |
13 |
Correct |
4 ms |
384 KB |
Output is correct |
14 |
Correct |
4 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
10 ms |
1280 KB |
Output is correct |
17 |
Correct |
70 ms |
7880 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
512 KB |
Output is correct |
4 |
Correct |
5 ms |
512 KB |
Output is correct |
5 |
Correct |
394 ms |
33072 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
588 ms |
33244 KB |
Output is correct |
3 |
Correct |
578 ms |
33372 KB |
Output is correct |
4 |
Correct |
570 ms |
33380 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
588 ms |
33244 KB |
Output is correct |
3 |
Correct |
578 ms |
33372 KB |
Output is correct |
4 |
Correct |
570 ms |
33380 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
596 ms |
33500 KB |
Output is correct |
7 |
Correct |
584 ms |
33320 KB |
Output is correct |
8 |
Correct |
575 ms |
33384 KB |
Output is correct |
9 |
Correct |
664 ms |
33404 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
588 ms |
33244 KB |
Output is correct |
3 |
Correct |
578 ms |
33372 KB |
Output is correct |
4 |
Correct |
570 ms |
33380 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
65 ms |
7792 KB |
Output is correct |
7 |
Correct |
68 ms |
7792 KB |
Output is correct |
8 |
Correct |
62 ms |
7792 KB |
Output is correct |
9 |
Correct |
61 ms |
7792 KB |
Output is correct |
10 |
Correct |
60 ms |
7792 KB |
Output is correct |
11 |
Correct |
66 ms |
7920 KB |
Output is correct |
12 |
Correct |
62 ms |
7796 KB |
Output is correct |
13 |
Correct |
68 ms |
7792 KB |
Output is correct |
14 |
Correct |
62 ms |
7792 KB |
Output is correct |
15 |
Correct |
68 ms |
7792 KB |
Output is correct |
16 |
Correct |
71 ms |
7792 KB |
Output is correct |
17 |
Correct |
63 ms |
7768 KB |
Output is correct |
18 |
Correct |
579 ms |
33584 KB |
Output is correct |
19 |
Correct |
579 ms |
33372 KB |
Output is correct |
20 |
Correct |
694 ms |
33360 KB |
Output is correct |
21 |
Correct |
70 ms |
7792 KB |
Output is correct |
22 |
Correct |
59 ms |
7792 KB |
Output is correct |
23 |
Correct |
184 ms |
26472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
4 ms |
384 KB |
Output is correct |
2 |
Correct |
68 ms |
7792 KB |
Output is correct |
3 |
Correct |
68 ms |
7796 KB |
Output is correct |
4 |
Correct |
696 ms |
33380 KB |
Output is correct |
5 |
Correct |
67 ms |
7792 KB |
Output is correct |
6 |
Correct |
65 ms |
7792 KB |
Output is correct |
7 |
Correct |
68 ms |
7792 KB |
Output is correct |
8 |
Correct |
75 ms |
7792 KB |
Output is correct |
9 |
Correct |
64 ms |
7796 KB |
Output is correct |
10 |
Correct |
66 ms |
7792 KB |
Output is correct |
11 |
Correct |
66 ms |
7792 KB |
Output is correct |
12 |
Correct |
59 ms |
7792 KB |
Output is correct |
13 |
Correct |
69 ms |
8048 KB |
Output is correct |
14 |
Correct |
652 ms |
33376 KB |
Output is correct |
15 |
Correct |
70 ms |
7920 KB |
Output is correct |
16 |
Correct |
565 ms |
33384 KB |
Output is correct |
17 |
Correct |
579 ms |
33672 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
11 ms |
1280 KB |
Output is correct |
3 |
Correct |
11 ms |
1280 KB |
Output is correct |
4 |
Correct |
4 ms |
384 KB |
Output is correct |
5 |
Correct |
5 ms |
384 KB |
Output is correct |
6 |
Correct |
4 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
384 KB |
Output is correct |
10 |
Correct |
4 ms |
384 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
4 ms |
384 KB |
Output is correct |
13 |
Correct |
4 ms |
384 KB |
Output is correct |
14 |
Correct |
4 ms |
384 KB |
Output is correct |
15 |
Correct |
5 ms |
384 KB |
Output is correct |
16 |
Correct |
10 ms |
1280 KB |
Output is correct |
17 |
Correct |
70 ms |
7880 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
384 KB |
Output is correct |
20 |
Correct |
4 ms |
384 KB |
Output is correct |
21 |
Correct |
5 ms |
384 KB |
Output is correct |
22 |
Correct |
4 ms |
384 KB |
Output is correct |
23 |
Correct |
4 ms |
384 KB |
Output is correct |
24 |
Correct |
5 ms |
384 KB |
Output is correct |
25 |
Correct |
5 ms |
384 KB |
Output is correct |
26 |
Correct |
5 ms |
512 KB |
Output is correct |
27 |
Correct |
5 ms |
512 KB |
Output is correct |
28 |
Correct |
394 ms |
33072 KB |
Output is correct |
29 |
Correct |
5 ms |
384 KB |
Output is correct |
30 |
Correct |
588 ms |
33244 KB |
Output is correct |
31 |
Correct |
578 ms |
33372 KB |
Output is correct |
32 |
Correct |
570 ms |
33380 KB |
Output is correct |
33 |
Correct |
4 ms |
384 KB |
Output is correct |
34 |
Correct |
596 ms |
33500 KB |
Output is correct |
35 |
Correct |
584 ms |
33320 KB |
Output is correct |
36 |
Correct |
575 ms |
33384 KB |
Output is correct |
37 |
Correct |
664 ms |
33404 KB |
Output is correct |
38 |
Correct |
5 ms |
384 KB |
Output is correct |
39 |
Correct |
65 ms |
7792 KB |
Output is correct |
40 |
Correct |
68 ms |
7792 KB |
Output is correct |
41 |
Correct |
62 ms |
7792 KB |
Output is correct |
42 |
Correct |
61 ms |
7792 KB |
Output is correct |
43 |
Correct |
60 ms |
7792 KB |
Output is correct |
44 |
Correct |
66 ms |
7920 KB |
Output is correct |
45 |
Correct |
62 ms |
7796 KB |
Output is correct |
46 |
Correct |
68 ms |
7792 KB |
Output is correct |
47 |
Correct |
62 ms |
7792 KB |
Output is correct |
48 |
Correct |
68 ms |
7792 KB |
Output is correct |
49 |
Correct |
71 ms |
7792 KB |
Output is correct |
50 |
Correct |
63 ms |
7768 KB |
Output is correct |
51 |
Correct |
579 ms |
33584 KB |
Output is correct |
52 |
Correct |
579 ms |
33372 KB |
Output is correct |
53 |
Correct |
694 ms |
33360 KB |
Output is correct |
54 |
Correct |
70 ms |
7792 KB |
Output is correct |
55 |
Correct |
59 ms |
7792 KB |
Output is correct |
56 |
Correct |
184 ms |
26472 KB |
Output is correct |
57 |
Correct |
4 ms |
384 KB |
Output is correct |
58 |
Correct |
68 ms |
7792 KB |
Output is correct |
59 |
Correct |
68 ms |
7796 KB |
Output is correct |
60 |
Correct |
696 ms |
33380 KB |
Output is correct |
61 |
Correct |
67 ms |
7792 KB |
Output is correct |
62 |
Correct |
65 ms |
7792 KB |
Output is correct |
63 |
Correct |
68 ms |
7792 KB |
Output is correct |
64 |
Correct |
75 ms |
7792 KB |
Output is correct |
65 |
Correct |
64 ms |
7796 KB |
Output is correct |
66 |
Correct |
66 ms |
7792 KB |
Output is correct |
67 |
Correct |
66 ms |
7792 KB |
Output is correct |
68 |
Correct |
59 ms |
7792 KB |
Output is correct |
69 |
Correct |
69 ms |
8048 KB |
Output is correct |
70 |
Correct |
652 ms |
33376 KB |
Output is correct |
71 |
Correct |
70 ms |
7920 KB |
Output is correct |
72 |
Correct |
565 ms |
33384 KB |
Output is correct |
73 |
Correct |
579 ms |
33672 KB |
Output is correct |
74 |
Correct |
5 ms |
384 KB |
Output is correct |
75 |
Correct |
4 ms |
384 KB |
Output is correct |
76 |
Correct |
5 ms |
384 KB |
Output is correct |
77 |
Correct |
5 ms |
384 KB |
Output is correct |
78 |
Correct |
5 ms |
384 KB |
Output is correct |
79 |
Correct |
10 ms |
1280 KB |
Output is correct |
80 |
Correct |
707 ms |
33404 KB |
Output is correct |
81 |
Correct |
685 ms |
33444 KB |
Output is correct |
82 |
Correct |
669 ms |
33508 KB |
Output is correct |
83 |
Correct |
659 ms |
33508 KB |
Output is correct |
84 |
Correct |
668 ms |
33500 KB |
Output is correct |
85 |
Correct |
666 ms |
33384 KB |
Output is correct |
86 |
Correct |
185 ms |
26216 KB |
Output is correct |
87 |
Correct |
5 ms |
384 KB |
Output is correct |