//#pragma GCC optimize ("O3")
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define endl '\n'
const int DIM = 1E6+7;
pair<ll,ll> M[DIM];
const ll MAXN = 2E18;
ll mult(ll a,ll b){
ll mult = b;
b = 0;
while(a){
if (a&1)
b = min(b+mult,MAXN);
mult = min(MAXN,mult*2);
a/=2;
}
return b;
}
int solve(){
ll n,A,B;
cin>>n>>A>>B;
for(int i = 1;i<=n;++i){
cin>>M[i].first>>M[i].second;
}
ll gcd = __gcd(A,(B+1)%A);
ll d = A/gcd;
B = d*B;
vector<pair<ll,ll> > V;
ll cnt = 0,sum = 0;
for(int i = 1;i<=n;++i){
sum+=M[i].second-M[i].first+1;
ll start = M[i].first%B;
ll fin = B-1;
if (fin-start>M[i].second-M[i].first){
V.push_back({M[i].first%B,1});
V.push_back({M[i].second%B+1,-1});
continue;
}
V.push_back({start,1});
V.push_back({fin+1,-1});
M[i].first+=fin-start+1;
fin = M[i].second%B;
start = 0;
if (fin-start>M[i].second-M[i].first){
V.push_back({M[i].first%B,1});
V.push_back({M[i].second%B+1,-1});
continue;
}
V.push_back({start,1});
V.push_back({fin+1,-1});
M[i].second-=fin-start+1;
cnt+=(M[i].second-M[i].first)/B;
}
sort(V.begin(),V.end());
ll last = 0,res = 0;
for(int i = 0;i<int(V.size());++i){
res+=(V[i].first-last)*max(0ll,cnt-1);
last = V[i].first;
int cur = V[i].first;
cnt+=V[i].second;
while(i+1<V.size() && V[i+1].first==cur){
cnt+=V[++i].second;
}
}
res+=(B-last)*cnt;
cout<<sum-res<<endl;
return 1;
}
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
assert(solve());
return 0;
}
Compilation message
strange_device.cpp: In function 'int solve()':
strange_device.cpp:62:18: warning: comparison of integer expressions of different signedness: 'int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
62 | while(i+1<V.size() && V[i+1].first==cur){
| ~~~^~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
0 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Incorrect |
1 ms |
204 KB |
Output isn't correct |
2 |
Halted |
0 ms |
0 KB |
- |