답안 #727059

# 제출 시각 아이디 문제 언어 결과 실행 시간 메모리
727059 2023-04-20T02:10:42 Z SanguineChameleon 이상한 기계 (APIO19_strange_device) C++17
100 / 100
627 ms 44380 KB
#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;
}
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 5 ms 596 KB Output is correct
3 Correct 5 ms 596 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 5 ms 596 KB Output is correct
17 Correct 52 ms 3548 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 1 ms 340 KB Output is correct
3 Correct 1 ms 340 KB Output is correct
4 Correct 1 ms 340 KB Output is correct
5 Correct 302 ms 32500 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 463 ms 32376 KB Output is correct
3 Correct 469 ms 34220 KB Output is correct
4 Correct 497 ms 44184 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 463 ms 32376 KB Output is correct
3 Correct 469 ms 34220 KB Output is correct
4 Correct 497 ms 44184 KB Output is correct
5 Correct 0 ms 340 KB Output is correct
6 Correct 476 ms 44264 KB Output is correct
7 Correct 487 ms 44380 KB Output is correct
8 Correct 489 ms 44248 KB Output is correct
9 Correct 550 ms 33060 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 463 ms 32376 KB Output is correct
3 Correct 469 ms 34220 KB Output is correct
4 Correct 497 ms 44184 KB Output is correct
5 Correct 1 ms 212 KB Output is correct
6 Correct 46 ms 7212 KB Output is correct
7 Correct 52 ms 7180 KB Output is correct
8 Correct 47 ms 7244 KB Output is correct
9 Correct 51 ms 7244 KB Output is correct
10 Correct 47 ms 7160 KB Output is correct
11 Correct 54 ms 7380 KB Output is correct
12 Correct 47 ms 7164 KB Output is correct
13 Correct 54 ms 7132 KB Output is correct
14 Correct 47 ms 7224 KB Output is correct
15 Correct 68 ms 7220 KB Output is correct
16 Correct 60 ms 7212 KB Output is correct
17 Correct 53 ms 7156 KB Output is correct
18 Correct 510 ms 44212 KB Output is correct
19 Correct 532 ms 44336 KB Output is correct
20 Correct 585 ms 44252 KB Output is correct
21 Correct 60 ms 7252 KB Output is correct
22 Correct 42 ms 7252 KB Output is correct
23 Correct 147 ms 16648 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 57 ms 7236 KB Output is correct
3 Correct 55 ms 7216 KB Output is correct
4 Correct 601 ms 35096 KB Output is correct
5 Correct 55 ms 7244 KB Output is correct
6 Correct 54 ms 7248 KB Output is correct
7 Correct 57 ms 7228 KB Output is correct
8 Correct 61 ms 7208 KB Output is correct
9 Correct 55 ms 7224 KB Output is correct
10 Correct 51 ms 7192 KB Output is correct
11 Correct 52 ms 7244 KB Output is correct
12 Correct 44 ms 7148 KB Output is correct
13 Correct 58 ms 7212 KB Output is correct
14 Correct 555 ms 33988 KB Output is correct
15 Correct 74 ms 7240 KB Output is correct
16 Correct 585 ms 33180 KB Output is correct
17 Correct 424 ms 33052 KB Output is correct
18 Correct 1 ms 212 KB Output is correct
# 결과 실행 시간 메모리 Grader output
1 Correct 0 ms 212 KB Output is correct
2 Correct 5 ms 596 KB Output is correct
3 Correct 5 ms 596 KB Output is correct
4 Correct 0 ms 212 KB Output is correct
5 Correct 0 ms 212 KB Output is correct
6 Correct 1 ms 212 KB Output is correct
7 Correct 1 ms 340 KB Output is correct
8 Correct 0 ms 340 KB Output is correct
9 Correct 0 ms 340 KB Output is correct
10 Correct 0 ms 212 KB Output is correct
11 Correct 0 ms 340 KB Output is correct
12 Correct 0 ms 212 KB Output is correct
13 Correct 0 ms 212 KB Output is correct
14 Correct 0 ms 340 KB Output is correct
15 Correct 1 ms 340 KB Output is correct
16 Correct 5 ms 596 KB Output is correct
17 Correct 52 ms 3548 KB Output is correct
18 Correct 0 ms 212 KB Output is correct
19 Correct 0 ms 212 KB Output is correct
20 Correct 1 ms 340 KB Output is correct
21 Correct 1 ms 336 KB Output is correct
22 Correct 1 ms 340 KB Output is correct
23 Correct 1 ms 212 KB Output is correct
24 Correct 0 ms 212 KB Output is correct
25 Correct 1 ms 340 KB Output is correct
26 Correct 1 ms 340 KB Output is correct
27 Correct 1 ms 340 KB Output is correct
28 Correct 302 ms 32500 KB Output is correct
29 Correct 0 ms 212 KB Output is correct
30 Correct 463 ms 32376 KB Output is correct
31 Correct 469 ms 34220 KB Output is correct
32 Correct 497 ms 44184 KB Output is correct
33 Correct 0 ms 340 KB Output is correct
34 Correct 476 ms 44264 KB Output is correct
35 Correct 487 ms 44380 KB Output is correct
36 Correct 489 ms 44248 KB Output is correct
37 Correct 550 ms 33060 KB Output is correct
38 Correct 1 ms 212 KB Output is correct
39 Correct 46 ms 7212 KB Output is correct
40 Correct 52 ms 7180 KB Output is correct
41 Correct 47 ms 7244 KB Output is correct
42 Correct 51 ms 7244 KB Output is correct
43 Correct 47 ms 7160 KB Output is correct
44 Correct 54 ms 7380 KB Output is correct
45 Correct 47 ms 7164 KB Output is correct
46 Correct 54 ms 7132 KB Output is correct
47 Correct 47 ms 7224 KB Output is correct
48 Correct 68 ms 7220 KB Output is correct
49 Correct 60 ms 7212 KB Output is correct
50 Correct 53 ms 7156 KB Output is correct
51 Correct 510 ms 44212 KB Output is correct
52 Correct 532 ms 44336 KB Output is correct
53 Correct 585 ms 44252 KB Output is correct
54 Correct 60 ms 7252 KB Output is correct
55 Correct 42 ms 7252 KB Output is correct
56 Correct 147 ms 16648 KB Output is correct
57 Correct 0 ms 212 KB Output is correct
58 Correct 57 ms 7236 KB Output is correct
59 Correct 55 ms 7216 KB Output is correct
60 Correct 601 ms 35096 KB Output is correct
61 Correct 55 ms 7244 KB Output is correct
62 Correct 54 ms 7248 KB Output is correct
63 Correct 57 ms 7228 KB Output is correct
64 Correct 61 ms 7208 KB Output is correct
65 Correct 55 ms 7224 KB Output is correct
66 Correct 51 ms 7192 KB Output is correct
67 Correct 52 ms 7244 KB Output is correct
68 Correct 44 ms 7148 KB Output is correct
69 Correct 58 ms 7212 KB Output is correct
70 Correct 555 ms 33988 KB Output is correct
71 Correct 74 ms 7240 KB Output is correct
72 Correct 585 ms 33180 KB Output is correct
73 Correct 424 ms 33052 KB Output is correct
74 Correct 1 ms 212 KB Output is correct
75 Correct 0 ms 332 KB Output is correct
76 Correct 1 ms 340 KB Output is correct
77 Correct 1 ms 340 KB Output is correct
78 Correct 1 ms 340 KB Output is correct
79 Correct 5 ms 980 KB Output is correct
80 Correct 627 ms 41100 KB Output is correct
81 Correct 605 ms 41024 KB Output is correct
82 Correct 589 ms 41076 KB Output is correct
83 Correct 547 ms 41044 KB Output is correct
84 Correct 589 ms 41116 KB Output is correct
85 Correct 578 ms 41036 KB Output is correct
86 Correct 149 ms 23628 KB Output is correct
87 Correct 1 ms 332 KB Output is correct