// 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;
}
bool not_taken (ll pos, const vector<ll> &begs, const vector<ll> &ends) {
ll begs_before = lower_bound(all(begs), pos + 1) - begs.begin();
ll ends_before = lower_bound(all(ends), pos) - ends.begin();
return begs_before == ends_before;
}
ll process (vector<pair<ll, int>> &v) {
sort(all(v));
ll cur_time = 0;
int open = 0;
ll ans = 0;
vector<ll> begs, ends;
for (auto ev : v) {
if (ev.second == -1) begs.push_back(ev.first);
else ends.push_back(ev.first);
ll passed = ev.first - cur_time;
if (open > 0) ans += passed;
open -= ev.second;
cur_time = ev.first;
}
return ans;
}
void solve() {
ll cycle_length = A / __gcd(A, B + 1);
bool got_all = false;
vector<pair<ll, int>> events;
unordered_map<ll, vector<pair<ll, int>>> events_all;
for (int i = 1; i <= n; i++) {
ll l = L[i], r = R[i];
ll k1 = l / B, r1 = l % B;
ll k2 = r / B, r2 = r % B;
if (k1 == k2) {
events_all[k1 % cycle_length].emplace_back(r1, -1);
events_all[k1 % cycle_length].emplace_back(r2 + 1, 1);
} else {
events_all[k1 % cycle_length].emplace_back(r1, -1);
events_all[k1 % cycle_length].emplace_back(B, 1);
events_all[k2 % cycle_length].emplace_back(0, -1);
events_all[k2 % cycle_length].emplace_back(r2 + 1, 1);
}
ll fr = k1 + 1, tt = k2 - 1;
if (fr > tt) continue;
if (tt - fr + 1 >= cycle_length) {
got_all = true;
break;
}
fr %= cycle_length;
tt %= cycle_length;
if (fr <= tt) {
events.push_back({fr, -1});
events.push_back({tt + 1, 1});
} else {
events.push_back({0, -1});
events.push_back({tt + 1, 1});
events.push_back({fr, -1});
events.push_back({cycle_length, 1});
}
}
if (got_all) {
cout << B * cycle_length << endl;
return;
}
sort(all(events));
ll cur_time = 0;
int open = 0;
vector<ll> begs, ends;
ll whole = process(events);
for (auto ev : events) {
if (ev.second == -1) begs.push_back(ev.first);
else ends.push_back(ev.first - 1);
}
ll ans = B * whole;
for (auto [k, v] : events_all) {
if (not_taken(k, begs, ends)) {
ans += process(v);
}
}
cout << ans << endl;
}
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);
}
solve();
return 0;
}
Compilation message
strange_device.cpp: In function 'void solve()':
strange_device.cpp:161:8: warning: unused variable 'cur_time' [-Wunused-variable]
161 | ll cur_time = 0;
| ^~~~~~~~
strange_device.cpp:162:9: warning: unused variable 'open' [-Wunused-variable]
162 | int open = 0;
| ^~~~
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
4 ms |
3404 KB |
Output is correct |
3 |
Correct |
5 ms |
3416 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
4 ms |
3420 KB |
Output is correct |
17 |
Correct |
39 ms |
13736 KB |
Output is correct |
18 |
Correct |
0 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2520 KB |
Output is correct |
2 |
Correct |
1 ms |
2636 KB |
Output is correct |
3 |
Correct |
1 ms |
2392 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
0 ms |
2512 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
2 ms |
2652 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
526 ms |
203496 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
492 ms |
149672 KB |
Output is correct |
3 |
Correct |
1426 ms |
307772 KB |
Output is correct |
4 |
Correct |
1336 ms |
313432 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
492 ms |
149672 KB |
Output is correct |
3 |
Correct |
1426 ms |
307772 KB |
Output is correct |
4 |
Correct |
1336 ms |
313432 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
994 ms |
238800 KB |
Output is correct |
7 |
Correct |
419 ms |
114040 KB |
Output is correct |
8 |
Correct |
959 ms |
238728 KB |
Output is correct |
9 |
Correct |
1032 ms |
238108 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
492 ms |
149672 KB |
Output is correct |
3 |
Correct |
1426 ms |
307772 KB |
Output is correct |
4 |
Correct |
1336 ms |
313432 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
59 ms |
24392 KB |
Output is correct |
7 |
Correct |
97 ms |
30644 KB |
Output is correct |
8 |
Correct |
99 ms |
30204 KB |
Output is correct |
9 |
Correct |
104 ms |
30388 KB |
Output is correct |
10 |
Correct |
54 ms |
21860 KB |
Output is correct |
11 |
Correct |
96 ms |
30148 KB |
Output is correct |
12 |
Correct |
93 ms |
31160 KB |
Output is correct |
13 |
Correct |
91 ms |
30320 KB |
Output is correct |
14 |
Correct |
49 ms |
14348 KB |
Output is correct |
15 |
Correct |
64 ms |
24412 KB |
Output is correct |
16 |
Correct |
33 ms |
14288 KB |
Output is correct |
17 |
Correct |
104 ms |
30180 KB |
Output is correct |
18 |
Correct |
1198 ms |
240456 KB |
Output is correct |
19 |
Correct |
1297 ms |
235172 KB |
Output is correct |
20 |
Correct |
1393 ms |
239184 KB |
Output is correct |
21 |
Correct |
115 ms |
30224 KB |
Output is correct |
22 |
Correct |
104 ms |
30328 KB |
Output is correct |
23 |
Correct |
191 ms |
89448 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
34 ms |
14184 KB |
Output is correct |
3 |
Correct |
32 ms |
13700 KB |
Output is correct |
4 |
Correct |
449 ms |
130248 KB |
Output is correct |
5 |
Correct |
34 ms |
14940 KB |
Output is correct |
6 |
Correct |
33 ms |
14672 KB |
Output is correct |
7 |
Correct |
34 ms |
14828 KB |
Output is correct |
8 |
Correct |
39 ms |
17236 KB |
Output is correct |
9 |
Correct |
43 ms |
17344 KB |
Output is correct |
10 |
Correct |
32 ms |
13660 KB |
Output is correct |
11 |
Correct |
39 ms |
13936 KB |
Output is correct |
12 |
Correct |
32 ms |
13796 KB |
Output is correct |
13 |
Correct |
40 ms |
16720 KB |
Output is correct |
14 |
Correct |
313 ms |
81204 KB |
Output is correct |
15 |
Correct |
36 ms |
13904 KB |
Output is correct |
16 |
Correct |
353 ms |
80812 KB |
Output is correct |
17 |
Correct |
446 ms |
120532 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
2392 KB |
Output is correct |
2 |
Correct |
4 ms |
3404 KB |
Output is correct |
3 |
Correct |
5 ms |
3416 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
0 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2396 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
0 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2396 KB |
Output is correct |
12 |
Correct |
1 ms |
2396 KB |
Output is correct |
13 |
Correct |
1 ms |
2396 KB |
Output is correct |
14 |
Correct |
1 ms |
2396 KB |
Output is correct |
15 |
Correct |
1 ms |
2396 KB |
Output is correct |
16 |
Correct |
4 ms |
3420 KB |
Output is correct |
17 |
Correct |
39 ms |
13736 KB |
Output is correct |
18 |
Correct |
0 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2520 KB |
Output is correct |
20 |
Correct |
1 ms |
2636 KB |
Output is correct |
21 |
Correct |
1 ms |
2392 KB |
Output is correct |
22 |
Correct |
1 ms |
2396 KB |
Output is correct |
23 |
Correct |
0 ms |
2512 KB |
Output is correct |
24 |
Correct |
1 ms |
2396 KB |
Output is correct |
25 |
Correct |
2 ms |
2652 KB |
Output is correct |
26 |
Correct |
1 ms |
2396 KB |
Output is correct |
27 |
Correct |
1 ms |
2652 KB |
Output is correct |
28 |
Correct |
526 ms |
203496 KB |
Output is correct |
29 |
Correct |
1 ms |
2396 KB |
Output is correct |
30 |
Correct |
492 ms |
149672 KB |
Output is correct |
31 |
Correct |
1426 ms |
307772 KB |
Output is correct |
32 |
Correct |
1336 ms |
313432 KB |
Output is correct |
33 |
Correct |
1 ms |
2396 KB |
Output is correct |
34 |
Correct |
994 ms |
238800 KB |
Output is correct |
35 |
Correct |
419 ms |
114040 KB |
Output is correct |
36 |
Correct |
959 ms |
238728 KB |
Output is correct |
37 |
Correct |
1032 ms |
238108 KB |
Output is correct |
38 |
Correct |
1 ms |
2396 KB |
Output is correct |
39 |
Correct |
59 ms |
24392 KB |
Output is correct |
40 |
Correct |
97 ms |
30644 KB |
Output is correct |
41 |
Correct |
99 ms |
30204 KB |
Output is correct |
42 |
Correct |
104 ms |
30388 KB |
Output is correct |
43 |
Correct |
54 ms |
21860 KB |
Output is correct |
44 |
Correct |
96 ms |
30148 KB |
Output is correct |
45 |
Correct |
93 ms |
31160 KB |
Output is correct |
46 |
Correct |
91 ms |
30320 KB |
Output is correct |
47 |
Correct |
49 ms |
14348 KB |
Output is correct |
48 |
Correct |
64 ms |
24412 KB |
Output is correct |
49 |
Correct |
33 ms |
14288 KB |
Output is correct |
50 |
Correct |
104 ms |
30180 KB |
Output is correct |
51 |
Correct |
1198 ms |
240456 KB |
Output is correct |
52 |
Correct |
1297 ms |
235172 KB |
Output is correct |
53 |
Correct |
1393 ms |
239184 KB |
Output is correct |
54 |
Correct |
115 ms |
30224 KB |
Output is correct |
55 |
Correct |
104 ms |
30328 KB |
Output is correct |
56 |
Correct |
191 ms |
89448 KB |
Output is correct |
57 |
Correct |
1 ms |
2396 KB |
Output is correct |
58 |
Correct |
34 ms |
14184 KB |
Output is correct |
59 |
Correct |
32 ms |
13700 KB |
Output is correct |
60 |
Correct |
449 ms |
130248 KB |
Output is correct |
61 |
Correct |
34 ms |
14940 KB |
Output is correct |
62 |
Correct |
33 ms |
14672 KB |
Output is correct |
63 |
Correct |
34 ms |
14828 KB |
Output is correct |
64 |
Correct |
39 ms |
17236 KB |
Output is correct |
65 |
Correct |
43 ms |
17344 KB |
Output is correct |
66 |
Correct |
32 ms |
13660 KB |
Output is correct |
67 |
Correct |
39 ms |
13936 KB |
Output is correct |
68 |
Correct |
32 ms |
13796 KB |
Output is correct |
69 |
Correct |
40 ms |
16720 KB |
Output is correct |
70 |
Correct |
313 ms |
81204 KB |
Output is correct |
71 |
Correct |
36 ms |
13904 KB |
Output is correct |
72 |
Correct |
353 ms |
80812 KB |
Output is correct |
73 |
Correct |
446 ms |
120532 KB |
Output is correct |
74 |
Correct |
1 ms |
2396 KB |
Output is correct |
75 |
Correct |
1 ms |
2396 KB |
Output is correct |
76 |
Correct |
1 ms |
2396 KB |
Output is correct |
77 |
Correct |
1 ms |
2396 KB |
Output is correct |
78 |
Correct |
1 ms |
2396 KB |
Output is correct |
79 |
Correct |
6 ms |
3908 KB |
Output is correct |
80 |
Correct |
409 ms |
79284 KB |
Output is correct |
81 |
Correct |
444 ms |
125780 KB |
Output is correct |
82 |
Correct |
1258 ms |
233696 KB |
Output is correct |
83 |
Correct |
1277 ms |
239256 KB |
Output is correct |
84 |
Correct |
1275 ms |
238304 KB |
Output is correct |
85 |
Correct |
1250 ms |
239588 KB |
Output is correct |
86 |
Correct |
188 ms |
85388 KB |
Output is correct |
87 |
Correct |
1 ms |
2396 KB |
Output is correct |