#include<bits/stdc++.h>
using namespace std;
#define all(fl) fl.begin(),fl.end()
#define pb push_back
#define fi first
#define se second
#define for1(i,j,k) for(int i=j;i<=k;i++)
#define for2(i,j,k) for(int i=j;i>=k;i--)
#define for3(i,j,k,l) for(int i=j;i<=k;i+=l)
#define lb lower_bound
#define ub upper_bound
#define sz(a) (int)a.size()
#define gcd __gcd
#define lcm(x,y) x*y/__gcd(x,y)
using pii=pair<int,int>;
using pli=pair<long long,int>;
using pil=pair<int,long long>;
using pll=pair<long long,long long>;
const int maxn=1e6+9;
pll a[maxn];
vector<pll>b[maxn];
struct cus{
long long l,r;
bool operator < (const cus &p)const {
if (r==p.r)return l<p.l;
return r<p.r;
}
};
int bit[maxn*2];
signed main(){
srand(time(0));
ios_base::sync_with_stdio(0);
cin.tie(0);
//freopen("usaco.INP","r",stdin);
//freopen("usaco.OUT","w",stdout);
int n;
long long x,y;
cin>>n>>x>>y;
for1(i,1,n){
cin>>a[i].fi>>a[i].se;
}
long long d1=a[n].se/y;
long long t=x/gcd(x,y+1);
if (d1<t){
long long ans=0;
for1(i,1,n){
ans+=(a[i].se-a[i].fi+1);
}
cout<<ans;
return 0;
}
long long z=t*y;
for1(i,1,n){
long long d=(a[i].fi)/z;
a[i].fi-=z*d;
a[i].se-=z*d;
long long t1=a[i].fi/z,t2=a[i].se/z;
if (t1+1<t2){
b[i].pb({0,z-1});
continue;
}
if (t1==t2){
b[i].pb({a[i].fi%z,a[i].se%z});
continue;
}
pll l1={a[i].fi,z-1};
pll l2={0,a[i].se%z};
b[i].pb(l1);
b[i].pb(l2);
}
vector<long long>cord;
for1(i,1,n){
for (auto v:b[i]){
cord.pb(v.fi);
cord.pb(v.se+1);
}
}
sort(all(cord));
vector<long long>::iterator it=unique(all(cord));
cord.resize(distance(cord.begin(),it));
map<long long,int>newcord;
for1(i,0,sz(cord)-1){
newcord[cord[i]]=i;
}
for1(i,1,n){
for (auto &v:b[i]){
v.fi=newcord[v.fi];
bit[v.fi]++;
bit[newcord[v.se+1]]--;
}
}
for1(i,1,sz(cord)-1){
bit[i]+=bit[i-1];
}
long long ans=0;
for1(i,0,sz(cord)-2){
if (bit[i]){
ans+=cord[i+1]-cord[i];
}
}
for1(i,sz(cord)-1,sz(cord)-1){
if (bit[i]){
ans++;
}
}
cout<<ans;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23764 KB |
Output is correct |
2 |
Correct |
25 ms |
25224 KB |
Output is correct |
3 |
Correct |
21 ms |
25348 KB |
Output is correct |
4 |
Correct |
12 ms |
23764 KB |
Output is correct |
5 |
Correct |
12 ms |
23764 KB |
Output is correct |
6 |
Correct |
13 ms |
23820 KB |
Output is correct |
7 |
Correct |
13 ms |
23764 KB |
Output is correct |
8 |
Correct |
13 ms |
23764 KB |
Output is correct |
9 |
Correct |
15 ms |
23720 KB |
Output is correct |
10 |
Correct |
13 ms |
23712 KB |
Output is correct |
11 |
Correct |
14 ms |
23764 KB |
Output is correct |
12 |
Correct |
13 ms |
23820 KB |
Output is correct |
13 |
Correct |
12 ms |
23860 KB |
Output is correct |
14 |
Correct |
12 ms |
23744 KB |
Output is correct |
15 |
Correct |
12 ms |
23764 KB |
Output is correct |
16 |
Correct |
20 ms |
25424 KB |
Output is correct |
17 |
Correct |
119 ms |
40388 KB |
Output is correct |
18 |
Correct |
13 ms |
23740 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23792 KB |
Output is correct |
2 |
Correct |
12 ms |
23764 KB |
Output is correct |
3 |
Correct |
12 ms |
23780 KB |
Output is correct |
4 |
Correct |
13 ms |
23764 KB |
Output is correct |
5 |
Correct |
14 ms |
23820 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23816 KB |
Output is correct |
2 |
Correct |
14 ms |
23892 KB |
Output is correct |
3 |
Correct |
14 ms |
23892 KB |
Output is correct |
4 |
Correct |
14 ms |
23960 KB |
Output is correct |
5 |
Correct |
369 ms |
87448 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
1026 ms |
153004 KB |
Output is correct |
3 |
Correct |
1037 ms |
152996 KB |
Output is correct |
4 |
Correct |
1224 ms |
190056 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
1026 ms |
153004 KB |
Output is correct |
3 |
Correct |
1037 ms |
152996 KB |
Output is correct |
4 |
Correct |
1224 ms |
190056 KB |
Output is correct |
5 |
Correct |
12 ms |
23764 KB |
Output is correct |
6 |
Correct |
1119 ms |
190036 KB |
Output is correct |
7 |
Correct |
1149 ms |
190128 KB |
Output is correct |
8 |
Correct |
1127 ms |
190204 KB |
Output is correct |
9 |
Correct |
1189 ms |
189976 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
1026 ms |
153004 KB |
Output is correct |
3 |
Correct |
1037 ms |
152996 KB |
Output is correct |
4 |
Correct |
1224 ms |
190056 KB |
Output is correct |
5 |
Correct |
15 ms |
23764 KB |
Output is correct |
6 |
Correct |
101 ms |
40520 KB |
Output is correct |
7 |
Correct |
102 ms |
40440 KB |
Output is correct |
8 |
Correct |
96 ms |
40516 KB |
Output is correct |
9 |
Correct |
102 ms |
40356 KB |
Output is correct |
10 |
Correct |
95 ms |
40460 KB |
Output is correct |
11 |
Correct |
106 ms |
40440 KB |
Output is correct |
12 |
Correct |
106 ms |
40344 KB |
Output is correct |
13 |
Correct |
112 ms |
40400 KB |
Output is correct |
14 |
Correct |
96 ms |
40388 KB |
Output is correct |
15 |
Correct |
113 ms |
40456 KB |
Output is correct |
16 |
Correct |
112 ms |
40416 KB |
Output is correct |
17 |
Correct |
102 ms |
40552 KB |
Output is correct |
18 |
Correct |
976 ms |
190104 KB |
Output is correct |
19 |
Correct |
1037 ms |
190096 KB |
Output is correct |
20 |
Correct |
1137 ms |
190024 KB |
Output is correct |
21 |
Correct |
113 ms |
40376 KB |
Output is correct |
22 |
Correct |
98 ms |
40416 KB |
Output is correct |
23 |
Correct |
167 ms |
60804 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
12 ms |
23764 KB |
Output is correct |
2 |
Correct |
107 ms |
36908 KB |
Output is correct |
3 |
Correct |
111 ms |
38032 KB |
Output is correct |
4 |
Correct |
1224 ms |
153072 KB |
Output is correct |
5 |
Correct |
113 ms |
38124 KB |
Output is correct |
6 |
Correct |
125 ms |
38132 KB |
Output is correct |
7 |
Correct |
114 ms |
38240 KB |
Output is correct |
8 |
Correct |
111 ms |
38140 KB |
Output is correct |
9 |
Correct |
110 ms |
38276 KB |
Output is correct |
10 |
Correct |
111 ms |
38088 KB |
Output is correct |
11 |
Correct |
106 ms |
38216 KB |
Output is correct |
12 |
Correct |
104 ms |
38072 KB |
Output is correct |
13 |
Correct |
109 ms |
38104 KB |
Output is correct |
14 |
Correct |
1144 ms |
153092 KB |
Output is correct |
15 |
Correct |
119 ms |
37880 KB |
Output is correct |
16 |
Correct |
1043 ms |
190044 KB |
Output is correct |
17 |
Correct |
1099 ms |
190264 KB |
Output is correct |
18 |
Correct |
13 ms |
23764 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
13 ms |
23764 KB |
Output is correct |
2 |
Correct |
25 ms |
25224 KB |
Output is correct |
3 |
Correct |
21 ms |
25348 KB |
Output is correct |
4 |
Correct |
12 ms |
23764 KB |
Output is correct |
5 |
Correct |
12 ms |
23764 KB |
Output is correct |
6 |
Correct |
13 ms |
23820 KB |
Output is correct |
7 |
Correct |
13 ms |
23764 KB |
Output is correct |
8 |
Correct |
13 ms |
23764 KB |
Output is correct |
9 |
Correct |
15 ms |
23720 KB |
Output is correct |
10 |
Correct |
13 ms |
23712 KB |
Output is correct |
11 |
Correct |
14 ms |
23764 KB |
Output is correct |
12 |
Correct |
13 ms |
23820 KB |
Output is correct |
13 |
Correct |
12 ms |
23860 KB |
Output is correct |
14 |
Correct |
12 ms |
23744 KB |
Output is correct |
15 |
Correct |
12 ms |
23764 KB |
Output is correct |
16 |
Correct |
20 ms |
25424 KB |
Output is correct |
17 |
Correct |
119 ms |
40388 KB |
Output is correct |
18 |
Correct |
13 ms |
23740 KB |
Output is correct |
19 |
Correct |
13 ms |
23792 KB |
Output is correct |
20 |
Correct |
12 ms |
23764 KB |
Output is correct |
21 |
Correct |
12 ms |
23780 KB |
Output is correct |
22 |
Correct |
13 ms |
23764 KB |
Output is correct |
23 |
Correct |
14 ms |
23820 KB |
Output is correct |
24 |
Correct |
13 ms |
23816 KB |
Output is correct |
25 |
Correct |
14 ms |
23892 KB |
Output is correct |
26 |
Correct |
14 ms |
23892 KB |
Output is correct |
27 |
Correct |
14 ms |
23960 KB |
Output is correct |
28 |
Correct |
369 ms |
87448 KB |
Output is correct |
29 |
Correct |
12 ms |
23764 KB |
Output is correct |
30 |
Correct |
1026 ms |
153004 KB |
Output is correct |
31 |
Correct |
1037 ms |
152996 KB |
Output is correct |
32 |
Correct |
1224 ms |
190056 KB |
Output is correct |
33 |
Correct |
12 ms |
23764 KB |
Output is correct |
34 |
Correct |
1119 ms |
190036 KB |
Output is correct |
35 |
Correct |
1149 ms |
190128 KB |
Output is correct |
36 |
Correct |
1127 ms |
190204 KB |
Output is correct |
37 |
Correct |
1189 ms |
189976 KB |
Output is correct |
38 |
Correct |
15 ms |
23764 KB |
Output is correct |
39 |
Correct |
101 ms |
40520 KB |
Output is correct |
40 |
Correct |
102 ms |
40440 KB |
Output is correct |
41 |
Correct |
96 ms |
40516 KB |
Output is correct |
42 |
Correct |
102 ms |
40356 KB |
Output is correct |
43 |
Correct |
95 ms |
40460 KB |
Output is correct |
44 |
Correct |
106 ms |
40440 KB |
Output is correct |
45 |
Correct |
106 ms |
40344 KB |
Output is correct |
46 |
Correct |
112 ms |
40400 KB |
Output is correct |
47 |
Correct |
96 ms |
40388 KB |
Output is correct |
48 |
Correct |
113 ms |
40456 KB |
Output is correct |
49 |
Correct |
112 ms |
40416 KB |
Output is correct |
50 |
Correct |
102 ms |
40552 KB |
Output is correct |
51 |
Correct |
976 ms |
190104 KB |
Output is correct |
52 |
Correct |
1037 ms |
190096 KB |
Output is correct |
53 |
Correct |
1137 ms |
190024 KB |
Output is correct |
54 |
Correct |
113 ms |
40376 KB |
Output is correct |
55 |
Correct |
98 ms |
40416 KB |
Output is correct |
56 |
Correct |
167 ms |
60804 KB |
Output is correct |
57 |
Correct |
12 ms |
23764 KB |
Output is correct |
58 |
Correct |
107 ms |
36908 KB |
Output is correct |
59 |
Correct |
111 ms |
38032 KB |
Output is correct |
60 |
Correct |
1224 ms |
153072 KB |
Output is correct |
61 |
Correct |
113 ms |
38124 KB |
Output is correct |
62 |
Correct |
125 ms |
38132 KB |
Output is correct |
63 |
Correct |
114 ms |
38240 KB |
Output is correct |
64 |
Correct |
111 ms |
38140 KB |
Output is correct |
65 |
Correct |
110 ms |
38276 KB |
Output is correct |
66 |
Correct |
111 ms |
38088 KB |
Output is correct |
67 |
Correct |
106 ms |
38216 KB |
Output is correct |
68 |
Correct |
104 ms |
38072 KB |
Output is correct |
69 |
Correct |
109 ms |
38104 KB |
Output is correct |
70 |
Correct |
1144 ms |
153092 KB |
Output is correct |
71 |
Correct |
119 ms |
37880 KB |
Output is correct |
72 |
Correct |
1043 ms |
190044 KB |
Output is correct |
73 |
Correct |
1099 ms |
190264 KB |
Output is correct |
74 |
Correct |
13 ms |
23764 KB |
Output is correct |
75 |
Correct |
13 ms |
23792 KB |
Output is correct |
76 |
Correct |
14 ms |
23824 KB |
Output is correct |
77 |
Correct |
13 ms |
23764 KB |
Output is correct |
78 |
Correct |
13 ms |
23776 KB |
Output is correct |
79 |
Correct |
22 ms |
25424 KB |
Output is correct |
80 |
Correct |
1184 ms |
190080 KB |
Output is correct |
81 |
Correct |
1138 ms |
190140 KB |
Output is correct |
82 |
Correct |
1201 ms |
189996 KB |
Output is correct |
83 |
Correct |
1147 ms |
190160 KB |
Output is correct |
84 |
Correct |
1203 ms |
190028 KB |
Output is correct |
85 |
Correct |
1154 ms |
190060 KB |
Output is correct |
86 |
Correct |
166 ms |
60552 KB |
Output is correct |
87 |
Correct |
15 ms |
23760 KB |
Output is correct |