// 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;
| ^~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
4 ms |
3404 KB |
Output is correct |
3 |
Correct |
4 ms |
3164 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2512 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2648 KB |
Output is correct |
12 |
Correct |
1 ms |
2392 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 |
3416 KB |
Output is correct |
17 |
Correct |
46 ms |
13928 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
0 ms |
2396 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2392 KB |
Output is correct |
5 |
Correct |
1 ms |
2396 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
1 ms |
2780 KB |
Output is correct |
3 |
Correct |
1 ms |
2396 KB |
Output is correct |
4 |
Correct |
1 ms |
2652 KB |
Output is correct |
5 |
Correct |
507 ms |
202628 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
499 ms |
149748 KB |
Output is correct |
3 |
Correct |
1415 ms |
308760 KB |
Output is correct |
4 |
Correct |
1336 ms |
308104 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
499 ms |
149748 KB |
Output is correct |
3 |
Correct |
1415 ms |
308760 KB |
Output is correct |
4 |
Correct |
1336 ms |
308104 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1004 ms |
234988 KB |
Output is correct |
7 |
Correct |
418 ms |
118396 KB |
Output is correct |
8 |
Correct |
976 ms |
242864 KB |
Output is correct |
9 |
Correct |
1103 ms |
243472 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
499 ms |
149748 KB |
Output is correct |
3 |
Correct |
1415 ms |
308760 KB |
Output is correct |
4 |
Correct |
1336 ms |
308104 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
62 ms |
23528 KB |
Output is correct |
7 |
Correct |
102 ms |
30268 KB |
Output is correct |
8 |
Correct |
102 ms |
30532 KB |
Output is correct |
9 |
Correct |
97 ms |
30388 KB |
Output is correct |
10 |
Correct |
53 ms |
21832 KB |
Output is correct |
11 |
Correct |
97 ms |
31036 KB |
Output is correct |
12 |
Correct |
100 ms |
30388 KB |
Output is correct |
13 |
Correct |
93 ms |
31064 KB |
Output is correct |
14 |
Correct |
44 ms |
14224 KB |
Output is correct |
15 |
Correct |
62 ms |
24592 KB |
Output is correct |
16 |
Correct |
32 ms |
14628 KB |
Output is correct |
17 |
Correct |
111 ms |
30396 KB |
Output is correct |
18 |
Correct |
1181 ms |
243896 KB |
Output is correct |
19 |
Correct |
1337 ms |
243420 KB |
Output is correct |
20 |
Correct |
1355 ms |
243784 KB |
Output is correct |
21 |
Correct |
112 ms |
30416 KB |
Output is correct |
22 |
Correct |
98 ms |
31120 KB |
Output is correct |
23 |
Correct |
195 ms |
85768 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
33 ms |
13904 KB |
Output is correct |
3 |
Correct |
32 ms |
13904 KB |
Output is correct |
4 |
Correct |
443 ms |
134612 KB |
Output is correct |
5 |
Correct |
34 ms |
14924 KB |
Output is correct |
6 |
Correct |
36 ms |
14652 KB |
Output is correct |
7 |
Correct |
41 ms |
15044 KB |
Output is correct |
8 |
Correct |
47 ms |
17320 KB |
Output is correct |
9 |
Correct |
42 ms |
17588 KB |
Output is correct |
10 |
Correct |
32 ms |
13660 KB |
Output is correct |
11 |
Correct |
40 ms |
14060 KB |
Output is correct |
12 |
Correct |
32 ms |
14152 KB |
Output is correct |
13 |
Correct |
38 ms |
16352 KB |
Output is correct |
14 |
Correct |
341 ms |
85520 KB |
Output is correct |
15 |
Correct |
36 ms |
14160 KB |
Output is correct |
16 |
Correct |
388 ms |
84952 KB |
Output is correct |
17 |
Correct |
493 ms |
129120 KB |
Output is correct |
18 |
Correct |
1 ms |
2392 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
2396 KB |
Output is correct |
2 |
Correct |
4 ms |
3404 KB |
Output is correct |
3 |
Correct |
4 ms |
3164 KB |
Output is correct |
4 |
Correct |
1 ms |
2396 KB |
Output is correct |
5 |
Correct |
1 ms |
2392 KB |
Output is correct |
6 |
Correct |
1 ms |
2396 KB |
Output is correct |
7 |
Correct |
1 ms |
2396 KB |
Output is correct |
8 |
Correct |
1 ms |
2512 KB |
Output is correct |
9 |
Correct |
1 ms |
2396 KB |
Output is correct |
10 |
Correct |
1 ms |
2396 KB |
Output is correct |
11 |
Correct |
1 ms |
2648 KB |
Output is correct |
12 |
Correct |
1 ms |
2392 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 |
3416 KB |
Output is correct |
17 |
Correct |
46 ms |
13928 KB |
Output is correct |
18 |
Correct |
1 ms |
2396 KB |
Output is correct |
19 |
Correct |
1 ms |
2396 KB |
Output is correct |
20 |
Correct |
0 ms |
2396 KB |
Output is correct |
21 |
Correct |
1 ms |
2396 KB |
Output is correct |
22 |
Correct |
1 ms |
2392 KB |
Output is correct |
23 |
Correct |
1 ms |
2396 KB |
Output is correct |
24 |
Correct |
1 ms |
2396 KB |
Output is correct |
25 |
Correct |
1 ms |
2780 KB |
Output is correct |
26 |
Correct |
1 ms |
2396 KB |
Output is correct |
27 |
Correct |
1 ms |
2652 KB |
Output is correct |
28 |
Correct |
507 ms |
202628 KB |
Output is correct |
29 |
Correct |
1 ms |
2396 KB |
Output is correct |
30 |
Correct |
499 ms |
149748 KB |
Output is correct |
31 |
Correct |
1415 ms |
308760 KB |
Output is correct |
32 |
Correct |
1336 ms |
308104 KB |
Output is correct |
33 |
Correct |
1 ms |
2392 KB |
Output is correct |
34 |
Correct |
1004 ms |
234988 KB |
Output is correct |
35 |
Correct |
418 ms |
118396 KB |
Output is correct |
36 |
Correct |
976 ms |
242864 KB |
Output is correct |
37 |
Correct |
1103 ms |
243472 KB |
Output is correct |
38 |
Correct |
1 ms |
2392 KB |
Output is correct |
39 |
Correct |
62 ms |
23528 KB |
Output is correct |
40 |
Correct |
102 ms |
30268 KB |
Output is correct |
41 |
Correct |
102 ms |
30532 KB |
Output is correct |
42 |
Correct |
97 ms |
30388 KB |
Output is correct |
43 |
Correct |
53 ms |
21832 KB |
Output is correct |
44 |
Correct |
97 ms |
31036 KB |
Output is correct |
45 |
Correct |
100 ms |
30388 KB |
Output is correct |
46 |
Correct |
93 ms |
31064 KB |
Output is correct |
47 |
Correct |
44 ms |
14224 KB |
Output is correct |
48 |
Correct |
62 ms |
24592 KB |
Output is correct |
49 |
Correct |
32 ms |
14628 KB |
Output is correct |
50 |
Correct |
111 ms |
30396 KB |
Output is correct |
51 |
Correct |
1181 ms |
243896 KB |
Output is correct |
52 |
Correct |
1337 ms |
243420 KB |
Output is correct |
53 |
Correct |
1355 ms |
243784 KB |
Output is correct |
54 |
Correct |
112 ms |
30416 KB |
Output is correct |
55 |
Correct |
98 ms |
31120 KB |
Output is correct |
56 |
Correct |
195 ms |
85768 KB |
Output is correct |
57 |
Correct |
1 ms |
2396 KB |
Output is correct |
58 |
Correct |
33 ms |
13904 KB |
Output is correct |
59 |
Correct |
32 ms |
13904 KB |
Output is correct |
60 |
Correct |
443 ms |
134612 KB |
Output is correct |
61 |
Correct |
34 ms |
14924 KB |
Output is correct |
62 |
Correct |
36 ms |
14652 KB |
Output is correct |
63 |
Correct |
41 ms |
15044 KB |
Output is correct |
64 |
Correct |
47 ms |
17320 KB |
Output is correct |
65 |
Correct |
42 ms |
17588 KB |
Output is correct |
66 |
Correct |
32 ms |
13660 KB |
Output is correct |
67 |
Correct |
40 ms |
14060 KB |
Output is correct |
68 |
Correct |
32 ms |
14152 KB |
Output is correct |
69 |
Correct |
38 ms |
16352 KB |
Output is correct |
70 |
Correct |
341 ms |
85520 KB |
Output is correct |
71 |
Correct |
36 ms |
14160 KB |
Output is correct |
72 |
Correct |
388 ms |
84952 KB |
Output is correct |
73 |
Correct |
493 ms |
129120 KB |
Output is correct |
74 |
Correct |
1 ms |
2392 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 |
9 ms |
3956 KB |
Output is correct |
80 |
Correct |
333 ms |
88548 KB |
Output is correct |
81 |
Correct |
464 ms |
134500 KB |
Output is correct |
82 |
Correct |
1269 ms |
243588 KB |
Output is correct |
83 |
Correct |
1289 ms |
242912 KB |
Output is correct |
84 |
Correct |
1259 ms |
243816 KB |
Output is correct |
85 |
Correct |
1303 ms |
244256 KB |
Output is correct |
86 |
Correct |
182 ms |
88688 KB |
Output is correct |
87 |
Correct |
1 ms |
2392 KB |
Output is correct |