제출 #744339

#제출 시각아이디문제언어결과실행 시간메모리
744339HamletPetrosyanStrange Device (APIO19_strange_device)C++17
100 / 100
643 ms63808 KiB
#include <bits/stdc++.h>
using namespace std;

#define all(a) (a).begin(), (a).end()
#define add push_back
#define pll pair<long long, long long>
#define ll long long
#define fr first
#define sc second

const int N = 1e6 + 5;

ll n, a, b;
ll k, h;
pll rng[N];
vector<pll> v;

void add_range(ll x, ll y){
	if(x <= y){
		v.add({x, 0});
		v.add({y, 1});
	}
	else {
		v.add({x, 0});
		v.add({h - 1, 1});
		v.add({0, 0});
		v.add({y, 1});
	}
}

void solve(){
	cin >> n >> a >> b;
	ll cnt = 0;
	for(int i = 0; i < n; i++){
		cin >> rng[i].fr >> rng[i].sc;
		cnt += rng[i].sc - rng[i].fr + 1;
	}
	k = a / __gcd(a, b + 1);
	ll maxk = 2'000'000'000'000'000'000ll / b;
	if(k >= maxk){
		cout << cnt << "\n";
		return;
	}
	h = k * b;

	for(int i = 0; i < n; i++){
		if((rng[i].sc - rng[i].fr + 1) / h > 0){
			cout << h << "\n";
			return;
		}
		add_range(rng[i].fr % h, rng[i].sc % h);
	}
	sort(all(v));

	ll ans = 0, opnd = 0, last = -1;
	for(auto& [tiv, id] : v){
		if(opnd) ans += tiv - last;
		else ans++;
		last = tiv;

		if(id) opnd--;
		else opnd++;
	}
	cout << ans << "\n";
}

int main(){
	ios_base::sync_with_stdio(false);
	cin.tie(NULL);
	solve();
	return 0;
}
#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...