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
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
#include <bits/stdc++.h>

using namespace std;

const int N = 15'007;
const int PERIOD = 840;
const int Q = 1'000'007;

int n[2], q;
vector <string> lights[N];

bool rev_ok[2][N][PERIOD];
int pref[2][PERIOD][N];
int can_move[2][PERIOD][N];

int ans[Q];
int dist[2][N][PERIOD];
vector <tuple <int, int, int> > queries[2][N];

int canMove(int t, int p, int v)
{
    int ret = 0;
    while (!rev_ok[t][v][p]) {
        ret++;
        p++;

        if (p == PERIOD) {
            p = 0;
        }
    }

    return ret;
}

void prepare(int t)
{
    for (int p = 0; p < PERIOD; ++p) {
        for (int i = 1; i <= n[t]; ++i) {
            pref[t][p][i] = pref[t][p][i - 1] + !rev_ok[t][i][p];
        }
    }

    for (int p = 0; p < PERIOD; ++p) {
        for (int i = 1; i <= n[t]; ++i) {
            can_move[t][p][i] = canMove(t, p, i);
        }
    }
}

void add_to_solve(int t, int p, int s, int e, int id)
{
    int min_c = min(s, e);
    int max_c = max(s, e);

    if (pref[t][p][max_c] == pref[t][p][min_c]) {
        return;
    }

    if (s < e) {
        s++, e++;
    }

    queries[t][s].push_back({e, p, id});
}

void add_to_solve(int t, int x1, int y1, int x2, int y2, int id)
{
    add_to_solve(0, t, x1, x2, id); 
    add_to_solve(1, t, y1, y2, id);
}

void solve(int t, int from, int to)
{
    if (from >= to) {
        return;
    }

    int mid = (from + to) / 2;
    solve(t, from, mid - 1);
    solve(t, mid + 1, to);

    for (int c = 0; c < 2; ++c) {
        for(int i = from; i <= to; ++i) {
            for (int p = 0; p < PERIOD; ++p) {
                dist[c][i][p] = 0;
            }
        }
    }

    for (int i = mid; i < to; ++i) {
        for (int p = 0; p < PERIOD; ++p) {
            int cur_p = (p + dist[0][i][p]) % PERIOD;
            dist[0][i + 1][p] = dist[0][i][p] + can_move[t][cur_p][i];
            
            cur_p = (p + can_move[t][p][i + 1]) % PERIOD;
            dist[1][i + 1][p] = dist[1][i][cur_p] + can_move[t][p][i + 1];
        }
    }

    for (int i = mid; i > from; --i) {
        for (int p = 0; p < PERIOD; ++p) {
            int cur_p = (p + dist[1][i][p]) % PERIOD;
            dist[1][i - 1][p] = dist[1][i][p] + can_move[t][cur_p][i];

            cur_p = (p + can_move[t][p][i - 1]) % PERIOD;
            dist[0][i - 1][p] = dist[0][i][cur_p] + can_move[t][p][i - 1];
        }
    }

    for (int i = from; i <= mid; ++i) {
        for (const auto &[e, p, id]: queries[t][i]) {
            if (e >= mid && e <= to) {
                int half_p = (p + dist[0][i][p]) % PERIOD;
                ans[id] += dist[0][i][p] + dist[0][e][half_p];
            }
        }
    }

    for (int i = mid; i <= to; ++i) {
        for (const auto &[e, p, id]: queries[t][i]) {
            if (e >= from && e <= mid) {
                int half_p = (p + dist[1][i][p]) % PERIOD;
                ans[id] += dist[1][i][p] + dist[1][e][half_p];
            }
        }
    }
}

int main() 
{
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);

    cin >> n[0] >> n[1] >> q;

    lights[0].resize(n[1] + 1);
    for (int i = 1; i <= n[0]; ++i) {
        lights[i].resize(n[1] + 1);
        for (int j = 1; j <= n[1]; ++j) {
            cin >> lights[i][j];

            int k = 0;
            for (int p = 0; p < PERIOD; ++p) {
                rev_ok[0][i][p] |= lights[i][j][k] == '0';
                rev_ok[1][j][p] |= lights[i][j][k] == '1';

                k++;
                if (k == (int)lights[i][j].size()) {
                    k = 0;
                }
            }
        }
    }

    for (int t = 0; t < 2; ++t) {
        prepare(t);
    }

    for (int i = 1; i <= q; ++i) {
        int t, x1, y1, x2, y2;
        cin >> t >> x1 >> y1 >> x2 >> y2;
        ans[i] = t;
        add_to_solve(t % PERIOD, x1, y1, x2, y2, i);
    }

    for (int t = 0; t < 2; ++t) {
        solve(t, 0, n[t] + 1);
    }

    for (int i = 1; i <= q; ++i) {
        cout << ans[i] << "\n";
    }

    return 0;
}