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>
using namespace std;
using ll = long long;
const ll inf = 4e18;
ll mul(ll a, ll b) {
if (a > (inf / b)) {
return inf;
} else {
return a * b;
}
}
int main() {
ios_base::sync_with_stdio(false);
int n;
cin >> n;
ll A, B;
cin >> A >> B;
A /= __gcd(A, B + 1);
ll C = mul(A, B);
vector<pair<ll, int>> go;
for (int i = 0; i < n; ++i) {
ll l, r;
cin >> l >> r;
if (r - l + 1 >= C) {
go.emplace_back(0, 1);
go.emplace_back(C, -1);
} else if (l % C <= r % C) {
l %= C, r %= C;
go.emplace_back(l, 1);
go.emplace_back(r + 1, -1);
} else {
l %= C, r %= C;
go.emplace_back(l, 1);
go.emplace_back(C, -1);
go.emplace_back(0, 1);
go.emplace_back(r + 1, -1);
}
}
sort(go.begin(), go.end());
ll ans = 0;
int l = 0;
int cnt = 0;
while (l < go.size()) {
int r = l;
while (r < go.size() && go[r].first == go[l].first) {
cnt += go[r++].second;
}
if (cnt) {
ans += go[r].first - go[l].first;
}
l = r;
}
cout << ans << "\n";
}
Compilation message (stderr)
strange_device.cpp: In function 'int main()':
strange_device.cpp:47:13: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while (l < go.size()) {
~~^~~~~~~~~~~
strange_device.cpp:49:16: warning: comparison between signed and unsigned integer expressions [-Wsign-compare]
while (r < go.size() && go[r].first == go[l].first) {
~~^~~~~~~~~~~
# | 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... |