Submission #934294

#TimeUsernameProblemLanguageResultExecution timeMemory
934294vjudge1Strange Device (APIO19_strange_device)C++17
15 / 100
412 ms87756 KiB
// Problem: A - Strange Device
// Contest: Virtual Judge - 2024-02-27 APIO Simulation
// URL: https://vjudge.net/contest/612540#problem/A
// Memory Limit: 512 MB
// Time Limit: 5000 ms
// 
// Powered by CP Editor (https://cpeditor.org)

#include <bits/stdc++.h>
using namespace std;

#define rep(i, a, b) for(int i = a; i < (b); ++i)
#define all(x) begin(x), end(x)
#define sz(x) (int)(x).size()
#define print(A,t) cerr << #A << ": "; copy(all(A),ostream_iterator<t>(cerr," " )); cerr << endl
#define prArr(A,n,t)  cerr << #A << ": "; copy(A,A + n,ostream_iterator<t>(cerr," " )); cerr << endl
#define what_is(x) cerr << #x << " is " << x << endl
typedef long long ll;
typedef long double ld;
typedef pair<int, int> pii;
typedef vector<int> vi;

int n; ll A, B;
const int MAXN = 1000010;
ll L[MAXN], R[MAXN];

void subtask1() {
	set<pair<ll, ll>> ans;
	for (int i = 1; i <= n; i++) {
		for (ll j = L[i]; j <= R[i]; j++) {
			ll x = (j + j / B) % A;
			ll y = j % B;
			ans.insert({x, y});
		}
	}
	cout << sz(ans) << endl;
}

void subtask4() {
	bool halved = false;
	if (A % 2 == 0) {
		A /= 2;
		halved = true;
	}
	bool full = false;
	vector<pair<ll, int>> events;
	for (int i = 1; i <= n; i++) {
		ll l = L[i] % A, r = R[i] % A;
		if (R[i] - L[i] + 1 >= A) {
			full = true;
			break;
		}
		if (l <= r) {
			events.push_back({l, -1});
			events.push_back({r + 1, 1});
		} else {
			events.push_back({0, -1});
			events.push_back({r + 1, 1});
			events.push_back({l, -1});
			events.push_back({A, 1});
		}
	}
	
	if (full) {
		cout << A << endl;
		if (halved) A *= 2;
		return;
	}
	
	ll ans = 0;
	int open = 0;
	sort(all(events));
	ll cur_time = 0;
	for (auto ev : events) {
		// cout << ev.first << " " << ev.second << endl;
		ll passed = ev.first - cur_time;
		if (open > 0) ans += passed;
		open -= ev.second;
		cur_time = ev.first;
	}
	cout << ans << endl;
	
	if (halved) A *= 2;
}

int main() {
	ios_base::sync_with_stdio(false);
	cin.tie(NULL); cout.tie(NULL);
	cin >> n >> A >> B;
	ll S = 0, SL = 0;
	for (int i = 1; i <= n; i++) {
		cin >> L[i] >> R[i];
		S += (R[i] - L[i] + 1);
		SL = max(SL, R[i] - L[i] + 1);
	}
	if (S <= 1000000) subtask1();
	else if (B == 1) subtask4();
	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...