이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <bits/stdc++.h>
#define lsb(x) (x & (-x))
using ull = unsigned long long;
using ll = long long;
using namespace std;
constexpr ll INF = 1e18;
int main() {
#ifdef HOME
ifstream cin("input.in");
ofstream cout("output.out");
#endif
ios::sync_with_stdio(false);
cin.tie(0), cout.tie(0);
int n;
ll a, b;
cin >> n >> a >> b;
vector<pair<ll, ll>> segs(n);
ll sum = 0;
for (int i = 0; i < n; i++) {
cin >> segs[i].first >> segs[i].second;
sum += segs[i].second - segs[i].first + 1;
}
if (INF / a < b / __gcd(a, b + 1)) {
cout << sum;
return 0;
}
multiset<pair<ll, ll>> mst;
ll period = (a / __gcd(a, b + 1)) * b;
for (int i = 0; i < n; i++) {
ll l, r;
tie(l, r) = segs[i];
if (r - l + 1 >= period) {
cout << period << "\n";
return 0;
}
auto Add = [&](ll l, ll r) {
ll new_l = l, new_r = r;
vector<pair<ll, ll>> temp;
auto itr = mst.insert({l, r});
auto prv = itr;
while (prv != mst.begin() && prv->second >= l) {
new_l = min(new_l, prv->first);
temp.push_back(*prv);
prv = prev(prv);
}
if (prv->second >= l) {
new_l = min(new_l, prv->first);
temp.push_back(*prv);
}
auto nxt = itr;
while (nxt != mst.end() && nxt->first <= r) {
new_r = max(new_r, nxt->second);
temp.push_back(*nxt);
nxt = next(nxt);
}
for (auto s : temp) {
mst.erase(s);
}
mst.insert({new_l, new_r});
};
l %= period;
r %= period;
if (l > r) {
Add(l, period - 1);
Add(0, r);
} else {
Add(l, r);
}
}
ll answer = 0;
for (auto itr : mst) {
answer += itr.second - itr.first + 1;
}
cout << answer;
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... |