#include <algorithm>
#include <bits/stdc++.h>
#define ll long long
#define fi first
#define se second
using namespace std;
int v2(ll n)
{
return log2(n) + 1;
}
int n, i, i1, i2, j, status = 0, idx;
pair<ll, ll> in[10000001];
pair<ll, ll> out[10000001];
ll a, b, tmp, l, r, cnt;
int main()
{
#define task "TASK"
if(fopen(task".inp", "r")) {
freopen(task".inp", "r", stdin);
//freopen(task".out","w",stdout);
}
cin.tie(0)->sync_with_stdio(0);
cin >> n >> a >> b;
tmp = a / (__gcd(a, b + 1));
i = 0;
while(i < n)
{
cin >> in[i].fi >> in[i].se;
i++;
}
if(v2(tmp) + v2(b) > 60)
{
i = 0;
tmp = 0;
while(i < n)
{
tmp = tmp + (in[i].se - in[i].fi + 1);
i++;
}
cout << tmp;
}
else
{
tmp = tmp * b;
//cout << tmp<<endl;
i = 0;
j = 0;
while(i < n)
{
if(in[i].fi / tmp == in[i].se / tmp)
{
out[i + j].fi = in[i].fi % tmp;
out[i + j].se = in[i].se % tmp;
}
else if((in[i].fi / tmp) + 1 == in[i].se / tmp)
{
out[i + j].fi = in[i].fi % tmp;
out[i + j].se = tmp - 1;
j++;
out[i + j].fi = 0;
out[i + j].se = in[i].se % tmp;
}
else
{
status = 1;
}
i++;
}
if(status == 1)
{
cout << tmp;
}
else
{
idx = i + j;
//cout << idx<<endl;
//i=0;
//while (i<idx)
//{
// cout << out[i].fi <<" "<< out[i].se << endl;
// i++;
//}
sort(out + 0, out + idx);
l = out[0].fi;
r = out[0].se;
cnt = r - l + 1;
i = 1;
while(i < idx)
{
if(out[i].fi > out[i - 1].se)
{
l = out[i].fi;
r = out[i].se;
cnt = cnt + r - l + 1;
}
else
{
if(out[i].se <= out[i - 1].se)
{
}
else
{
r = out[i].se;
cnt = cnt + r - out[i - 1].se;
}
}
i++;
}
cout << cnt;
}
}
}
Compilation message
strange_device.cpp: In function 'int main()':
strange_device.cpp:19:12: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
19 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
5 ms |
980 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Correct |
1 ms |
212 KB |
Output is correct |
3 |
Correct |
1 ms |
212 KB |
Output is correct |
4 |
Correct |
1 ms |
212 KB |
Output is correct |
5 |
Correct |
1 ms |
212 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
212 KB |
Output is correct |
2 |
Incorrect |
1 ms |
340 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
397 ms |
31876 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
397 ms |
31876 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
397 ms |
31876 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
44 ms |
6512 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
0 ms |
212 KB |
Output is correct |
2 |
Incorrect |
5 ms |
980 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |