This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#include <bits/stdc++.h>
#define int long long
#define bp __builtin_popcountll
#define pb push_back
#define in(s) freopen(s, "r", stdin);
#define out(s) freopen(s, "w", stdout);
#define inout(s, end1, end2) freopen((string(s) + "." + end1).c_str(), "r", stdin),\
freopen((string(s) + "." + end2).c_str(), "w", stdout);
#define fi first
#define se second
#define bw(i, r, l) for (int i = r - 1; i >= l; i--)
#define fw(i, l, r) for (int i = l; i < r; i++)
#define fa(i, x) for (auto i: x)
using namespace std;
const int mod = 1e9 + 7, inf = 1061109567;
const long long infll = 4557430888798830399;
const int MAX = 4e18 + 1;
int n, A, B;
int mul(int x, int y) {
if ((MAX + y - 1) / y < x) return MAX;
return x * y;
}
map<int, int> mp;
signed main() {
#ifdef BLU
in("blu.inp");
#endif
ios_base::sync_with_stdio(false); cin.tie(0); cout.tie(0);
cin >> n >> A >> B;
int d = A / __gcd(A, B + 1);
//B * d is the cycle length.
int cycleLen = mul(d, B);
fw (i, 0, n) {
int l, r;
cin >> l >> r;
if (r - l + 1 >= cycleLen) {
cout << cycleLen << "\n";
return 0;
}
//ELse it's either 1 or 2 segments.
l %= cycleLen, r %= cycleLen;
if (l > r) {
//Divide into l to cycleLen - 1 and 0 to r.
mp[l]++;
mp[cycleLen]--;
mp[0]++;
mp[r + 1]--;
} else {
//Just 1 segment.
mp[l]++;
mp[r + 1]--;
}
}
if (!mp.count(cycleLen)) mp[cycleLen] = 0;
int curCnt = 0, lst = 0, ans = 0;
fa (i, mp) {
int coord = i.fi, cnt = i.se;
if (curCnt > 0) ans += coord - lst;
curCnt += cnt;
lst = coord;
}
cout << ans;
return 0;
}
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |