#include <bits/stdc++.h>
#define no "NO"
#define yes "YES"
#define F first
#define S second
#define vec vector
#define task "LASERS"
#define ll long long
#define _mp make_pair
#define ii pair<int, int>
#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()
#define evoid(val) return void(std::cout << val)
#define FOR(i, a, b) for(int i = (a); i <= (b); ++i)
#define FOD(i, b, a) for(int i = (b); i >= (a); --i)
#define unq(x) sort(all(x)); x.resize(unique(all(x)) - x.begin())
using namespace std;
template<typename U, typename V> bool maximize(U &a, V b) {
if (a < b) { a = b; return 1; } return 0;
}
template<typename U, typename V> bool minimize(U &a, V b) {
if (a > b) { a = b; return 1; } return 0;
}
const int N = (int)2e5 + 9;
const int INF = (int)1e9;
const int MOD = (int)1e9 + 7;
int len, numRow;
vector<vector<int>> rows;
namespace sub1 {
bool valid() {
return numRow == 1 && rows[1][0] == 1;
}
void solve() {
if (rows[1][1] <= len / 2) cout << 0 << "\n";
else cout << rows[1][1] - (len - rows[1][1] + 1) + 1 << "\n";
}
}
namespace sub2 {
bool valid() {
FOR(i, 1, numRow) if (rows[i][0] != 1) return false;
return true;
}
void solve() {
int maxWidth = 0;
FOR(i, 1, numRow) maximize(maxWidth, rows[i][1]);
if (maxWidth <= len / 2) cout << 0 << "\n";
else cout << maxWidth - (len - maxWidth+ 1) + 1 << "\n";
}
}
namespace sub34 {
bool valid() {
int sumX = 0;
FOR(i, 1, numRow) sumX += rows[i][0];
return ((numRow == 2 && len <= (int)1e6) || (len <= (int)1e3 && sumX <= (int)1e3));
}
void solve() {
vector<vector<int>> sum;
FOR(i, 1, numRow) {
vector<int> curSum(rows[i][0] + 1, 0);
FOR(j, 1, rows[i][0]) curSum[j] = curSum[j - 1] + rows[i][j];
sum.push_back(curSum);
}
int ans = len;
FOR(i, 1, len) {
bool ok = 1;
FOR(id, 0, numRow - 1) {
int pos = lower_bound(sum[id].begin(), sum[id].end(), i) - sum[id].begin();
if (pos > rows[id + 1][0]) continue;
pos--;
if (sum[id][rows[id + 1][0]] - sum[id][pos] >= len - i + 1) {
ok = 0;
break;
}
}
if (ok) ans--;
}
cout << ans << "\n";
}
}
namespace subFull {
vector<ii> events;
int sum[N];
void solve() {
FOR(i, 1, numRow) {
sum[0] = 0;
FOR(j, 1, rows[i][0]) sum[j] = sum[j - 1] + rows[i][j];
FOR(j, 1, rows[i][0]) {
int val = rows[i][j];
int L = sum[j - 1] + 1, R = len - (sum[rows[i][0]] - sum[j]);
if (L + val - 1 < R - val + 1) continue;
events.push_back({R - val + 1, 1});
events.push_back({L + val, -1});
}
}
sort(events.begin(), events.end());
int ans = 0, sum = 0, curL = -1;
for (auto [u, v] : events) {
sum += v;
if (curL == -1) curL = u;
if (sum == 0) {
ans += u - curL;
curL = -1;
}
}
cout << ans << "\n";
}
}
void main_code() {
cin >> len >> numRow;
rows.resize(numRow + 1);
FOR(i, 1, numRow) {
int n;
cin >> n;
rows[i].push_back(n);
FOR(j, 1, n) {
int x;
cin >> x;
rows[i].push_back(x);
}
}
if (sub1::valid()) sub1::solve();
else if (sub2::valid()) sub2::solve();
else if (sub34::valid()) sub34::solve();
else subFull::solve();
}
int main() {
ios::sync_with_stdio(0); cin.tie(0); cout.tie(0);
if (fopen(task".inp", "r")) {
freopen(task".inp", "r", stdin);
freopen(task".out", "w", stdout);
}
const bool MULTITEST = 0;
int num_test = 1; if (MULTITEST) cin >> num_test;
while (num_test--) { main_code(); cout << "\n"; }
}
/* Lak lu theo dieu nhac */
Compilation message
lasers.cpp: In function 'int main()':
lasers.cpp:149:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
149 | freopen(task".inp", "r", stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
lasers.cpp:150:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
150 | freopen(task".out", "w", stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
79 ms |
33004 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
82 ms |
33396 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
3984 KB |
Output is correct |
2 |
Correct |
24 ms |
1500 KB |
Output is correct |
3 |
Correct |
31 ms |
1420 KB |
Output is correct |
4 |
Correct |
49 ms |
4696 KB |
Output is correct |
5 |
Correct |
21 ms |
2508 KB |
Output is correct |
6 |
Correct |
56 ms |
5588 KB |
Output is correct |
7 |
Correct |
11 ms |
540 KB |
Output is correct |
8 |
Correct |
59 ms |
6900 KB |
Output is correct |
9 |
Correct |
29 ms |
2524 KB |
Output is correct |
10 |
Correct |
46 ms |
4632 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
1 ms |
344 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
1 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
0 ms |
348 KB |
Output is correct |
6 |
Correct |
1 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
1 ms |
348 KB |
Output is correct |
9 |
Correct |
1 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
46 ms |
3984 KB |
Output is correct |
2 |
Correct |
24 ms |
1500 KB |
Output is correct |
3 |
Correct |
31 ms |
1420 KB |
Output is correct |
4 |
Correct |
49 ms |
4696 KB |
Output is correct |
5 |
Correct |
21 ms |
2508 KB |
Output is correct |
6 |
Correct |
56 ms |
5588 KB |
Output is correct |
7 |
Correct |
11 ms |
540 KB |
Output is correct |
8 |
Correct |
59 ms |
6900 KB |
Output is correct |
9 |
Correct |
29 ms |
2524 KB |
Output is correct |
10 |
Correct |
46 ms |
4632 KB |
Output is correct |
11 |
Correct |
1 ms |
344 KB |
Output is correct |
12 |
Correct |
0 ms |
348 KB |
Output is correct |
13 |
Correct |
1 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
0 ms |
348 KB |
Output is correct |
18 |
Correct |
1 ms |
348 KB |
Output is correct |
19 |
Correct |
1 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
74 ms |
31828 KB |
Output is correct |
22 |
Correct |
16 ms |
2768 KB |
Output is correct |
23 |
Correct |
9 ms |
2260 KB |
Output is correct |
24 |
Correct |
38 ms |
8564 KB |
Output is correct |
25 |
Correct |
74 ms |
31908 KB |
Output is correct |
26 |
Correct |
32 ms |
7380 KB |
Output is correct |
27 |
Correct |
13 ms |
2776 KB |
Output is correct |
28 |
Correct |
73 ms |
31772 KB |
Output is correct |
29 |
Correct |
72 ms |
31828 KB |
Output is correct |
30 |
Correct |
27 ms |
5460 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
0 ms |
348 KB |
Output is correct |
2 |
Correct |
0 ms |
348 KB |
Output is correct |
3 |
Correct |
0 ms |
348 KB |
Output is correct |
4 |
Correct |
0 ms |
348 KB |
Output is correct |
5 |
Correct |
1 ms |
344 KB |
Output is correct |
6 |
Correct |
0 ms |
348 KB |
Output is correct |
7 |
Correct |
0 ms |
348 KB |
Output is correct |
8 |
Correct |
0 ms |
344 KB |
Output is correct |
9 |
Correct |
0 ms |
348 KB |
Output is correct |
10 |
Correct |
0 ms |
348 KB |
Output is correct |
11 |
Correct |
1 ms |
348 KB |
Output is correct |
12 |
Correct |
79 ms |
33004 KB |
Output is correct |
13 |
Correct |
0 ms |
348 KB |
Output is correct |
14 |
Correct |
0 ms |
348 KB |
Output is correct |
15 |
Correct |
0 ms |
348 KB |
Output is correct |
16 |
Correct |
1 ms |
348 KB |
Output is correct |
17 |
Correct |
82 ms |
33396 KB |
Output is correct |
18 |
Correct |
0 ms |
348 KB |
Output is correct |
19 |
Correct |
0 ms |
348 KB |
Output is correct |
20 |
Correct |
0 ms |
348 KB |
Output is correct |
21 |
Correct |
46 ms |
3984 KB |
Output is correct |
22 |
Correct |
24 ms |
1500 KB |
Output is correct |
23 |
Correct |
31 ms |
1420 KB |
Output is correct |
24 |
Correct |
49 ms |
4696 KB |
Output is correct |
25 |
Correct |
21 ms |
2508 KB |
Output is correct |
26 |
Correct |
56 ms |
5588 KB |
Output is correct |
27 |
Correct |
11 ms |
540 KB |
Output is correct |
28 |
Correct |
59 ms |
6900 KB |
Output is correct |
29 |
Correct |
29 ms |
2524 KB |
Output is correct |
30 |
Correct |
46 ms |
4632 KB |
Output is correct |
31 |
Correct |
1 ms |
344 KB |
Output is correct |
32 |
Correct |
0 ms |
348 KB |
Output is correct |
33 |
Correct |
1 ms |
348 KB |
Output is correct |
34 |
Correct |
0 ms |
348 KB |
Output is correct |
35 |
Correct |
0 ms |
348 KB |
Output is correct |
36 |
Correct |
1 ms |
348 KB |
Output is correct |
37 |
Correct |
0 ms |
348 KB |
Output is correct |
38 |
Correct |
1 ms |
348 KB |
Output is correct |
39 |
Correct |
1 ms |
348 KB |
Output is correct |
40 |
Correct |
0 ms |
348 KB |
Output is correct |
41 |
Correct |
74 ms |
31828 KB |
Output is correct |
42 |
Correct |
16 ms |
2768 KB |
Output is correct |
43 |
Correct |
9 ms |
2260 KB |
Output is correct |
44 |
Correct |
38 ms |
8564 KB |
Output is correct |
45 |
Correct |
74 ms |
31908 KB |
Output is correct |
46 |
Correct |
32 ms |
7380 KB |
Output is correct |
47 |
Correct |
13 ms |
2776 KB |
Output is correct |
48 |
Correct |
73 ms |
31772 KB |
Output is correct |
49 |
Correct |
72 ms |
31828 KB |
Output is correct |
50 |
Correct |
27 ms |
5460 KB |
Output is correct |
51 |
Correct |
17 ms |
2772 KB |
Output is correct |
52 |
Correct |
77 ms |
32852 KB |
Output is correct |
53 |
Correct |
81 ms |
32852 KB |
Output is correct |
54 |
Correct |
24 ms |
5072 KB |
Output is correct |
55 |
Correct |
57 ms |
10556 KB |
Output is correct |
56 |
Correct |
33 ms |
8148 KB |
Output is correct |
57 |
Correct |
38 ms |
8396 KB |
Output is correct |
58 |
Correct |
79 ms |
33108 KB |
Output is correct |
59 |
Correct |
29 ms |
5328 KB |
Output is correct |
60 |
Correct |
38 ms |
6608 KB |
Output is correct |