#include <bits/stdc++.h>
using namespace std;
#define loop(i,n) for (ll i = 0; i < n; i++)
#define ll long long
#define INF 1e9+5
#define MAXN 300007
#define pb push_back
#define mp make_pair
typedef pair<ll,ll> pii;
int main(){
ll l,r,i,j,k,x,maxi=0;
cin>>l>>r;
vector<vector<ll>>row(r);
vector<vector<ll>>dp(r);
for(i=0;i<r;i++){
cin>>x;
maxi=max(maxi,x);
dp[i].pb(0);
for(j=1;j<=x;j++){
cin>>k;
row[i].pb(k);
dp[i].pb(dp[i][j-1]+k);
}
}
if(maxi==1){//só há um por linha
ll savedL=INF, savedR=-1;
for(i=0;i<r;i++){
k=row[i][0];
savedR = max(savedR , k);
savedL = min(savedL, l-k-1);
}
k = savedL+1;
savedR=max(savedR,savedL+1);
k+=(l-savedR);
printf("%lld\n",l-k);
}else{
ll left, right,m,cur=0;
vector<pii>overall;
for(i=0;i<r;i++){
m=dp[i][dp[i].size()-1];
vector<pii>intervals;
if(m==l){
cout<<l<<'\n';
return 0;
}
for(j=0;j<(ll)dp[i].size();j++){
left=dp[i][j]+1;
right=l-(m-left)-1;
if(left>right)continue;
intervals.pb({ left,1 });
intervals.pb({ right+1,-1 });//a partir de right, não está safe
}
sort(intervals.begin(),intervals.end());
for(j=0;j<(ll)intervals.size();j++){
cur+=intervals[j].second;
while(j+1<(ll)intervals.size() && intervals[j+1].first==intervals[j].first){
j++;
cur+=intervals[j].second;
}
if(cur==0){
//a partir desta posição, os lasers foram apanhados em todas as configurações
if(j==(ll)intervals.size()-1){
overall.pb({ intervals[j].first, 1 });
overall.pb({ l+1, -1 });
}else{
overall.pb({ intervals[j].first, 1 });
overall.pb({ intervals[j+1].first, -1 });
}
}
}
}
sort(overall.begin(),overall.end());
/*
for(j=0;j<(ll)overall.size();j++){
cout<<overall[j].first<<" "<<overall[j].second<<" ";}
cout<<endl;*/
cur=0; ll ans=0;
for(i=0;i<(ll)overall.size();i++){
if(overall[i].first==l+1)break;
cur+=overall[i].second;
while(i<(ll)overall.size()-1 && overall[i+1].first==overall[i].first){
i++;
cur+=overall[i].second;
}
if(cur>0){
ans+=(overall[i+1].first-overall[i].first);
}
}
cout<<ans<<endl;
}
return 0;
}
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
256 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
256 KB |
Output is correct |
5 |
Correct |
5 ms |
256 KB |
Output is correct |
6 |
Correct |
4 ms |
256 KB |
Output is correct |
7 |
Correct |
4 ms |
256 KB |
Output is correct |
8 |
Correct |
5 ms |
256 KB |
Output is correct |
9 |
Correct |
5 ms |
256 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
256 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
256 KB |
Output is correct |
5 |
Correct |
5 ms |
256 KB |
Output is correct |
6 |
Correct |
4 ms |
256 KB |
Output is correct |
7 |
Correct |
4 ms |
256 KB |
Output is correct |
8 |
Correct |
5 ms |
256 KB |
Output is correct |
9 |
Correct |
5 ms |
256 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
6 ms |
384 KB |
Output is correct |
12 |
Correct |
507 ms |
55928 KB |
Output is correct |
13 |
Correct |
4 ms |
256 KB |
Output is correct |
14 |
Correct |
5 ms |
256 KB |
Output is correct |
15 |
Correct |
6 ms |
384 KB |
Output is correct |
16 |
Correct |
6 ms |
384 KB |
Output is correct |
17 |
Correct |
506 ms |
56312 KB |
Output is correct |
18 |
Correct |
5 ms |
256 KB |
Output is correct |
19 |
Correct |
4 ms |
256 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
146 ms |
28868 KB |
Output is correct |
2 |
Correct |
29 ms |
5156 KB |
Output is correct |
3 |
Correct |
40 ms |
5520 KB |
Output is correct |
4 |
Correct |
149 ms |
25552 KB |
Output is correct |
5 |
Correct |
76 ms |
13528 KB |
Output is correct |
6 |
Correct |
158 ms |
28100 KB |
Output is correct |
7 |
Correct |
8 ms |
832 KB |
Output is correct |
8 |
Correct |
172 ms |
35344 KB |
Output is correct |
9 |
Correct |
82 ms |
15692 KB |
Output is correct |
10 |
Correct |
157 ms |
32192 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
384 KB |
Output is correct |
2 |
Correct |
4 ms |
256 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
384 KB |
Output is correct |
5 |
Correct |
4 ms |
384 KB |
Output is correct |
6 |
Correct |
5 ms |
384 KB |
Output is correct |
7 |
Correct |
5 ms |
384 KB |
Output is correct |
8 |
Correct |
5 ms |
384 KB |
Output is correct |
9 |
Correct |
5 ms |
432 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
146 ms |
28868 KB |
Output is correct |
2 |
Correct |
29 ms |
5156 KB |
Output is correct |
3 |
Correct |
40 ms |
5520 KB |
Output is correct |
4 |
Correct |
149 ms |
25552 KB |
Output is correct |
5 |
Correct |
76 ms |
13528 KB |
Output is correct |
6 |
Correct |
158 ms |
28100 KB |
Output is correct |
7 |
Correct |
8 ms |
832 KB |
Output is correct |
8 |
Correct |
172 ms |
35344 KB |
Output is correct |
9 |
Correct |
82 ms |
15692 KB |
Output is correct |
10 |
Correct |
157 ms |
32192 KB |
Output is correct |
11 |
Correct |
5 ms |
384 KB |
Output is correct |
12 |
Correct |
4 ms |
256 KB |
Output is correct |
13 |
Correct |
5 ms |
384 KB |
Output is correct |
14 |
Correct |
5 ms |
384 KB |
Output is correct |
15 |
Correct |
4 ms |
384 KB |
Output is correct |
16 |
Correct |
5 ms |
384 KB |
Output is correct |
17 |
Correct |
5 ms |
384 KB |
Output is correct |
18 |
Correct |
5 ms |
384 KB |
Output is correct |
19 |
Correct |
5 ms |
432 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
436 ms |
59144 KB |
Output is correct |
22 |
Correct |
68 ms |
6124 KB |
Output is correct |
23 |
Correct |
47 ms |
6612 KB |
Output is correct |
24 |
Correct |
188 ms |
26504 KB |
Output is correct |
25 |
Correct |
433 ms |
59000 KB |
Output is correct |
26 |
Correct |
147 ms |
16016 KB |
Output is correct |
27 |
Correct |
64 ms |
5480 KB |
Output is correct |
28 |
Correct |
423 ms |
58872 KB |
Output is correct |
29 |
Correct |
440 ms |
59256 KB |
Output is correct |
30 |
Correct |
140 ms |
17192 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
5 ms |
256 KB |
Output is correct |
2 |
Correct |
5 ms |
384 KB |
Output is correct |
3 |
Correct |
5 ms |
384 KB |
Output is correct |
4 |
Correct |
5 ms |
256 KB |
Output is correct |
5 |
Correct |
5 ms |
256 KB |
Output is correct |
6 |
Correct |
4 ms |
256 KB |
Output is correct |
7 |
Correct |
4 ms |
256 KB |
Output is correct |
8 |
Correct |
5 ms |
256 KB |
Output is correct |
9 |
Correct |
5 ms |
256 KB |
Output is correct |
10 |
Correct |
5 ms |
384 KB |
Output is correct |
11 |
Correct |
6 ms |
384 KB |
Output is correct |
12 |
Correct |
507 ms |
55928 KB |
Output is correct |
13 |
Correct |
4 ms |
256 KB |
Output is correct |
14 |
Correct |
5 ms |
256 KB |
Output is correct |
15 |
Correct |
6 ms |
384 KB |
Output is correct |
16 |
Correct |
6 ms |
384 KB |
Output is correct |
17 |
Correct |
506 ms |
56312 KB |
Output is correct |
18 |
Correct |
5 ms |
256 KB |
Output is correct |
19 |
Correct |
4 ms |
256 KB |
Output is correct |
20 |
Correct |
5 ms |
384 KB |
Output is correct |
21 |
Correct |
146 ms |
28868 KB |
Output is correct |
22 |
Correct |
29 ms |
5156 KB |
Output is correct |
23 |
Correct |
40 ms |
5520 KB |
Output is correct |
24 |
Correct |
149 ms |
25552 KB |
Output is correct |
25 |
Correct |
76 ms |
13528 KB |
Output is correct |
26 |
Correct |
158 ms |
28100 KB |
Output is correct |
27 |
Correct |
8 ms |
832 KB |
Output is correct |
28 |
Correct |
172 ms |
35344 KB |
Output is correct |
29 |
Correct |
82 ms |
15692 KB |
Output is correct |
30 |
Correct |
157 ms |
32192 KB |
Output is correct |
31 |
Correct |
5 ms |
384 KB |
Output is correct |
32 |
Correct |
4 ms |
256 KB |
Output is correct |
33 |
Correct |
5 ms |
384 KB |
Output is correct |
34 |
Correct |
5 ms |
384 KB |
Output is correct |
35 |
Correct |
4 ms |
384 KB |
Output is correct |
36 |
Correct |
5 ms |
384 KB |
Output is correct |
37 |
Correct |
5 ms |
384 KB |
Output is correct |
38 |
Correct |
5 ms |
384 KB |
Output is correct |
39 |
Correct |
5 ms |
432 KB |
Output is correct |
40 |
Correct |
5 ms |
384 KB |
Output is correct |
41 |
Correct |
436 ms |
59144 KB |
Output is correct |
42 |
Correct |
68 ms |
6124 KB |
Output is correct |
43 |
Correct |
47 ms |
6612 KB |
Output is correct |
44 |
Correct |
188 ms |
26504 KB |
Output is correct |
45 |
Correct |
433 ms |
59000 KB |
Output is correct |
46 |
Correct |
147 ms |
16016 KB |
Output is correct |
47 |
Correct |
64 ms |
5480 KB |
Output is correct |
48 |
Correct |
423 ms |
58872 KB |
Output is correct |
49 |
Correct |
440 ms |
59256 KB |
Output is correct |
50 |
Correct |
140 ms |
17192 KB |
Output is correct |
51 |
Correct |
72 ms |
7056 KB |
Output is correct |
52 |
Correct |
507 ms |
59972 KB |
Output is correct |
53 |
Correct |
485 ms |
59896 KB |
Output is correct |
54 |
Correct |
130 ms |
10320 KB |
Output is correct |
55 |
Correct |
293 ms |
21956 KB |
Output is correct |
56 |
Correct |
173 ms |
20560 KB |
Output is correct |
57 |
Correct |
193 ms |
17448 KB |
Output is correct |
58 |
Correct |
498 ms |
60536 KB |
Output is correct |
59 |
Correct |
144 ms |
11072 KB |
Output is correct |
60 |
Correct |
201 ms |
13768 KB |
Output is correct |