#include <bits/stdc++.h>
using namespace std;
#define sz(x) (int) (x).size()
#define all(x) (x).begin(), (x).end()
const int INF = 1e9 + 5;
signed main() {
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
int N, R;
cin >> N >> R;
vector<int> walls[R];
for (int i = 0; i < R; i++) {
int k;
cin >> k;
walls[i].resize(k + 2);
walls[i][0] = walls[i][k + 1] = 0;
for (int j = 1; j <= k; j++)
cin >> walls[i][j];
}
unordered_map<int, int> diff_mp;
for (int i = 0; i < R; i++) {
vector<int> pref(sz(walls[i]), 0);
vector<pair<int, int>> ranges, merged_ranges;
for (int j = 1; j < sz(walls[i]); j++)
pref[j] = walls[i][j] + pref[j - 1];
for (int j = 0; j < sz(walls[i]) - 1; j++) {
int l = pref[j] + 1;
int r = N - (pref.back() - pref[j]);
if (l <= r) ranges.emplace_back(l, r);
}
ranges.emplace_back(INF, INF);
int cur_l = -1, cur_r = -1;
for (const auto &[l, r] : ranges) {
if (l > cur_r) {
if (cur_l > -1) merged_ranges.emplace_back(cur_l, cur_r);
cur_l = l, cur_r = r;
} else cur_r = max(cur_r, r);
}
for (const auto &[l, r] : merged_ranges) {
// cout << l << " " << r << "\n";
diff_mp[l]++;
diff_mp[r + 1]--;
}
// cout << "\n\n";
}
vector<pair<int, int>> diff;
for (const auto [k, v] : diff_mp) {
diff.emplace_back(k, v);
diff.emplace_back(k - 1, 0);
diff.emplace_back(k + 1, 0);
}
sort(all(diff));
int total = 0;
for (int i = 2; i < sz(diff); i++)
diff[i].second += diff[i - 1].second;
for (int i = 1; i < sz(diff); i++) {
bool move = false;
if (diff[i].second == R) {
int extend = diff[i].first;
total -= extend - 1;
while (i < sz(diff) - 1) {
i++;
move = true;
if (diff[i].second != R) break;
extend = diff[i].first;
}
total += extend;
}
i -= move;
}
cout << N - total;
}
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
460 KB |
Output is correct |
12 |
Correct |
125 ms |
32956 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
448 KB |
Output is correct |
16 |
Correct |
1 ms |
448 KB |
Output is correct |
17 |
Correct |
406 ms |
72544 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
0 ms |
204 KB |
Output is correct |
20 |
Correct |
0 ms |
204 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
153 ms |
28404 KB |
Output is correct |
2 |
Correct |
21 ms |
5924 KB |
Output is correct |
3 |
Correct |
17 ms |
3936 KB |
Output is correct |
4 |
Correct |
194 ms |
40340 KB |
Output is correct |
5 |
Correct |
59 ms |
14172 KB |
Output is correct |
6 |
Correct |
63 ms |
14020 KB |
Output is correct |
7 |
Correct |
2 ms |
844 KB |
Output is correct |
8 |
Correct |
80 ms |
16284 KB |
Output is correct |
9 |
Correct |
120 ms |
22864 KB |
Output is correct |
10 |
Correct |
206 ms |
42048 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
288 KB |
Output is correct |
2 |
Correct |
0 ms |
312 KB |
Output is correct |
3 |
Correct |
0 ms |
332 KB |
Output is correct |
4 |
Correct |
1 ms |
312 KB |
Output is correct |
5 |
Correct |
1 ms |
332 KB |
Output is correct |
6 |
Correct |
1 ms |
332 KB |
Output is correct |
7 |
Correct |
1 ms |
332 KB |
Output is correct |
8 |
Correct |
1 ms |
332 KB |
Output is correct |
9 |
Correct |
1 ms |
304 KB |
Output is correct |
10 |
Correct |
1 ms |
332 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
153 ms |
28404 KB |
Output is correct |
2 |
Correct |
21 ms |
5924 KB |
Output is correct |
3 |
Correct |
17 ms |
3936 KB |
Output is correct |
4 |
Correct |
194 ms |
40340 KB |
Output is correct |
5 |
Correct |
59 ms |
14172 KB |
Output is correct |
6 |
Correct |
63 ms |
14020 KB |
Output is correct |
7 |
Correct |
2 ms |
844 KB |
Output is correct |
8 |
Correct |
80 ms |
16284 KB |
Output is correct |
9 |
Correct |
120 ms |
22864 KB |
Output is correct |
10 |
Correct |
206 ms |
42048 KB |
Output is correct |
11 |
Correct |
1 ms |
288 KB |
Output is correct |
12 |
Correct |
0 ms |
312 KB |
Output is correct |
13 |
Correct |
0 ms |
332 KB |
Output is correct |
14 |
Correct |
1 ms |
312 KB |
Output is correct |
15 |
Correct |
1 ms |
332 KB |
Output is correct |
16 |
Correct |
1 ms |
332 KB |
Output is correct |
17 |
Correct |
1 ms |
332 KB |
Output is correct |
18 |
Correct |
1 ms |
332 KB |
Output is correct |
19 |
Correct |
1 ms |
304 KB |
Output is correct |
20 |
Correct |
1 ms |
332 KB |
Output is correct |
21 |
Correct |
391 ms |
69752 KB |
Output is correct |
22 |
Correct |
62 ms |
11700 KB |
Output is correct |
23 |
Correct |
46 ms |
10412 KB |
Output is correct |
24 |
Correct |
180 ms |
26184 KB |
Output is correct |
25 |
Correct |
379 ms |
69548 KB |
Output is correct |
26 |
Correct |
140 ms |
22880 KB |
Output is correct |
27 |
Correct |
50 ms |
10804 KB |
Output is correct |
28 |
Correct |
393 ms |
69544 KB |
Output is correct |
29 |
Correct |
389 ms |
69816 KB |
Output is correct |
30 |
Correct |
129 ms |
22576 KB |
Output is correct |
# |
Verdict |
Execution time |
Memory |
Grader output |
1 |
Correct |
1 ms |
332 KB |
Output is correct |
2 |
Correct |
0 ms |
204 KB |
Output is correct |
3 |
Correct |
0 ms |
204 KB |
Output is correct |
4 |
Correct |
0 ms |
204 KB |
Output is correct |
5 |
Correct |
0 ms |
204 KB |
Output is correct |
6 |
Correct |
0 ms |
204 KB |
Output is correct |
7 |
Correct |
0 ms |
204 KB |
Output is correct |
8 |
Correct |
0 ms |
204 KB |
Output is correct |
9 |
Correct |
0 ms |
204 KB |
Output is correct |
10 |
Correct |
0 ms |
204 KB |
Output is correct |
11 |
Correct |
1 ms |
460 KB |
Output is correct |
12 |
Correct |
125 ms |
32956 KB |
Output is correct |
13 |
Correct |
0 ms |
204 KB |
Output is correct |
14 |
Correct |
0 ms |
204 KB |
Output is correct |
15 |
Correct |
1 ms |
448 KB |
Output is correct |
16 |
Correct |
1 ms |
448 KB |
Output is correct |
17 |
Correct |
406 ms |
72544 KB |
Output is correct |
18 |
Correct |
0 ms |
204 KB |
Output is correct |
19 |
Correct |
0 ms |
204 KB |
Output is correct |
20 |
Correct |
0 ms |
204 KB |
Output is correct |
21 |
Correct |
153 ms |
28404 KB |
Output is correct |
22 |
Correct |
21 ms |
5924 KB |
Output is correct |
23 |
Correct |
17 ms |
3936 KB |
Output is correct |
24 |
Correct |
194 ms |
40340 KB |
Output is correct |
25 |
Correct |
59 ms |
14172 KB |
Output is correct |
26 |
Correct |
63 ms |
14020 KB |
Output is correct |
27 |
Correct |
2 ms |
844 KB |
Output is correct |
28 |
Correct |
80 ms |
16284 KB |
Output is correct |
29 |
Correct |
120 ms |
22864 KB |
Output is correct |
30 |
Correct |
206 ms |
42048 KB |
Output is correct |
31 |
Correct |
1 ms |
288 KB |
Output is correct |
32 |
Correct |
0 ms |
312 KB |
Output is correct |
33 |
Correct |
0 ms |
332 KB |
Output is correct |
34 |
Correct |
1 ms |
312 KB |
Output is correct |
35 |
Correct |
1 ms |
332 KB |
Output is correct |
36 |
Correct |
1 ms |
332 KB |
Output is correct |
37 |
Correct |
1 ms |
332 KB |
Output is correct |
38 |
Correct |
1 ms |
332 KB |
Output is correct |
39 |
Correct |
1 ms |
304 KB |
Output is correct |
40 |
Correct |
1 ms |
332 KB |
Output is correct |
41 |
Correct |
391 ms |
69752 KB |
Output is correct |
42 |
Correct |
62 ms |
11700 KB |
Output is correct |
43 |
Correct |
46 ms |
10412 KB |
Output is correct |
44 |
Correct |
180 ms |
26184 KB |
Output is correct |
45 |
Correct |
379 ms |
69548 KB |
Output is correct |
46 |
Correct |
140 ms |
22880 KB |
Output is correct |
47 |
Correct |
50 ms |
10804 KB |
Output is correct |
48 |
Correct |
393 ms |
69544 KB |
Output is correct |
49 |
Correct |
389 ms |
69816 KB |
Output is correct |
50 |
Correct |
129 ms |
22576 KB |
Output is correct |
51 |
Correct |
66 ms |
12140 KB |
Output is correct |
52 |
Correct |
426 ms |
73900 KB |
Output is correct |
53 |
Correct |
429 ms |
73784 KB |
Output is correct |
54 |
Correct |
84 ms |
13940 KB |
Output is correct |
55 |
Correct |
273 ms |
43700 KB |
Output is correct |
56 |
Correct |
196 ms |
26256 KB |
Output is correct |
57 |
Correct |
167 ms |
25600 KB |
Output is correct |
58 |
Correct |
457 ms |
74840 KB |
Output is correct |
59 |
Correct |
126 ms |
22420 KB |
Output is correct |
60 |
Correct |
177 ms |
25064 KB |
Output is correct |