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>
typedef long long ll;
int main() {
std::ios_base::sync_with_stdio(false);
std::cin.tie(nullptr);
ll l, r;
std::cin >> l >> r;
std::priority_queue<std::pair<ll, ll>, std::vector<std::pair<ll, ll>>,
std::greater<>>
pq;
std::vector<ll> left(r), sum(r);
std::vector<std::vector<ll>> rows(r);
std::multiset<ll> st;
// x blocks from right ==> l - x + 1 from left
auto f = [&](ll cnt) { return l - (sum[cnt] - left[cnt]) + 1; };
for (ll cnt = 0; cnt < r; ++cnt) {
auto &v = rows[cnt];
ll len;
std::cin >> len;
v.resize(len);
for (auto &i : v) {
std::cin >> i;
}
std::reverse(v.begin(), v.end());
sum[cnt] = std::accumulate(v.begin(), v.end(), 0LL);
pq.push({left[cnt] + v.back(), cnt});
v.pop_back();
st.insert(f(cnt));
}
auto get = [](ll l, ll r, ll c) {
if (r <= c) {
return r - l + 1;
}
if (l > c) {
return 0LL;
}
return c - l + 1;
};
ll ans = l;
for (ll _i = 1; _i <= l; ++_i) {
while (!pq.empty() and pq.top().first < _i) {
auto [l, i] = pq.top();
pq.pop();
st.erase(st.find(f(i)));
left[i] = l;
st.insert(f(i));
if (!rows[i].empty()) {
pq.push({left[i] + rows[i].back(), i});
rows[i].pop_back();
}
}
ll beg = _i, end = !pq.empty() ? pq.top().first : l;
ans -= get(beg + 1, end + 1, *st.begin());
_i = end;
}
std::cout << ans << '\n';
}
# | 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... |