Submission #496123

# Submission time Handle Problem Language Result Execution time Memory
496123 2021-12-20T18:09:21 Z aryan12 Strange Device (APIO19_strange_device) C++17
100 / 100
547 ms 69884 KB
#include <bits/stdc++.h>
using namespace std;
#define int long long

mt19937_64 RNG(chrono::steady_clock::now().time_since_epoch().count());

const int MAXT = 1e18 + 5;

int gcd(int a, int b) {
	if(a % b == 0) {
		return b;
	}
	return gcd(b, a % b);
}

void Solve() {
	int n, a, b;
	cin >> n >> a >> b;
	int repetition = a / gcd(a, b + 1);
	vector<pair<int, int> > v(n + 1);
	int sum = 0;
	for(int i = 1; i <= n; i++) {
		cin >> v[i].first >> v[i].second;
		sum += (v[i].second - v[i].first + 1);
	}
	if(b > MAXT / repetition) {
		cout << sum << "\n";
		return;
	}
	int repetition2 = a / gcd(a, b + 1) * b;
	vector<pair<int, int> > temp;
	for(int i = 1; i <= n; i++) {
		if(v[i].second - v[i].first + 1 >= repetition2) {
			cout << repetition2 << "\n";
			return;
		}
		int startpoint = v[i].first % repetition2;
		int endpoint = v[i].second % repetition2;
		if(startpoint <= endpoint) {
			temp.push_back({startpoint, endpoint});
		}
		else {
			temp.push_back({startpoint, repetition2 - 1});
			temp.push_back({0LL, endpoint});
		}
	}
	sort(temp.begin(), temp.end());
	int ans = 0, left = 0, right = -1;
	for(int i = 0; i < temp.size(); i++) {
		if(temp[i].first > right) {
			ans += (right - left + 1);
			left = temp[i].first;
			right = temp[i].second;
		}
		else {
			right = max(right, temp[i].second);
		}
	}
	ans += (right - left + 1);
	cout << ans << "\n";
}

int32_t main() {
	auto begin = std::chrono::high_resolution_clock::now();
	ios_base::sync_with_stdio(0);
	cin.tie(0);
	int t = 1;
	//cin >> t;
	while(t--) {
		Solve();
	}
	auto end = std::chrono::high_resolution_clock::now();
    auto elapsed = std::chrono::duration_cast<std::chrono::nanoseconds>(end - begin);
    cerr << "Time measured: " << elapsed.count() * 1e-9 << " seconds.\n"; 
	return 0;
}

Compilation message

strange_device.cpp: In function 'void Solve()':
strange_device.cpp:49:19: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
   49 |  for(int i = 0; i < temp.size(); i++) {
      |                 ~~^~~~~~~~~~~~~
# Verdict Execution time Memory Grader output
1 Correct 1 ms 208 KB Output is correct
2 Correct 6 ms 1228 KB Output is correct
3 Correct 5 ms 1368 KB Output is correct
4 Correct 1 ms 208 KB Output is correct
5 Correct 1 ms 208 KB Output is correct
6 Correct 1 ms 256 KB Output is correct
7 Correct 1 ms 320 KB Output is correct
8 Correct 0 ms 208 KB Output is correct
9 Correct 1 ms 208 KB Output is correct
10 Correct 1 ms 208 KB Output is correct
11 Correct 1 ms 320 KB Output is correct
12 Correct 1 ms 316 KB Output is correct
13 Correct 1 ms 208 KB Output is correct
14 Correct 1 ms 208 KB Output is correct
15 Correct 1 ms 208 KB Output is correct
16 Correct 5 ms 1120 KB Output is correct
17 Correct 47 ms 7880 KB Output is correct
18 Correct 1 ms 208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 320 KB Output is correct
2 Correct 1 ms 208 KB Output is correct
3 Correct 1 ms 208 KB Output is correct
4 Correct 1 ms 320 KB Output is correct
5 Correct 1 ms 320 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 208 KB Output is correct
2 Correct 1 ms 336 KB Output is correct
3 Correct 1 ms 336 KB Output is correct
4 Correct 1 ms 336 KB Output is correct
5 Correct 335 ms 57724 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 208 KB Output is correct
2 Correct 466 ms 69828 KB Output is correct
3 Correct 473 ms 69652 KB Output is correct
4 Correct 438 ms 69760 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 208 KB Output is correct
2 Correct 466 ms 69828 KB Output is correct
3 Correct 473 ms 69652 KB Output is correct
4 Correct 438 ms 69760 KB Output is correct
5 Correct 1 ms 320 KB Output is correct
6 Correct 473 ms 69652 KB Output is correct
7 Correct 460 ms 69740 KB Output is correct
8 Correct 470 ms 69788 KB Output is correct
9 Correct 497 ms 69648 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 208 KB Output is correct
2 Correct 466 ms 69828 KB Output is correct
3 Correct 473 ms 69652 KB Output is correct
4 Correct 438 ms 69760 KB Output is correct
5 Correct 1 ms 208 KB Output is correct
6 Correct 47 ms 7708 KB Output is correct
7 Correct 56 ms 7752 KB Output is correct
8 Correct 59 ms 7744 KB Output is correct
9 Correct 62 ms 7752 KB Output is correct
10 Correct 47 ms 7748 KB Output is correct
11 Correct 48 ms 7748 KB Output is correct
12 Correct 47 ms 7764 KB Output is correct
13 Correct 55 ms 7752 KB Output is correct
14 Correct 47 ms 7752 KB Output is correct
15 Correct 51 ms 7892 KB Output is correct
16 Correct 59 ms 7772 KB Output is correct
17 Correct 52 ms 7752 KB Output is correct
18 Correct 532 ms 69616 KB Output is correct
19 Correct 450 ms 69728 KB Output is correct
20 Correct 496 ms 69652 KB Output is correct
21 Correct 64 ms 7752 KB Output is correct
22 Correct 43 ms 7748 KB Output is correct
23 Correct 160 ms 26624 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 324 KB Output is correct
2 Correct 52 ms 7772 KB Output is correct
3 Correct 58 ms 7756 KB Output is correct
4 Correct 511 ms 69780 KB Output is correct
5 Correct 46 ms 7768 KB Output is correct
6 Correct 50 ms 7776 KB Output is correct
7 Correct 46 ms 7732 KB Output is correct
8 Correct 54 ms 7748 KB Output is correct
9 Correct 54 ms 7728 KB Output is correct
10 Correct 49 ms 7748 KB Output is correct
11 Correct 45 ms 7756 KB Output is correct
12 Correct 42 ms 7744 KB Output is correct
13 Correct 53 ms 7760 KB Output is correct
14 Correct 502 ms 69884 KB Output is correct
15 Correct 51 ms 7788 KB Output is correct
16 Correct 498 ms 69624 KB Output is correct
17 Correct 495 ms 69640 KB Output is correct
18 Correct 1 ms 208 KB Output is correct
# Verdict Execution time Memory Grader output
1 Correct 1 ms 208 KB Output is correct
2 Correct 6 ms 1228 KB Output is correct
3 Correct 5 ms 1368 KB Output is correct
4 Correct 1 ms 208 KB Output is correct
5 Correct 1 ms 208 KB Output is correct
6 Correct 1 ms 256 KB Output is correct
7 Correct 1 ms 320 KB Output is correct
8 Correct 0 ms 208 KB Output is correct
9 Correct 1 ms 208 KB Output is correct
10 Correct 1 ms 208 KB Output is correct
11 Correct 1 ms 320 KB Output is correct
12 Correct 1 ms 316 KB Output is correct
13 Correct 1 ms 208 KB Output is correct
14 Correct 1 ms 208 KB Output is correct
15 Correct 1 ms 208 KB Output is correct
16 Correct 5 ms 1120 KB Output is correct
17 Correct 47 ms 7880 KB Output is correct
18 Correct 1 ms 208 KB Output is correct
19 Correct 1 ms 320 KB Output is correct
20 Correct 1 ms 208 KB Output is correct
21 Correct 1 ms 208 KB Output is correct
22 Correct 1 ms 320 KB Output is correct
23 Correct 1 ms 320 KB Output is correct
24 Correct 1 ms 208 KB Output is correct
25 Correct 1 ms 336 KB Output is correct
26 Correct 1 ms 336 KB Output is correct
27 Correct 1 ms 336 KB Output is correct
28 Correct 335 ms 57724 KB Output is correct
29 Correct 1 ms 208 KB Output is correct
30 Correct 466 ms 69828 KB Output is correct
31 Correct 473 ms 69652 KB Output is correct
32 Correct 438 ms 69760 KB Output is correct
33 Correct 1 ms 320 KB Output is correct
34 Correct 473 ms 69652 KB Output is correct
35 Correct 460 ms 69740 KB Output is correct
36 Correct 470 ms 69788 KB Output is correct
37 Correct 497 ms 69648 KB Output is correct
38 Correct 1 ms 208 KB Output is correct
39 Correct 47 ms 7708 KB Output is correct
40 Correct 56 ms 7752 KB Output is correct
41 Correct 59 ms 7744 KB Output is correct
42 Correct 62 ms 7752 KB Output is correct
43 Correct 47 ms 7748 KB Output is correct
44 Correct 48 ms 7748 KB Output is correct
45 Correct 47 ms 7764 KB Output is correct
46 Correct 55 ms 7752 KB Output is correct
47 Correct 47 ms 7752 KB Output is correct
48 Correct 51 ms 7892 KB Output is correct
49 Correct 59 ms 7772 KB Output is correct
50 Correct 52 ms 7752 KB Output is correct
51 Correct 532 ms 69616 KB Output is correct
52 Correct 450 ms 69728 KB Output is correct
53 Correct 496 ms 69652 KB Output is correct
54 Correct 64 ms 7752 KB Output is correct
55 Correct 43 ms 7748 KB Output is correct
56 Correct 160 ms 26624 KB Output is correct
57 Correct 1 ms 324 KB Output is correct
58 Correct 52 ms 7772 KB Output is correct
59 Correct 58 ms 7756 KB Output is correct
60 Correct 511 ms 69780 KB Output is correct
61 Correct 46 ms 7768 KB Output is correct
62 Correct 50 ms 7776 KB Output is correct
63 Correct 46 ms 7732 KB Output is correct
64 Correct 54 ms 7748 KB Output is correct
65 Correct 54 ms 7728 KB Output is correct
66 Correct 49 ms 7748 KB Output is correct
67 Correct 45 ms 7756 KB Output is correct
68 Correct 42 ms 7744 KB Output is correct
69 Correct 53 ms 7760 KB Output is correct
70 Correct 502 ms 69884 KB Output is correct
71 Correct 51 ms 7788 KB Output is correct
72 Correct 498 ms 69624 KB Output is correct
73 Correct 495 ms 69640 KB Output is correct
74 Correct 1 ms 208 KB Output is correct
75 Correct 1 ms 324 KB Output is correct
76 Correct 1 ms 208 KB Output is correct
77 Correct 1 ms 208 KB Output is correct
78 Correct 1 ms 336 KB Output is correct
79 Correct 5 ms 1220 KB Output is correct
80 Correct 538 ms 69632 KB Output is correct
81 Correct 547 ms 69768 KB Output is correct
82 Correct 537 ms 69632 KB Output is correct
83 Correct 506 ms 69696 KB Output is correct
84 Correct 484 ms 69764 KB Output is correct
85 Correct 492 ms 69568 KB Output is correct
86 Correct 161 ms 26428 KB Output is correct
87 Correct 1 ms 208 KB Output is correct