-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathg.cpp
66 lines (62 loc) · 1.57 KB
/
g.cpp
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
#include <bits/stdc++.h>
#define endl '\n'
#define eat cin
#define moo cout
#define int long long
using namespace std;
int T, N, M, pfx[2][500000], p[500000];
string b[500000], c[2][500000], a[500000];
int s(){
multiset<int> m;
int ptrl = 0, ptrr = 0;
for(int j = 0; 1; j++){
while(ptrr < N && p[ptrr] <= j){
ptrr++;
}
while(ptrl < N && (int)a[ptrl].size() <= j - p[ptrl]){
ptrl++;
}
if(ptrl == N) break;
for(int i = ptrl; i < ptrr; i++){
if(a[i][j - p[i]] == '0') continue;
auto it = m.upper_bound(i-j);
if(it == m.begin()) m.insert(i-j);
else{
it--;
m.erase(it);
m.insert(i-j);
}
}
}
return (int)m.size();
}
int32_t main(){
eat.tie(0) -> sync_with_stdio(0);
eat >> T;
while(T--){
eat >> N >> M;
for(int i = 0; i < N; i++){
eat >> b[i];
}
for(int i = 0; i < N; i++){
c[0][i] = "";
c[1][i] = "";
pfx[0][i] = pfx[1][i] = 0;
for(int j = 0; j < (i+1)/2; j++){
pfx[0][i]++;
}
for(int j = 0; j < i/2; j++){
pfx[1][i]++;
}
for(int j = 0; j < M; j++){
c[(i+j)%2][i] += b[i][j];
}
}
copy(c[0], c[0]+N, a);
copy(pfx[0], pfx[0]+N, p);
int ans = s();
copy(c[1], c[1]+N, a);
copy(pfx[1], pfx[1]+N, p);
moo << ans + s() << endl;
}
}