이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const int maxn = 4e6;
int n, m, tot, sum[maxn + 3];
ll A, B, C, l, r, temp[maxn + 3];
pair<ll, ll> sect[maxn + 3];
ll gcd(ll a, ll b) {
return b ? gcd(b, a % b) : a;
}
int main() {
cin >> n >> A >> B;
C = A / gcd(A, B + 1) * B; //AB/gcd(A,B+1)
for (int i = 1; i <= n; i++) {
cin >> l >> r;
if (r - l + 1 >= C) {
cout << C << "\n";
return 0;
}
l %= C, r %= C; //"how far they are into" the cycle they are in (I feel so stupid for not seeing that all you need to do is %)
if (l <= r) {
sect[++tot] = make_pair(l, r);
} else {
sect[++tot] = make_pair(0, r);
sect[++tot] = make_pair(l, C - 1);
}
}
for (int i = 1; i <= tot; i++) {
temp[++m] = sect[i].first;
temp[++m] = sect[i].second + 1;
}
sort(temp + 1, temp + m + 1);
for (int i = 1; i <= tot; i++) {
int x = lower_bound(temp + 1, temp + m + 1, sect[i].first) - temp; //find the one that ends first that starts with sect[i].first
int y = lower_bound(temp + 1, temp + m + 1, sect[i].second + 1) - temp; //find the first one that is >= sect[i].second+1
sum[x]++, sum[y]--;
}
ll ans = 0;
for (int i = 1; i <= m; i++) {
sum[i] += sum[i - 1];
if (sum[i]) {
ans += temp[i + 1] - temp[i];
}
}
printf("%lld\n", 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... |