#include<bits/stdc++.h>
#define f first
#define s second
#define ll long long
#define pb push_back
#define pii pair<int,int>
#define pll pair<ll,ll>
#define sz(x) (int)x.size()
#define all(x) x.begin(),x.end()
#define vi vector<int>
#define vvi vector<vi>
#define vll vector<ll>
using namespace std;
vll val,vval;int sz;
vector<pll>t;
vector<ll>lz;
void build(int i,int l,int r){
if(l==r)return void(t[i]={0,vval[l]-vval[l-1]});
int m=(l+r)>>1;build(2*i,l,m);build(2*i+1,m+1,r);
t[i]={0,t[2*i].s+t[2*i+1].s};
}
void push(int i,int l,int r){
t[i].f+=lz[i];
if(l<r)lz[2*i]+=lz[i],lz[2*i+1]+=lz[i];
lz[i]=0;
}
void upd(int i,int l,int r,int tl,int tr,ll v){
push(i,l,r);
if(r<tl||l>tr)return;
if(r<=tr&&l>=tl){
lz[i]+=v;push(i,l,r);return;
}int m=(l+r)>>1;upd(2*i,l,m,tl,tr,v);upd(2*i+1,m+1,r,tl,tr,v);
if(t[2*i].f==t[2*i+1].f)t[i]={t[2*i].f,t[2*i].s+t[2*i+1].s};
else t[i]=min(t[2*i],t[2*i+1]);
}
int main(){
ios_base::sync_with_stdio(0);cin.tie(0);
ll n,a,b,d;cin>>n>>a>>b;d=a/__gcd(a,b+1);val.pb(0);val.pb(d);
ll ans=0;vector<pair<ll,pll>>v;
for(int i=0;i<n;i++){
ll l,r;cin>>l>>r;
ll x=l/b,y=r/b,z=l%b,tt=r%b;
if(z<=tt){
if(0<=z-1&&x+1<=y)v.pb({0,{x+1,y}}),v.pb({z,{-x-1,-y}}),val.pb((x+1)%d),val.pb((y)%d+1);
if(z<=tt&&x<=y)v.pb({z,{x,y}}),v.pb({tt+1,{-x,-y}}),val.pb(x%d),val.pb((y)%d+1);
if(tt+1<=b-1&&x<=y-1)v.pb({tt+1,{x,y-1}}),v.pb({b,{-x,-y+1}}),val.pb(x%d),val.pb((y-1+d)%d+1);
}
else {
if(0<=tt&&x+1<=y)v.pb({0,{x+1,y}}),v.pb({tt+1,{-x-1,-y}}),val.pb((x+1)%d),val.pb((y)%d+1);
if(tt+1<=z-1&&x+1<=y-1)v.pb({tt+1,{x+1,y-1}}),v.pb({z,{-x-1,-y+1}}),val.pb((x+1)%d),val.pb((y-1+d)%d+1);
if(z<=b-1&&x<=y-1)v.pb({z,{x,y-1}}),v.pb({b,{-x,-y+1}}),val.pb(x%d),val.pb((y-1+d)%d+1);
}
}sort(v.begin(),v.end());sort(val.begin(),val.end());val.erase(unique(val.begin(),val.end()),val.end());vval=val;vval.pb(d);
sz=(int)val.size();t.resize(4*((int)val.size()+1)),lz.resize(4*((int)val.size()+1),0);build(1,1,sz);
for(int i=0;i<v.size()-1;i++){
ll x=abs(v[i].s.f)/d,y=abs(v[i].s.s)/d,z=abs(v[i].s.f)%d,tt=abs(v[i].s.s)%d;
ll add=y-x+1;
if(v[i].s.f>=0&&v[i].s.s>=0){
if(z<=tt){
int tl,tr;
tl = upper_bound(val.begin(),val.end(),0)-val.begin();
tr = upper_bound(val.begin(),val.end(),z)-val.begin();
upd(1,1,sz,tl,tr-1,add-1);
tl = upper_bound(val.begin(),val.end(),z)-val.begin();
tr = upper_bound(val.begin(),val.end(),tt+1)-val.begin();
upd(1,1,sz,tl,tr-1,add);
tl = upper_bound(val.begin(),val.end(),tt+1)-val.begin();
tr = upper_bound(val.begin(),val.end(),d)-val.begin();
upd(1,1,sz,tl,tr-1,add-1);
}
else {
int tl,tr;
tl = upper_bound(val.begin(),val.end(),0)-val.begin();
tr = upper_bound(val.begin(),val.end(),tt+1)-val.begin();
upd(1,1,sz,tl,tr-1,add-1);
tl = upper_bound(val.begin(),val.end(),tt+1)-val.begin();
tr = upper_bound(val.begin(),val.end(),z)-val.begin();
upd(1,1,sz,tl,tr-1,add-2);
tl = upper_bound(val.begin(),val.end(),z)-val.begin();
tr = upper_bound(val.begin(),val.end(),d)-val.begin();
upd(1,1,sz,tl,tr-1,add-1);
}
}
else {
if(z<=tt){
int tl,tr;
tl = upper_bound(val.begin(),val.end(),0)-val.begin();
tr = upper_bound(val.begin(),val.end(),z)-val.begin();
upd(1,1,sz,tl,tr-1,-add+1);
tl = upper_bound(val.begin(),val.end(),z)-val.begin();
tr = upper_bound(val.begin(),val.end(),tt+1)-val.begin();
upd(1,1,sz,tl,tr-1,-add);
tl = upper_bound(val.begin(),val.end(),tt+1)-val.begin();
tr = upper_bound(val.begin(),val.end(),d)-val.begin();
upd(1,1,sz,tl,tr-1,-add+1);
}
else {
int tl,tr;
tl = upper_bound(val.begin(),val.end(),0)-val.begin();
tr = upper_bound(val.begin(),val.end(),tt+1)-val.begin();
upd(1,1,sz,tl,tr-1,-add+1);
tl = upper_bound(val.begin(),val.end(),tt+1)-val.begin();
tr = upper_bound(val.begin(),val.end(),z)-val.begin();
upd(1,1,sz,tl,tr-1,-add+2);
tl = upper_bound(val.begin(),val.end(),z)-val.begin();
tr = upper_bound(val.begin(),val.end(),d)-val.begin();
upd(1,1,sz,tl,tr-1,-add+1);
}
}
if(t[1].f==0)ans+=(v[i+1].f-v[i].f)*(d-t[1].s);
}cout<<ans;
}
Compilation message
strange_device.cpp: In function 'int main()':
strange_device.cpp:55:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, std::pair<long long int, long long int> > >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
55 | for(int i=0;i<v.size()-1;i++){
| ~^~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
14 ms |
1396 KB |
Output is correct |
3 |
Correct |
12 ms |
1396 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
21 ms |
1396 KB |
Output is correct |
17 |
Correct |
123 ms |
9524 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
344 KB |
Output is correct |
4 |
Correct |
1 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
7 ms |
824 KB |
Output is correct |
3 |
Correct |
2 ms |
604 KB |
Output is correct |
4 |
Correct |
4 ms |
604 KB |
Output is correct |
5 |
Correct |
515 ms |
65420 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
2901 ms |
174132 KB |
Output is correct |
3 |
Correct |
2880 ms |
174056 KB |
Output is correct |
4 |
Correct |
2918 ms |
175208 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
2901 ms |
174132 KB |
Output is correct |
3 |
Correct |
2880 ms |
174056 KB |
Output is correct |
4 |
Correct |
2918 ms |
175208 KB |
Output is correct |
5 |
Correct |
1 ms |
504 KB |
Output is correct |
6 |
Execution timed out |
5041 ms |
347512 KB |
Time limit exceeded |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
348 KB |
Output is correct |
2 |
Correct |
2901 ms |
174132 KB |
Output is correct |
3 |
Correct |
2880 ms |
174056 KB |
Output is correct |
4 |
Correct |
2918 ms |
175208 KB |
Output is correct |
5 |
Correct |
0 ms |
344 KB |
Output is correct |
6 |
Correct |
461 ms |
25016 KB |
Output is correct |
7 |
Correct |
790 ms |
44276 KB |
Output is correct |
8 |
Correct |
775 ms |
43676 KB |
Output is correct |
9 |
Correct |
755 ms |
42656 KB |
Output is correct |
10 |
Correct |
464 ms |
19792 KB |
Output is correct |
11 |
Correct |
808 ms |
45024 KB |
Output is correct |
12 |
Correct |
763 ms |
43932 KB |
Output is correct |
13 |
Correct |
565 ms |
35768 KB |
Output is correct |
14 |
Correct |
81 ms |
8964 KB |
Output is correct |
15 |
Correct |
817 ms |
33180 KB |
Output is correct |
16 |
Correct |
204 ms |
9888 KB |
Output is correct |
17 |
Correct |
881 ms |
43480 KB |
Output is correct |
18 |
Execution timed out |
5109 ms |
411260 KB |
Time limit exceeded |
19 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
160 ms |
9576 KB |
Output is correct |
3 |
Correct |
218 ms |
8152 KB |
Output is correct |
4 |
Correct |
4928 ms |
134772 KB |
Output is correct |
5 |
Correct |
269 ms |
12504 KB |
Output is correct |
6 |
Correct |
283 ms |
11832 KB |
Output is correct |
7 |
Correct |
244 ms |
11896 KB |
Output is correct |
8 |
Correct |
323 ms |
14280 KB |
Output is correct |
9 |
Correct |
323 ms |
14508 KB |
Output is correct |
10 |
Correct |
216 ms |
11600 KB |
Output is correct |
11 |
Correct |
101 ms |
12252 KB |
Output is correct |
12 |
Correct |
121 ms |
11464 KB |
Output is correct |
13 |
Correct |
294 ms |
13500 KB |
Output is correct |
14 |
Correct |
2063 ms |
101744 KB |
Output is correct |
15 |
Correct |
181 ms |
11976 KB |
Output is correct |
16 |
Correct |
1660 ms |
101828 KB |
Output is correct |
17 |
Correct |
4439 ms |
166272 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
344 KB |
Output is correct |
2 |
Correct |
14 ms |
1396 KB |
Output is correct |
3 |
Correct |
12 ms |
1396 KB |
Output is correct |
4 |
Correct |
0 ms |
344 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
1 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
0 ms |
348 KB |
Output is correct |
12 |
Correct |
1 ms |
348 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
1 ms |
348 KB |
Output is correct |
15 |
Correct |
1 ms |
348 KB |
Output is correct |
16 |
Correct |
21 ms |
1396 KB |
Output is correct |
17 |
Correct |
123 ms |
9524 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
0 ms |
344 KB |
Output is correct |
22 |
Correct |
1 ms |
348 KB |
Output is correct |
23 |
Correct |
0 ms |
348 KB |
Output is correct |
24 |
Correct |
1 ms |
348 KB |
Output is correct |
25 |
Correct |
7 ms |
824 KB |
Output is correct |
26 |
Correct |
2 ms |
604 KB |
Output is correct |
27 |
Correct |
4 ms |
604 KB |
Output is correct |
28 |
Correct |
515 ms |
65420 KB |
Output is correct |
29 |
Correct |
1 ms |
348 KB |
Output is correct |
30 |
Correct |
2901 ms |
174132 KB |
Output is correct |
31 |
Correct |
2880 ms |
174056 KB |
Output is correct |
32 |
Correct |
2918 ms |
175208 KB |
Output is correct |
33 |
Correct |
1 ms |
504 KB |
Output is correct |
34 |
Execution timed out |
5041 ms |
347512 KB |
Time limit exceeded |
35 |
Halted |
0 ms |
0 KB |
- |