#include <bits/stdc++.h>
using namespace std;
#define TASK "long"
#define fi first
#define se second
#define ll long long
#define pb push_back
#define ALL(x) (x).begin(), (x).end()
#define GETBIT(mask, i) ((mask) >> (i) & 1)
#define MASK(i) ((1LL) << (i))
#define SZ(x) ((int)(x).size())
#define mp make_pair
#define CNTBIT(mask) __builtin_popcount(mask)
template<class X, class Y> bool maximize(X &x, Y y){ if (x < y) {x = y; return true;} return false;};
template<class X, class Y> bool minimize(X &x, Y y){ if (x > y) {x = y; return true;} return false;};
typedef pair<int, int> ii;
const int N = 1e6 + 5;
const int INF = 1e9 + 7;
const int mod = 1e9 + 7;
long long A, B;
int n;
pair<long long, long long> a[N];
void read() {
cin >> n >> A >> B;
for(int i = 1; i <= n; i++) cin >> a[i].fi >> a[i].se;
}
pair<long long, long long> cal(long long t) {
// return mp(1, 1);
return mp((t + (t / B)) % A, t % B);
}
void sub1() {
map<pair<long long, long long>, bool> m;
for(int i = 1; i <= n; i++) {
for(long long j = a[i].fi; j <= a[i].se; j++) {
m[cal(j)] = 1;
}
}
cout << SZ(m);
}
bool check1() {
long long sum = 0;
for(int i = 1; i <= n; i++) sum += (a[i].se - a[i].fi + 1);
// cerr << sum << endl;
return (sum <= (int)1e6);
}
vector<pair<long long, long long>> seg;
void add(pair<long long, long long> tmp) {
long long r = (2LL * tmp.fi) % A;
cerr << tmp.fi << ' ' << tmp.se << ' ' << r << endl;
if ((r & 1) == ((A - 1) & 1)) {
long long rig = (A - 1);
long long len = tmp.se - tmp.fi + 1;
if (len > A / 2 + (A & 1)) {
// cerr << "TRUE\n";
if (r & 1) cerr << 1 << ' ' << rig << endl;
else cerr << 0 << ' ' << rig << endl;
if (r & 1) seg.push_back({1, rig});
else seg.push_back({0, rig});
}
else {
long long cnt = (rig - r) / 2 + 1;
if (cnt >= len) {
seg.push_back({r, r + 2 * (len - 1)});
cerr << r << ' ' << r + 2 * (len - 1) << endl;
}
else {
seg.push_back({r, rig});
len -= cnt;
cerr << r << ' ' << rig << endl;
if (A & 1) cerr << 0 << ' ' << 2 * (len - 1) << endl;
else cout << 1 << ' ' << 1 + 2 * (len - 1) << endl;
if (A & 1) seg.push_back(mp(0, 2 * (len - 1)));
else seg.push_back({1LL, 1 + 2LL * (len - 1)});
}
}
}
else {
// cerr << "FALSE: " << tmp.fi << ' ' << tmp.se << ' ' << r << endl;
// assert(false);
}
}
void sub4() {
for(int i = 1; i <= n; i++) {
for(int iter = 0; iter < 3; iter++) {
long long len = a[i].se - a[i].fi + 1;
if (len <= 0) break;
long long r = 2LL * a[i].fi % A;
long long rig = (A - 1) - (((A - 1) & 1) != (r & 1));
long long cnt = (rig - r) / 2 + 1;
if (cnt >= len) {
seg.push_back({r, r + 2 * (len - 1)});
break;
}
else {
seg.push_back({r, rig});
a[i].fi += cnt;
}
}
}
sort(ALL(seg));
vector<pair<long long, long long>> odd, even;
for(auto &i: seg) {
if (i.fi & 1) odd.push_back(i);
else even.push_back(i);
// cout << i.fi << ' ' << i.se << endl;
}
long long ans = 0;
for(int i = 0; i < SZ(odd); i++) {
int j = i;
while(j + 1 < SZ(odd) && odd[j + 1].fi <= odd[j].se) ++j;
ans += (odd[j].se - odd[i].fi) / 2 + 1;
i = j;
}
for(int i = 0; i < SZ(even); i++) {
int j = i;
while(j + 1 < SZ(even) && even[j + 1].fi <= even[j].se) ++j;
ans += (even[j].se - even[i].fi) / 2 + 1;
i = j;
}
cout << ans;
}
void solve() {
sub4(); return;
if (check1()) {
sub1();
return;
}
if (B == 1) sub4();
}
signed main() {
ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
if (fopen(TASK".inp", "r")) {
freopen(TASK".inp", "r", stdin);
freopen(TASK".out", "w", stdout);
}
int test = 1;
// cin >> test;
while(test--) {
read();
solve();
}
return 0;
}
Compilation message
strange_device.cpp: In function 'int main()':
strange_device.cpp:133:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
133 | freopen(TASK".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
strange_device.cpp:134:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
134 | freopen(TASK".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Incorrect |
5 ms |
1528 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Incorrect |
1 ms |
336 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
328 ms |
66784 KB |
Output is correct |
3 |
Correct |
360 ms |
96164 KB |
Output is correct |
4 |
Correct |
307 ms |
96340 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
328 ms |
66784 KB |
Output is correct |
3 |
Correct |
360 ms |
96164 KB |
Output is correct |
4 |
Correct |
307 ms |
96340 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Incorrect |
298 ms |
101848 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Correct |
328 ms |
66784 KB |
Output is correct |
3 |
Correct |
360 ms |
96164 KB |
Output is correct |
4 |
Correct |
307 ms |
96340 KB |
Output is correct |
5 |
Correct |
1 ms |
336 KB |
Output is correct |
6 |
Incorrect |
31 ms |
12392 KB |
Output isn't correct |
7 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Incorrect |
41 ms |
10720 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
336 KB |
Output is correct |
2 |
Incorrect |
5 ms |
1528 KB |
Output isn't correct |
3 |
Halted |
0 ms |
0 KB |
- |