/** try <3 **/
#include<bits/stdc++.h>
using namespace std;
#define task "sol"
#define lb lower_bound
#define ub upper_bound
#define fi first
#define se second
#define pb push_back
#define mp make_pair
#define zs(v) ((int)(v).size())
#define BIT(x, i) (((x) >> (i)) & 1)
#define CNTBIT __builtin_popcountll
#define ALL(v) (v).begin(),(v).end()
#define endl '\n'
typedef long double ld;
typedef long long ll;
typedef pair<int,int> pii;
const int dx[4]={-1, 0, 1, 0}, dy[4]={0, 1, 0, -1};
const int dxx[8]={-1, -1, 0, 1, 1, 1, 0, -1}, dyy[8]={0, 1, 1, 1, 0, -1, -1, -1};
const ll mod=1000000007; /// 998244353
const int base=311;
const int N=5e5+5;
int n,m;
vector<int> a[N];
void gogo()
{
cin>>m>>n;
for(int i=1;i<=n;++i)
{
int x;
cin>>x;
for(int j=1;j<=x;++j)
{
int k;
cin>>k;
a[i].pb(k);
}
}
vector<pii> all;
for(int i=1;i<=n;++i)
{
vector<pii> seg;
int sum=0;
for(int j:a[i]) sum+=j;
seg.pb(mp(1,m-sum));
seg.pb(mp(sum+1,m));
int s=0;
for(int j:a[i])
{
s+=j;
seg.pb(mp(s+1,m-(sum-s)));
}
sort(ALL(seg));
// for(pii j:seg) cout<<j.fi<<' '<<j.se<<endl;
// cout<<endl;
int L=-1, R=-1;
for(pii i:seg)
{
if(L<=i.fi&&i.fi<=R)
{
R=max(R,i.se);
}
else if(i.fi>R)
{
if(L!=-1)
{
all.pb(mp(L,R));
}
L=i.fi;
R=i.se;
}
}
all.pb(mp(L,R));
}
vector<int> s;
for(pii i:all)
{
s.pb(i.fi);
s.pb(i.se);
// cout<<i.fi<<' '<<i.se<<endl;
}
sort(ALL(s));
s.resize(unique(ALL(s))-s.begin());
int mx=0;
for(pii &i:all)
{
i.fi=lb(ALL(s),i.fi)-s.begin();
i.se=lb(ALL(s),i.se)-s.begin();
i.fi*=2;
i.se*=2;
mx=max(mx,i.se);
}
vector<int> f(mx+5,0);
int ans=0;
for(pii i:all)
{
f[i.fi]++;
f[i.se+1]--;
}
for(int i=1;i<=mx;++i) f[i]+=f[i-1];
for(int i=0;i<=mx;++i)
{
if(f[i]<n) continue;
int j=i;
while(j<mx&&f[j+1]==f[i]) ++j;
// cout<<s[i/2]<<' '<<s[j/2]<<endl;
assert(j%2==0&&i%2==0);
ans+=s[j/2]-s[i/2]+1;
i=j;
}
cout<<m-ans;
}
int main()
{
ios_base::sync_with_stdio(0); cin.tie(0);
if(fopen(task".inp","r"))
{
freopen(task".inp","r",stdin);
freopen(task".out","w",stdout);
}
gogo();
}
Compilation message
lasers.cpp: In function 'int main()':
lasers.cpp:141:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
141 | freopen(task".inp","r",stdin);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~
lasers.cpp:142:16: warning: ignoring return value of 'FILE* freopen(const char*, const char*, FILE*)' declared with attribute 'warn_unused_result' [-Wunused-result]
142 | freopen(task".out","w",stdout);
| ~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
12120 KB |
Output is correct |
2 |
Correct |
7 ms |
12052 KB |
Output is correct |
3 |
Correct |
7 ms |
11980 KB |
Output is correct |
4 |
Correct |
7 ms |
12060 KB |
Output is correct |
5 |
Correct |
7 ms |
12064 KB |
Output is correct |
6 |
Correct |
7 ms |
11980 KB |
Output is correct |
7 |
Correct |
7 ms |
12060 KB |
Output is correct |
8 |
Correct |
7 ms |
12060 KB |
Output is correct |
9 |
Correct |
7 ms |
11980 KB |
Output is correct |
10 |
Correct |
7 ms |
11980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
12120 KB |
Output is correct |
2 |
Correct |
7 ms |
12052 KB |
Output is correct |
3 |
Correct |
7 ms |
11980 KB |
Output is correct |
4 |
Correct |
7 ms |
12060 KB |
Output is correct |
5 |
Correct |
7 ms |
12064 KB |
Output is correct |
6 |
Correct |
7 ms |
11980 KB |
Output is correct |
7 |
Correct |
7 ms |
12060 KB |
Output is correct |
8 |
Correct |
7 ms |
12060 KB |
Output is correct |
9 |
Correct |
7 ms |
11980 KB |
Output is correct |
10 |
Correct |
7 ms |
11980 KB |
Output is correct |
11 |
Correct |
8 ms |
12136 KB |
Output is correct |
12 |
Correct |
309 ms |
69268 KB |
Output is correct |
13 |
Correct |
7 ms |
11980 KB |
Output is correct |
14 |
Correct |
7 ms |
12024 KB |
Output is correct |
15 |
Correct |
8 ms |
12108 KB |
Output is correct |
16 |
Correct |
8 ms |
12068 KB |
Output is correct |
17 |
Correct |
396 ms |
51164 KB |
Output is correct |
18 |
Correct |
7 ms |
11980 KB |
Output is correct |
19 |
Correct |
7 ms |
11980 KB |
Output is correct |
20 |
Correct |
7 ms |
11980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
112 ms |
22592 KB |
Output is correct |
2 |
Correct |
22 ms |
13692 KB |
Output is correct |
3 |
Correct |
24 ms |
13492 KB |
Output is correct |
4 |
Correct |
123 ms |
19516 KB |
Output is correct |
5 |
Correct |
51 ms |
16356 KB |
Output is correct |
6 |
Correct |
69 ms |
20592 KB |
Output is correct |
7 |
Correct |
8 ms |
12236 KB |
Output is correct |
8 |
Correct |
82 ms |
21548 KB |
Output is correct |
9 |
Correct |
62 ms |
17408 KB |
Output is correct |
10 |
Correct |
124 ms |
22808 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
8 ms |
11980 KB |
Output is correct |
2 |
Correct |
8 ms |
12108 KB |
Output is correct |
3 |
Correct |
8 ms |
12060 KB |
Output is correct |
4 |
Correct |
8 ms |
12060 KB |
Output is correct |
5 |
Correct |
8 ms |
11980 KB |
Output is correct |
6 |
Correct |
8 ms |
11980 KB |
Output is correct |
7 |
Correct |
8 ms |
12060 KB |
Output is correct |
8 |
Correct |
8 ms |
12040 KB |
Output is correct |
9 |
Correct |
8 ms |
12096 KB |
Output is correct |
10 |
Correct |
7 ms |
11980 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
112 ms |
22592 KB |
Output is correct |
2 |
Correct |
22 ms |
13692 KB |
Output is correct |
3 |
Correct |
24 ms |
13492 KB |
Output is correct |
4 |
Correct |
123 ms |
19516 KB |
Output is correct |
5 |
Correct |
51 ms |
16356 KB |
Output is correct |
6 |
Correct |
69 ms |
20592 KB |
Output is correct |
7 |
Correct |
8 ms |
12236 KB |
Output is correct |
8 |
Correct |
82 ms |
21548 KB |
Output is correct |
9 |
Correct |
62 ms |
17408 KB |
Output is correct |
10 |
Correct |
124 ms |
22808 KB |
Output is correct |
11 |
Correct |
8 ms |
11980 KB |
Output is correct |
12 |
Correct |
8 ms |
12108 KB |
Output is correct |
13 |
Correct |
8 ms |
12060 KB |
Output is correct |
14 |
Correct |
8 ms |
12060 KB |
Output is correct |
15 |
Correct |
8 ms |
11980 KB |
Output is correct |
16 |
Correct |
8 ms |
11980 KB |
Output is correct |
17 |
Correct |
8 ms |
12060 KB |
Output is correct |
18 |
Correct |
8 ms |
12040 KB |
Output is correct |
19 |
Correct |
8 ms |
12096 KB |
Output is correct |
20 |
Correct |
7 ms |
11980 KB |
Output is correct |
21 |
Correct |
387 ms |
50020 KB |
Output is correct |
22 |
Correct |
51 ms |
15548 KB |
Output is correct |
23 |
Correct |
32 ms |
14420 KB |
Output is correct |
24 |
Correct |
112 ms |
22700 KB |
Output is correct |
25 |
Correct |
393 ms |
49976 KB |
Output is correct |
26 |
Correct |
110 ms |
18824 KB |
Output is correct |
27 |
Correct |
43 ms |
14976 KB |
Output is correct |
28 |
Correct |
389 ms |
49932 KB |
Output is correct |
29 |
Correct |
405 ms |
50056 KB |
Output is correct |
30 |
Correct |
83 ms |
19216 KB |
Output is correct |
# |
결과 |
실행 시간 |
메모리 |
Grader output |
1 |
Correct |
7 ms |
12120 KB |
Output is correct |
2 |
Correct |
7 ms |
12052 KB |
Output is correct |
3 |
Correct |
7 ms |
11980 KB |
Output is correct |
4 |
Correct |
7 ms |
12060 KB |
Output is correct |
5 |
Correct |
7 ms |
12064 KB |
Output is correct |
6 |
Correct |
7 ms |
11980 KB |
Output is correct |
7 |
Correct |
7 ms |
12060 KB |
Output is correct |
8 |
Correct |
7 ms |
12060 KB |
Output is correct |
9 |
Correct |
7 ms |
11980 KB |
Output is correct |
10 |
Correct |
7 ms |
11980 KB |
Output is correct |
11 |
Correct |
8 ms |
12136 KB |
Output is correct |
12 |
Correct |
309 ms |
69268 KB |
Output is correct |
13 |
Correct |
7 ms |
11980 KB |
Output is correct |
14 |
Correct |
7 ms |
12024 KB |
Output is correct |
15 |
Correct |
8 ms |
12108 KB |
Output is correct |
16 |
Correct |
8 ms |
12068 KB |
Output is correct |
17 |
Correct |
396 ms |
51164 KB |
Output is correct |
18 |
Correct |
7 ms |
11980 KB |
Output is correct |
19 |
Correct |
7 ms |
11980 KB |
Output is correct |
20 |
Correct |
7 ms |
11980 KB |
Output is correct |
21 |
Correct |
112 ms |
22592 KB |
Output is correct |
22 |
Correct |
22 ms |
13692 KB |
Output is correct |
23 |
Correct |
24 ms |
13492 KB |
Output is correct |
24 |
Correct |
123 ms |
19516 KB |
Output is correct |
25 |
Correct |
51 ms |
16356 KB |
Output is correct |
26 |
Correct |
69 ms |
20592 KB |
Output is correct |
27 |
Correct |
8 ms |
12236 KB |
Output is correct |
28 |
Correct |
82 ms |
21548 KB |
Output is correct |
29 |
Correct |
62 ms |
17408 KB |
Output is correct |
30 |
Correct |
124 ms |
22808 KB |
Output is correct |
31 |
Correct |
8 ms |
11980 KB |
Output is correct |
32 |
Correct |
8 ms |
12108 KB |
Output is correct |
33 |
Correct |
8 ms |
12060 KB |
Output is correct |
34 |
Correct |
8 ms |
12060 KB |
Output is correct |
35 |
Correct |
8 ms |
11980 KB |
Output is correct |
36 |
Correct |
8 ms |
11980 KB |
Output is correct |
37 |
Correct |
8 ms |
12060 KB |
Output is correct |
38 |
Correct |
8 ms |
12040 KB |
Output is correct |
39 |
Correct |
8 ms |
12096 KB |
Output is correct |
40 |
Correct |
7 ms |
11980 KB |
Output is correct |
41 |
Correct |
387 ms |
50020 KB |
Output is correct |
42 |
Correct |
51 ms |
15548 KB |
Output is correct |
43 |
Correct |
32 ms |
14420 KB |
Output is correct |
44 |
Correct |
112 ms |
22700 KB |
Output is correct |
45 |
Correct |
393 ms |
49976 KB |
Output is correct |
46 |
Correct |
110 ms |
18824 KB |
Output is correct |
47 |
Correct |
43 ms |
14976 KB |
Output is correct |
48 |
Correct |
389 ms |
49932 KB |
Output is correct |
49 |
Correct |
405 ms |
50056 KB |
Output is correct |
50 |
Correct |
83 ms |
19216 KB |
Output is correct |
51 |
Correct |
47 ms |
15808 KB |
Output is correct |
52 |
Correct |
404 ms |
51064 KB |
Output is correct |
53 |
Correct |
409 ms |
50976 KB |
Output is correct |
54 |
Correct |
79 ms |
17104 KB |
Output is correct |
55 |
Correct |
163 ms |
24028 KB |
Output is correct |
56 |
Correct |
104 ms |
22092 KB |
Output is correct |
57 |
Correct |
116 ms |
20388 KB |
Output is correct |
58 |
Correct |
445 ms |
51296 KB |
Output is correct |
59 |
Correct |
93 ms |
18208 KB |
Output is correct |
60 |
Correct |
111 ms |
20160 KB |
Output is correct |