| #include <algorithm>
|
| #include <iostream>
|
| #include <iterator>
|
| #include <set>
|
| #include <vector>
|
| using namespace std;
|
|
|
| const int LIM = 2000005;
|
|
|
| template<class T>
|
| class segment_tree {
|
| static T join_values(const T &a, const T &b) {
|
| return min(a, b);
|
| }
|
|
|
| static T join_value_with_delta(const T &v, const T &d, int len) {
|
| return v + d;
|
| }
|
|
|
| static T join_deltas(const T &d1, const T &d2) {
|
| return d1 + d2;
|
| }
|
|
|
| int len;
|
| vector<T> value, delta;
|
| vector<bool> pending;
|
|
|
| void build(int i, int lo, int hi, const T &v) {
|
| if (lo == hi) {
|
| value[i] = v;
|
| return;
|
| }
|
| int mid = lo + (hi - lo)/2;
|
| build(i*2 + 1, lo, mid, v);
|
| build(i*2 + 2, mid + 1, hi, v);
|
| value[i] = join_values(value[i*2 + 1], value[i*2 + 2]);
|
| }
|
|
|
| void push_delta(int i, int lo, int hi) {
|
| if (pending[i]) {
|
| value[i] = join_value_with_delta(value[i], delta[i], hi - lo + 1);
|
| if (lo != hi) {
|
| int l = 2*i + 1, r = 2*i + 2;
|
| delta[l] = pending[l] ? join_deltas(delta[l], delta[i]) : delta[i];
|
| delta[r] = pending[r] ? join_deltas(delta[r], delta[i]) : delta[i];
|
| pending[l] = pending[r] = true;
|
| }
|
| pending[i] = false;
|
| }
|
| }
|
|
|
| T query(int i, int lo, int hi, int tgt_lo, int tgt_hi) {
|
| push_delta(i, lo, hi);
|
| if (lo == tgt_lo && hi == tgt_hi) {
|
| return value[i];
|
| }
|
| int mid = lo + (hi - lo)/2;
|
| if (tgt_lo <= mid && mid < tgt_hi) {
|
| return join_values(
|
| query(i*2 + 1, lo, mid, tgt_lo, min(tgt_hi, mid)),
|
| query(i*2 + 2, mid + 1, hi, max(tgt_lo, mid + 1), tgt_hi));
|
| }
|
| if (tgt_lo <= mid) {
|
| return query(i*2 + 1, lo, mid, tgt_lo, min(tgt_hi, mid));
|
| }
|
| return query(i*2 + 2, mid + 1, hi, max(tgt_lo, mid + 1), tgt_hi);
|
| }
|
|
|
| void update(int i, int lo, int hi, int tgt_lo, int tgt_hi, const T &d) {
|
| push_delta(i, lo, hi);
|
| if (hi < tgt_lo || lo > tgt_hi) {
|
| return;
|
| }
|
| if (tgt_lo <= lo && hi <= tgt_hi) {
|
| delta[i] = d;
|
| pending[i] = true;
|
| push_delta(i, lo, hi);
|
| return;
|
| }
|
| update(2*i + 1, lo, (lo + hi)/2, tgt_lo, tgt_hi, d);
|
| update(2*i + 2, (lo + hi)/2 + 1, hi, tgt_lo, tgt_hi, d);
|
| value[i] = join_values(value[2*i + 1], value[2*i + 2]);
|
| }
|
|
|
| public:
|
| segment_tree(int n, const T &v = T())
|
| : len(n), value(4*len), delta(4*len), pending(4*len, false) {
|
| build(0, 0, len - 1, v);
|
| }
|
|
|
| int size() const { return len; }
|
| T at(int i) { return query(i, i); }
|
| T query(int lo, int hi) { return query(0, 0, len - 1, lo, hi); }
|
| void update(int i, const T &d) { update(0, 0, len - 1, i, i, d); }
|
| void update(int lo, int hi, const T &d) { update(0, 0, len - 1, lo, hi, d); }
|
| };
|
|
|
| int R, C, K, S;
|
| string G[LIM];
|
| set<int> B[LIM];
|
| set<int>::iterator I1[LIM], I2[LIM];
|
|
|
| void add_car(segment_tree<int> &st, int r, int c) {
|
|
|
| int i1 = *I1[c], i2 = *I2[c];
|
| B[c].insert(r);
|
| if (r > i1) {
|
| I1[c]++;
|
| st.update(i1 + 1, *I1[c] - 1, 1);
|
| }
|
| if (r < i2) {
|
| I2[c]--;
|
| st.update(*I2[c] + 1, i2 - 1, 1);
|
| }
|
|
|
| if (*I1[c] <= r && r <= *I2[c]) {
|
| st.update(r, r, 1);
|
| }
|
| }
|
|
|
| void remove_car(segment_tree<int> &st, int r, int c) {
|
|
|
| int i1 = *I1[c], i2 = *I2[c];
|
| if (r >= i1) {
|
| I1[c]--;
|
| st.update(*I1[c] + 1, i1 - 1, -1);
|
| }
|
| if (r <= i2) {
|
| I2[c]++;
|
| st.update(i2 + 1, *I2[c] - 1, -1);
|
| }
|
| B[c].erase(r);
|
|
|
| if (*I1[c] <= r && r <= *I2[c]) {
|
| st.update(r, r, -1);
|
| }
|
| }
|
|
|
| long long solve() {
|
| for (int i = 0; i < LIM; i++) {
|
| B[i].clear();
|
| G[i].clear();
|
| }
|
| cin >> R >> C >> K >> S;
|
| for (int j = 0; j < C; j++) {
|
|
|
| for (int i = R - K; i >= 0; i--) {
|
| B[j].insert(-1 - i);
|
| }
|
| I1[j] = B[j].begin();
|
|
|
| for (int i = K - 1; i >= 0; i--) {
|
| B[j].insert(R + 2 + i);
|
| }
|
| I2[j] = prev(B[j].end());
|
| }
|
| segment_tree<int> st(R + 2);
|
| for (int i = 0; i <= R + 1; i++) {
|
| st.update(i, i, abs(i - K));
|
| if (1 <= i && i <= R) {
|
| cin >> G[i];
|
| for (int j = 0; j < C; j++) {
|
| if (G[i][j] == 'X') {
|
| add_car(st, i, j);
|
| }
|
| }
|
| }
|
| }
|
| long long ans = 0;
|
| while (S--) {
|
| int i, j;
|
| cin >> i >> j;
|
| j--;
|
| if (G[i][j] == 'X') {
|
| G[i][j] = '.';
|
| remove_car(st, i, j);
|
| } else {
|
| G[i][j] = 'X';
|
| add_car(st, i, j);
|
| }
|
| ans += st.query(0, R + 1);
|
| }
|
| return ans;
|
| }
|
|
|
| int main() {
|
| int T;
|
| cin >> T;
|
| for (int t = 1; t <= T; t++) {
|
| cout << "Case #" << t << ": " << solve() << endl;
|
| }
|
| return 0;
|
| }
|
|
|