제출 #727059

#제출 시각아이디문제언어결과실행 시간메모리
727059SanguineChameleon이상한 기계 (APIO19_strange_device)C++17
100 / 100
627 ms44380 KiB
#include <bits/stdc++.h>
using namespace std;

void just_do_it();

int main() {
	#ifdef KAMIRULEZ
		freopen("kamirulez.inp", "r", stdin);
		freopen("kamirulez.out", "w", stdout);
	#endif
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	just_do_it();
	return 0;
}

const int maxN = 1e6 + 20;
const long long inf = 1e18L + 20;
long long L[maxN];
long long R[maxN];

long long mul(long long x, long long y) {
	if (x > inf / y) {
		return inf;
	}
	else {
		return x * y;
	}
}

void just_do_it() {
	int N;
	long long A, B;
	cin >> N >> A >> B;
	for (int i = 0; i < N; i++) {
		cin >> L[i] >> R[i];
	}
	long long cycle = mul(A / __gcd(B + 1, A), B);
	if (cycle == inf) {
		long long res = 0;
		for (int i = 0; i < N; i++) {
			res += R[i] - L[i] + 1;
		}
		cout << res;
		return;
	}
	deque<pair<long long, long long>> q;
	for (int i = 0; i < N; i++) {
		if (R[i] - L[i] + 1 >= cycle) {
			cout << cycle;
			return;
		}
		long long x = L[i] % cycle;
		long long y = R[i] % cycle;
		if (x <= y) {
			q.push_back({x, y});
		}
		else {
			q.push_back({x, cycle - 1});
			q.push_back({0, y});
		}
	}
	sort(q.begin(), q.end());
	long long res = 0;
	while (!q.empty()) {
		if ((int)q.size() >= 2 && q[1].first <= q[0].second) {
			pair<long long, long long> merged = {q[0].first, max(q[0].second, q[1].second)};
			q.pop_front();
			q.pop_front();
			q.push_front(merged);
		}
		else {
			res += q[0].second - q[0].first + 1;
			q.pop_front();
		}
	}
	cout << res;
}
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...
#Verdict Execution timeMemoryGrader output
Fetching results...