#include <iostream> #include <vector> #define st first #define nd second using namespace std; vector<vector<vector<int>>> path; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m, q; cin >> n >> m >> q; vector<vector<int>> hor(n+1, vector<int>(9)), ver (m+1, vector<int>(9)); for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { string s; cin >> s; for (int b = 0; b < s.size(); b++) { if (s[b] == '1') ver[j][s.size()] |= (1<<b); else hor[i][s.size()] |= (1<<b); } } } vector<vector<int>> open_hor(n+1), open_ver(m+1); for (int i = 1; i <= n; i++) { for (int t = 0; t < 840; t++) { for (int l = 2; l <= 8; l++) { if (hor[i][l]&(1<<(t%l))) { open_hor[i].push_back(t); break; } } } } for (int j = 1; j <= m; j++) { for (int t = 0; t < 840; t++) { for (int l = 2; l <= 8; l++) { if (ver[j][l]&(1<<(t%l))) { open_ver[j].push_back(t); break; } } } } vector<pair<pair<int, int>, pair<int, int>>> query(q); vector<int> ts(q), ans(q); for (int i = 0; i < q; i++) { cin >> ts[i]; int a, b, c, d; cin >> a >> b >> c >> d; query[i] = {{a, b}, {c, d}}; } path = vector<vector<vector<int>>>(n+1, vector<vector<int>>(840, vector<int> (14, -1))); for (int i = 1; i <= n; i++) { for (int t:open_hor[i]) { for (int t2 = t; (t2 >= 0) && (path[i-1][t2][0] == -1); t2--) path[i-1][t2][0] = t-t2; } for (int t2 = 839; path[i-1][t2][0] == -1; t2--) path[i-1][t2][0] = *(open_hor[i].begin()) + 840 -t2; } for (int k = 1; k < 14; k++) { for (int i = 0; i <= n; i++) { if (i + (1<<k) > n) break; for (int t = 0; t < 840; t++) { path[i][t][k] = path[i][t][k-1] + path[i+(1<<(k-1))][(t+path[i][t][k-1])%840][k-1]; } } } for (int i = 0; i < q; i++) { if (query[i].st.st < query[i].nd.st) { int a = query[i].st.st, c = query[i].nd.st, sum = 0, t = (ts[i]%840); for (int k = 13; k >= 0; k--) { if (a+(1<<k) <= c) { sum += path[a][t][k]; t = (t+path[a][t][k])%840; a += (1<<k); } } ans[i] += sum; } } path.clear(); path = vector<vector<vector<int>>>(n+1, vector<vector<int>>(840, vector<int> (14, -1))); for (int i = 1; i <= n; i++) { for (int t:open_hor[i]) { for (int t2 = t; (t2 >= 0) && (path[i][t2][0] == -1); t2--) path[i][t2][0] = t-t2; } for (int t2 = 839; path[i][t2][0] == -1; t2--) path[i][t2][0] = *open_hor[i].begin() + 840 -t2; } for (int k = 1; k < 14; k++) { for (int i = n; i >= 0; i--) { if ((i - (1<<k)) < 0) break; for (int t = 0; t < 840; t++) { path[i][t][k] = path[i][t][k-1] + path[i-(1<<(k-1))][(t+path[i][t][k-1])%840][k-1]; } } } for (int i = 0; i < q; i++) { if (query[i].st.st > query[i].nd.st) { int a = query[i].st.st, c = query[i].nd.st, sum = 0, t = (ts[i]%840); for (int k = 13; k >= 0; k--) { if (a-(1<<k) >= c) { sum += path[a][t][k]; t = (t+path[a][t][k])%840; a -= (1<<k); } } ans[i] += sum; } } path.clear(); path = vector<vector<vector<int>>>(m+1, vector<vector<int>>(840, vector<int> (14, -1))); for (int i = 1; i <= m; i++) { for (int t:open_ver[i]) { for (int t2 = t; (t2 >= 0) && (path[i-1][t2][0] == -1); t2--) path[i-1][t2][0] = t-t2; } for (int t2 = 839; path[i-1][t2][0] == -1; t2--) path[i-1][t2][0] = *(open_ver[i].begin()) + 840 -t2; } for (int k = 1; k < 14; k++) { for (int i = 0; i <= m; i++) { if (i + (1<<k) > m) break; for (int t = 0; t < 840; t++) { path[i][t][k] = path[i][t][k-1] + path[i+(1<<(k-1))][(t+path[i][t][k-1])%840][k-1]; } } } for (int i = 0; i < q; i++) { if (query[i].st.nd < query[i].nd.nd) { int b = query[i].st.nd, d = query[i].nd.nd, sum = 0, t = (ts[i]%840); for (int k = 13; k >= 0; k--) { if (b+(1<<k) <= d) { sum += path[b][t][k]; t = (t+path[b][t][k])%840; b += (1<<k); } } ans[i] += sum; } } path.clear(); path = vector<vector<vector<int>>>(m+1, vector<vector<int>>(840, vector<int> (14, -1))); for (int i = 1; i <= m; i++) { for (int t:open_ver[i]) { for (int t2 = t; (t2 >= 0) && (path[i][t2][0] == -1); t2--) path[i][t2][0] = t-t2; } for (int t2 = 839; path[i][t2][0] == -1; t2--) path[i][t2][0] = *open_ver[i].begin() + 840 -t2; } for (int k = 1; k < 14; k++) { for (int i = m; i >= 0; i--) { if (i - (1<<k) < 0) break; for (int t = 0; t < 840; t++) { path[i][t][k] = path[i][t][k-1] + path[i-(1<<(k-1))][(t+path[i][t][k-1])%840][k-1]; } } } for (int i = 0; i < q; i++) { if (query[i].st.nd > query[i].nd.nd) { int b = query[i].st.nd, d = query[i].nd.nd, sum = 0, t = (ts[i]%840); for (int k = 13; k >= 0; k--) { if (b-(1<<k) >= d) { sum += path[b][t][k]; t = (t+path[b][t][k])%840; b -= (1<<k); } } ans[i] += sum; } } path.clear(); for (int i = 0; i < q; i++) cout << ts[i] + ans[i] << '\n'; }
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 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 | #include <iostream> #include <vector> #define st first #define nd second using namespace std; vector<vector<vector<int>>> path; int main() { ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0); int n, m, q; cin >> n >> m >> q; vector<vector<int>> hor(n+1, vector<int>(9)), ver (m+1, vector<int>(9)); for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { string s; cin >> s; for (int b = 0; b < s.size(); b++) { if (s[b] == '1') ver[j][s.size()] |= (1<<b); else hor[i][s.size()] |= (1<<b); } } } vector<vector<int>> open_hor(n+1), open_ver(m+1); for (int i = 1; i <= n; i++) { for (int t = 0; t < 840; t++) { for (int l = 2; l <= 8; l++) { if (hor[i][l]&(1<<(t%l))) { open_hor[i].push_back(t); break; } } } } for (int j = 1; j <= m; j++) { for (int t = 0; t < 840; t++) { for (int l = 2; l <= 8; l++) { if (ver[j][l]&(1<<(t%l))) { open_ver[j].push_back(t); break; } } } } vector<pair<pair<int, int>, pair<int, int>>> query(q); vector<int> ts(q), ans(q); for (int i = 0; i < q; i++) { cin >> ts[i]; int a, b, c, d; cin >> a >> b >> c >> d; query[i] = {{a, b}, {c, d}}; } path = vector<vector<vector<int>>>(n+1, vector<vector<int>>(840, vector<int> (14, -1))); for (int i = 1; i <= n; i++) { for (int t:open_hor[i]) { for (int t2 = t; (t2 >= 0) && (path[i-1][t2][0] == -1); t2--) path[i-1][t2][0] = t-t2; } for (int t2 = 839; path[i-1][t2][0] == -1; t2--) path[i-1][t2][0] = *(open_hor[i].begin()) + 840 -t2; } for (int k = 1; k < 14; k++) { for (int i = 0; i <= n; i++) { if (i + (1<<k) > n) break; for (int t = 0; t < 840; t++) { path[i][t][k] = path[i][t][k-1] + path[i+(1<<(k-1))][(t+path[i][t][k-1])%840][k-1]; } } } for (int i = 0; i < q; i++) { if (query[i].st.st < query[i].nd.st) { int a = query[i].st.st, c = query[i].nd.st, sum = 0, t = (ts[i]%840); for (int k = 13; k >= 0; k--) { if (a+(1<<k) <= c) { sum += path[a][t][k]; t = (t+path[a][t][k])%840; a += (1<<k); } } ans[i] += sum; } } path.clear(); path = vector<vector<vector<int>>>(n+1, vector<vector<int>>(840, vector<int> (14, -1))); for (int i = 1; i <= n; i++) { for (int t:open_hor[i]) { for (int t2 = t; (t2 >= 0) && (path[i][t2][0] == -1); t2--) path[i][t2][0] = t-t2; } for (int t2 = 839; path[i][t2][0] == -1; t2--) path[i][t2][0] = *open_hor[i].begin() + 840 -t2; } for (int k = 1; k < 14; k++) { for (int i = n; i >= 0; i--) { if ((i - (1<<k)) < 0) break; for (int t = 0; t < 840; t++) { path[i][t][k] = path[i][t][k-1] + path[i-(1<<(k-1))][(t+path[i][t][k-1])%840][k-1]; } } } for (int i = 0; i < q; i++) { if (query[i].st.st > query[i].nd.st) { int a = query[i].st.st, c = query[i].nd.st, sum = 0, t = (ts[i]%840); for (int k = 13; k >= 0; k--) { if (a-(1<<k) >= c) { sum += path[a][t][k]; t = (t+path[a][t][k])%840; a -= (1<<k); } } ans[i] += sum; } } path.clear(); path = vector<vector<vector<int>>>(m+1, vector<vector<int>>(840, vector<int> (14, -1))); for (int i = 1; i <= m; i++) { for (int t:open_ver[i]) { for (int t2 = t; (t2 >= 0) && (path[i-1][t2][0] == -1); t2--) path[i-1][t2][0] = t-t2; } for (int t2 = 839; path[i-1][t2][0] == -1; t2--) path[i-1][t2][0] = *(open_ver[i].begin()) + 840 -t2; } for (int k = 1; k < 14; k++) { for (int i = 0; i <= m; i++) { if (i + (1<<k) > m) break; for (int t = 0; t < 840; t++) { path[i][t][k] = path[i][t][k-1] + path[i+(1<<(k-1))][(t+path[i][t][k-1])%840][k-1]; } } } for (int i = 0; i < q; i++) { if (query[i].st.nd < query[i].nd.nd) { int b = query[i].st.nd, d = query[i].nd.nd, sum = 0, t = (ts[i]%840); for (int k = 13; k >= 0; k--) { if (b+(1<<k) <= d) { sum += path[b][t][k]; t = (t+path[b][t][k])%840; b += (1<<k); } } ans[i] += sum; } } path.clear(); path = vector<vector<vector<int>>>(m+1, vector<vector<int>>(840, vector<int> (14, -1))); for (int i = 1; i <= m; i++) { for (int t:open_ver[i]) { for (int t2 = t; (t2 >= 0) && (path[i][t2][0] == -1); t2--) path[i][t2][0] = t-t2; } for (int t2 = 839; path[i][t2][0] == -1; t2--) path[i][t2][0] = *open_ver[i].begin() + 840 -t2; } for (int k = 1; k < 14; k++) { for (int i = m; i >= 0; i--) { if (i - (1<<k) < 0) break; for (int t = 0; t < 840; t++) { path[i][t][k] = path[i][t][k-1] + path[i-(1<<(k-1))][(t+path[i][t][k-1])%840][k-1]; } } } for (int i = 0; i < q; i++) { if (query[i].st.nd > query[i].nd.nd) { int b = query[i].st.nd, d = query[i].nd.nd, sum = 0, t = (ts[i]%840); for (int k = 13; k >= 0; k--) { if (b-(1<<k) >= d) { sum += path[b][t][k]; t = (t+path[b][t][k])%840; b -= (1<<k); } } ans[i] += sum; } } path.clear(); for (int i = 0; i < q; i++) cout << ts[i] + ans[i] << '\n'; } |