Prefix Enlightenment

There are $n$ lamps on a line, numbered from $1$ to $n$. Each one has an initial state off ($0$) or on ($1$).

You’re given $k$ subsets $A_1, \ldots, A_k$ of $\{1, 2, \dots, n\}$, such that the intersection of any three subsets is empty. In other words, for all $1 \le i_1 < i_2 < i_3 \le k$, $A_{i_1} \cap A_{i_2} \cap A_{i_3} = \varnothing$.

In one operation, you can choose one of these $k$ subsets and switch the state of all lamps in it. It is guaranteed that, with the given subsets, it’s possible to make all lamps be simultaneously on using this type of operation.

Let $m_i$ be the minimum number of operations you have to do in order to make the $i$ first lamps be simultaneously on. Note that there is no condition upon the state of other lamps (between $i+1$ and $n$), they can be either off or on.

You have to compute $m_i$ for all $1 \le i \le n$.Input

The first line contains two integers $n$ and $k$ ($1 \le n, k \le 3 \cdot 10^5$).

The second line contains a binary string of length $n$, representing the initial state of each lamp (the lamp $i$ is off if $s_i = 0$, on if $s_i = 1$).

The description of each one of the $k$ subsets follows, in the following format:

The first line of the description contains a single integer $c$ ($1 \le c \le n$)  — the number of elements in the subset.

The second line of the description contains $c$ distinct integers $x_1, \ldots, x_c$ ($1 \le x_i \le n$)  — the elements of the subset.

It is guaranteed that:

  • The intersection of any three subsets is empty;
  • It’s possible to make all lamps be simultaneously on using some operations.

Output

You must output $n$ lines. The $i$-th line should contain a single integer $m_i$  — the minimum number of operations required to make the lamps $1$ to $i$ be simultaneously on.Examplesinput

7 3
0011100
3
1 4 6
3
3 4 7
2
2 3

output

1
2
3
3
3
3
3

input

8 6
00110011
3
1 3 8
5
1 2 5 6 7
2
6 8
2
3 5
2
4 7
1
2

output

1
1
1
1
1
1
4
4

input

5 3
00011
3
1 2 3
1
4
3
3 4 5

output

1
1
1
1
1

input

19 5
1001001001100000110
2
2 3
2
5 6
2
8 9
5
12 13 14 15 16
1
19

output

0
1
1
1
2
2
2
3
3
3
3
4
4
4
4
4
4
4
5

Note

In the first example:

  • For $i = 1$, we can just apply one operation on $A_1$, the final states will be $1010110$;
  • For $i = 2$, we can apply operations on $A_1$ and $A_3$, the final states will be $1100110$;
  • For $i \ge 3$, we can apply operations on $A_1$, $A_2$ and $A_3$, the final states will be $1111111$.

In the second example:

  • For $i \le 6$, we can just apply one operation on $A_2$, the final states will be $11111101$;
  • For $i \ge 7$, we can apply operations on $A_1, A_3, A_4, A_6$, the final states will be $11111111$.

Solution:

#include <bits/stdc++.h>

using namespace std;

template <typename A, typename B>
string to_string(pair<A, B> p);

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p);

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p);

string to_string(const string& s) {
return '"' + s + '"';
}

string to_string(const char* s) {
return to_string((string) s);
}

string to_string(bool b) {
return (b ? "true" : "false");
}

string to_string(vector<bool> v) {
bool first = true;
string res = "{";
for (int i = 0; i < static_cast<int>(v.size()); i++) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(v[i]);
}
res += "}";
return res;
}

template <size_t N>
string to_string(bitset<N> v) {
string res = "";
for (size_t i = 0; i < N; i++) {
    res += static_cast<char>('0' + v[i]);
}
return res;
}

template <typename A>
string to_string(A v) {
bool first = true;
string res = "{";
for (const auto &x : v) {
if (!first) {
res += ", ";
}
first = false;
res += to_string(x);
}
res += "}";
return res;
}

template <typename A, typename B>
string to_string(pair<A, B> p) {
return "(" + to_string(p.first) + ", " + to_string(p.second) + ")";
}

template <typename A, typename B, typename C>
string to_string(tuple<A, B, C> p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ")";
}

template <typename A, typename B, typename C, typename D>
string to_string(tuple<A, B, C, D> p) {
return "(" + to_string(get<0>(p)) + ", " + to_string(get<1>(p)) + ", " + to_string(get<2>(p)) + ", " + to_string(get<3>(p)) + ")";
}

void debug_out() { cerr << endl; }

template <typename Head, typename... Tail>
void debug_out(Head H, Tail... T) {
cerr << " " << to_string(H);
  debug_out(T...);
}

#ifdef LOCAL
#define debug(...) cerr << "[" << #__VA_ARGS__ << "]:", debug_out(__VA_ARGS__)
#else
#define debug(...) 42
#endif

class dsu {
 public:
  vector<int> p;
vector<long long> k0;
vector<long long> k1;
int n;

dsu(int _n) : n(_n) {
p.resize(n);
iota(p.begin(), p.end(), 0);
k0.resize(n);
k1.resize(n);
for (int i = 0; i < n / 2; i++) {
      k0[i] = 1;
    }
    for (int i = n / 2; i < n; i++) {
      k1[i] = 1;
    }
  }

  inline int get(int x) {
    return (x == p[x] ? x : (p[x] = get(p[x])));
  }

  inline bool unite(int x, int y) {
    x = get(x);
    y = get(y);
    if (x != y) {
      p[x] = y;
      k0[y] += k0[x];
      k1[y] += k1[x];
      return true;
    }
    return false;
  }
};

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int n, k;
  cin >> n >> k;
string s;
cin >> s;
vector<vector<int>> at(n);
for (int i = 0; i < k; i++) {
    int foo;
    cin >> foo;
for (int j = 0; j < foo; j++) {
      int x;
      cin >> x;
--x;
at[x].push_back(i);
}
}
dsu d(2 * k);
const int inf = (int) 1e9;
long long ans = 0;
auto P = [&](int x) {
return x < k ? x + k : x - k;
  };
  auto Unite = [&](int u, int v) {
    u = d.get(u);
    v = d.get(v);
    if (u == v) {
      return;
    }
    ans -= min(d.k1[u], d.k1[P(u)]);
    ans -= min(d.k1[v], d.k1[P(v)]);
    d.unite(u, v);
    d.unite(P(u), P(v));
    ans += min(d.k1[v], d.k1[P(v)]);
  };
  for (int i = 0; i < n; i++) {
    if (at[i].size() == 0) {
      assert(s[i] == '1');
    } else {
      if (at[i].size() == 1) {
        int v = d.get(s[i] == '0' ? at[i][0] : at[i][0] + k);
        ans -= min(d.k1[v], d.k1[P(v)]);
        d.k1[v] = inf;
        ans += min(d.k1[v], d.k1[P(v)]);
      } else {
        int v = at[i][0];
        int u = at[i][1];
        if (s[i] == '1') {
          Unite(u, v);
        } else {
          Unite(u, v + k);
        }
      }
    }
    cout << ans << '\n';
  }
  return 0;
}