# | 제출 시각 | 아이디 | 문제 | 언어 | 결과 | 실행 시간 | 메모리 |
---|---|---|---|---|---|---|---|
800967 | abczz | 이상한 기계 (APIO19_strange_device) | C++14 | 0 ms | 0 KiB |
이 제출은 이전 버전의 oj.uz에서 채점하였습니다. 현재는 제출 당시와는 다른 서버에서 채점을 하기 때문에, 다시 제출하면 결과가 달라질 수도 있습니다.
#include <iostream>
#include <numeric>
#include <array>
#include <algorithm>
#include <vector>
#define ll long long
using namespace std;
ll n, a, b, l, r, x, y, z;
string S;
vector <array<__int128, 2>> V;
__int128 cl, cr, k, p, f;
void print(__int128 v) {
S.clear();
while (v) {
S.push_back(char('0'+v%10));
v /= 10;
}
if (S.empty()) S.push_back('0');
reverse(S.begin(), S.end());
cout << S << '\n';
}
int main() {
cin >> n >> a >> b;
x = (b+1)%a, y = a;
z = gcd(x, y);
k = y/z;
k *= b;
for (int i=0; i<n; ++i) {
cin >> l >> r;
cl = l, cr = r;
cr = min(cl+k-1, cr);
cl %= k;
cr %= k;
if (cl > cr) {
V.push_back({cl, k-1});
V.push_back({0, cr});
}
else V.push_back({cl, cr});
}
sort(V.begin(), V.end());
p = -1;
for (auto [l, r] : V) {
if (l <= p && p <= r) {
f += r-p;
}
else if (p < l) {
f += r-l+1;
}
p = max(p, r);
}
print(f);
}