# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
163996 | staniewzki | Strange Device (APIO19_strange_device) | C++14 | 0 ms | 0 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include<bits/stdc++.h>
using namespace std;
ostream& operator<<(ostream &out, string str) {
for(char c : str) out << c;
return out;
}
template<class L, class R> ostream& operator<<(ostream &out, pair<L, R> p) {
return out << "(" << p.first << ", " << p.second << ")";
}
template<class T> auto operator<<(ostream &out, T a) -> decltype(a.begin(), out) {
out << "{";
for(auto it = a.begin(); it != a.end(); it = next(it))
out << (it != a.begin() ? ", " : "") << *it;
return out << "}";
}
void dump() { cerr << "\n"; }
template<class T, class... Ts> void dump(T a, Ts... x) {
cerr << a << ", ";
dump(x...);
}
#ifdef DEBUG
# define debug(...) cerr << "[" #__VA_ARGS__ "]: ", dump(__VA_ARGS__)
#else
# define debug(...) false
#endif
#define REP(i, n) for(int i = 0; i < n; i++)
#define FOR(i, a, b) for(int i = a; i <= b; i++)
#define ST first
#define ND second
template<class T> int size(T && a) { return a.size(); }
using LL = long long;
using PII = pair<LL, LL>;
int main() {
ios_base::sync_with_stdio(0);
cin.tie(0);
int n;
LL A, B;
cin >> n >> A >> B;
LL d = gcd(A, B + 1);
debug(d);
LL q = -1;
if(1e18 / A < B / d) q = 1e18 + 1;
else q = A * B / d;
debug(q);
vector<PII> events;
auto add_interval = [&](LL l, LL r) {
events.emplace_back(l, +1);
events.emplace_back(r + 1, -1);
};
REP(i, n) {
LL l, r;
cin >> l >> r;
LL a = l / q, b = r / q;
if(a != b) {
if(a + 1 < b) {
cout << q << "\n";
return 0;
}
l %= q, r %= q;
add_interval(l, q - 1);
add_interval(0, r);
}
else add_interval(l % q, r % q);
}
LL ans = 0, last = 0, sum = 0;
sort(events.begin(), events.end());
for(auto &e : events) {
if(sum) ans += (e.ST - last);
sum += e.ND;
last = e.ST;
}
cout << ans << "\n";
}