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>
using namespace std;
const int N = 1e6 + 1,MOD = 1e9 + 7;
typedef long long ll;
#define int long long
int n,m;
ll ans =0;
bool ok = true;
vector<int> g[N];
void solve(){
vector<pair<int,int>> _;
for(int i = 1;i <= m;i++){
int cur = 1,sum = 0;
for(auto j:g[i]){
sum += j;
}
if(sum == n){
cout << n;
return;
}
vector<pair<int,int>> v,all;
for(auto j:g[i]){
if(n - sum >= cur){
v.push_back({cur,n - sum});
}
cur += j;
sum -= j;
}
if(n - sum >= cur){
v.push_back({cur,n - sum});
}
int k = v.size();
for(int f = 0;f < k;f++){
int mx = v[f].second;
for(int j = f + 1;j <= k;j++){
if(j == k || v[j].first > mx){
all.push_back({v[f].first,mx});
f = j - 1;
break;
}else{
mx = max(mx,v[j].second);
}
}
}
sort(all.begin(),all.end());
if(all.empty()) continue;
if(all[0].first != 1){
_.push_back({1,all[0].first - 1});
}
if(all.back().second != n){
_.push_back({all.back().second + 1,n});
}
for(int j = 1;j < all.size();j++){
int l = all[j - 1].second + 1,r = all[j].first - 1;
if(l <= r){
_.push_back({l,r});
}
}
}
vector<pair<int,int>> v = _;
sort(v.begin(),v.end());
int k = v.size();
for(int i = 0;i < k;i++){
int mx = v[i].second;
for(int j = i + 1;j <= k;j++){
if(j == k || v[j].first > mx){
ans += (mx - v[i].first + 1);
i = j - 1;
break;
}else{
mx = max(mx,v[j].second);
}
}
}
cout << ans << '\n';
}
void test(){
cin >> n >> m;
for(int i = 1;i <= m;i++){
int k;
cin >> k;
if(k > 1){
ok = false;
}
while(k--){
int s;
cin >> s;
g[i].push_back(s);
}
}
solve();
}
signed main(){
ios_base::sync_with_stdio(0);
cin.tie(0);
int t = 1;
//cin >> t;
while(t--){
test();
}
}
Compilation message (stderr)
lasers.cpp: In function 'void solve()':
lasers.cpp:55:25: warning: comparison of integer expressions of different signedness: 'long long int' and 'std::vector<std::pair<long long int, long long int> >::size_type' {aka 'long unsigned int'} [-Wsign-compare]
55 | for(int j = 1;j < all.size();j++){
| ~~^~~~~~~~~~~~
# | 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... |