This submission is migrated from previous version of oj.uz, which used different machine for grading. This submission may have different result if resubmitted.
#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 (stderr)
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);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~~
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |
# | Verdict | Execution time | Memory | Grader output |
---|
Fetching results... |