#include <bits/stdc++.h> using namespace std; enum {PIONOWY, POZIOMY, TRYWIALNY}; int n, m, queries; int period = 1; void init(); void read_input(); void calc_sums(); void find_rectangles(); void find_borders(); int shortest_path(int t, int a, int b, int c, int d); // pref[dlugosc_stringa][indeks][x][y] vector<int> rectangles[855], reszty[855]; int recttype[855]; vector<vector<int>> poziom[7][8]; string s; string typ[3] = {"PIONOWY", "POZIOMY", "TRYWIALNY"}; vector<int> lewo[855], prawo[855]; int32_t main() { init(); read_input(); calc_sums(); find_rectangles(); find_borders(); while (queries--) { int t, a, b, c, d; cin >> t >> a >> b >> c >> d; cout << t + shortest_path(t, a, b, c, d) << "\n"; } } void find_borders() { for (int t = 0; t < period; t++) { if (recttype[t] == TRYWIALNY) continue; int cur_border = 0; int cur_elem = rectangles[t][0]; for (int i = 0; i < rectangles[t].size(); i++) { if (cur_elem != rectangles[t][i]) cur_elem = rectangles[t][i], cur_border = i; lewo[t].push_back(cur_border); prawo[t].push_back(0); } cur_border = rectangles[t].size() - 1; cur_elem = rectangles[t].back(); for (int i = rectangles[t].size() - 1; i >= 0; i--) { if (cur_elem != rectangles[t][i]) cur_elem = rectangles[t][i], cur_border = i; prawo[t][i] = cur_border; } } } int shortest_path(int t, int a, int b, int c, int d) { t %= period; if (recttype[t] == TRYWIALNY) return 0; if (recttype[t] == POZIOMY) { if (rectangles[t][a] == rectangles[t][c]) return 0; int res = 0; int l = lewo[t][rectangles[t][a]]; int r = prawo[t][rectangles[t][a]]; while (c < l || c > r) { t++, res++; if (t >= period) t -= period; if (recttype[t] != POZIOMY) return res; l = min(l, lewo[t][rectangles[t][l]]); r = max(r, prawo[t][rectangles[t][r]]); } return res; } if (rectangles[t][b] == rectangles[t][d]) return 0; int res = 0; int l = lewo[t][rectangles[t][b]]; int r = prawo[t][rectangles[t][b]]; while (d < l || d > r) { t++, res++; if (t >= period) t -= period; if (recttype[t] != PIONOWY) return res; l = min(l, lewo[t][rectangles[t][l]]); r = max(r, prawo[t][rectangles[t][r]]); } return res; } void init() { cin >> n >> m >> queries; for (int len = 0; len <= 6; len++) { for (int idx = 0; idx < len + 2; idx++) { for (int i1 = 0; i1 <= n; i1++) { poziom[len][idx].push_back({}); for (int j1 = 0; j1 <= m; j1++) { poziom[len][idx][i1].push_back(0); } } } } for (int i = 0; i < 840; i++) { for (int j = 2; j <= 8; j++) { reszty[i].push_back(i % j); } } } void read_input() { for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { cin >> s; int l = s.size(); period = l * period / __gcd(l, period); for (int k = 0; k < s.size(); k++) { if (s[k] == '1') { poziom[l-2][k][i][j]++; } } } } } void calc_sums() { for (int len = 0; len <= 6; len++) { for (int idx = 0; idx < len + 2; idx++) { for (int i1 = 1; i1 <= n; i1++) { for (int j1 = 1; j1 <= m; j1++) { poziom[len][idx][i1][j1] += poziom[len][idx][i1-1][j1] + poziom[len][idx][i1][j1-1] - poziom[len][idx][i1-1][j1-1]; } } } } } int ile_poziomych(int t, int lgx, int lgy, int pdx, int pdy) { int res = 0; for (int len = 0; len <= 6; len++) { int idx = reszty[t][len]; res += poziom[len][idx][pdx][pdy] - poziom[len][idx][lgx-1][pdy] - poziom[len][idx][pdx][lgy-1] + poziom[len][idx][lgx-1][lgy-1]; } return res; } void find_rectangles() { for (int t = 0; t < period; t++) { int j = 0, cnt = 0; for (int i = 1; i <= n; i++) { if (ile_poziomych(t, i, 1, i, m) == m) { if (rectangles[t].empty()) recttype[t] = POZIOMY; while (j < i) { rectangles[t].push_back(cnt); j++; } cnt = j; } } if (!rectangles[t].empty()) { while (j <= n) { rectangles[t].push_back(cnt); j++; } continue; } for (int i = 1; i <= m; i++) { if (ile_poziomych(t, 1, i, n, i) == 0) { if (rectangles[t].empty()) recttype[t] = PIONOWY; while (j < i) { rectangles[t].push_back(cnt); j++; } cnt = j; } } if (!rectangles[t].empty()) { while (j <= m) { rectangles[t].push_back(cnt); j++; } continue; } recttype[t] = TRYWIALNY; } }
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 | #include <bits/stdc++.h> using namespace std; enum {PIONOWY, POZIOMY, TRYWIALNY}; int n, m, queries; int period = 1; void init(); void read_input(); void calc_sums(); void find_rectangles(); void find_borders(); int shortest_path(int t, int a, int b, int c, int d); // pref[dlugosc_stringa][indeks][x][y] vector<int> rectangles[855], reszty[855]; int recttype[855]; vector<vector<int>> poziom[7][8]; string s; string typ[3] = {"PIONOWY", "POZIOMY", "TRYWIALNY"}; vector<int> lewo[855], prawo[855]; int32_t main() { init(); read_input(); calc_sums(); find_rectangles(); find_borders(); while (queries--) { int t, a, b, c, d; cin >> t >> a >> b >> c >> d; cout << t + shortest_path(t, a, b, c, d) << "\n"; } } void find_borders() { for (int t = 0; t < period; t++) { if (recttype[t] == TRYWIALNY) continue; int cur_border = 0; int cur_elem = rectangles[t][0]; for (int i = 0; i < rectangles[t].size(); i++) { if (cur_elem != rectangles[t][i]) cur_elem = rectangles[t][i], cur_border = i; lewo[t].push_back(cur_border); prawo[t].push_back(0); } cur_border = rectangles[t].size() - 1; cur_elem = rectangles[t].back(); for (int i = rectangles[t].size() - 1; i >= 0; i--) { if (cur_elem != rectangles[t][i]) cur_elem = rectangles[t][i], cur_border = i; prawo[t][i] = cur_border; } } } int shortest_path(int t, int a, int b, int c, int d) { t %= period; if (recttype[t] == TRYWIALNY) return 0; if (recttype[t] == POZIOMY) { if (rectangles[t][a] == rectangles[t][c]) return 0; int res = 0; int l = lewo[t][rectangles[t][a]]; int r = prawo[t][rectangles[t][a]]; while (c < l || c > r) { t++, res++; if (t >= period) t -= period; if (recttype[t] != POZIOMY) return res; l = min(l, lewo[t][rectangles[t][l]]); r = max(r, prawo[t][rectangles[t][r]]); } return res; } if (rectangles[t][b] == rectangles[t][d]) return 0; int res = 0; int l = lewo[t][rectangles[t][b]]; int r = prawo[t][rectangles[t][b]]; while (d < l || d > r) { t++, res++; if (t >= period) t -= period; if (recttype[t] != PIONOWY) return res; l = min(l, lewo[t][rectangles[t][l]]); r = max(r, prawo[t][rectangles[t][r]]); } return res; } void init() { cin >> n >> m >> queries; for (int len = 0; len <= 6; len++) { for (int idx = 0; idx < len + 2; idx++) { for (int i1 = 0; i1 <= n; i1++) { poziom[len][idx].push_back({}); for (int j1 = 0; j1 <= m; j1++) { poziom[len][idx][i1].push_back(0); } } } } for (int i = 0; i < 840; i++) { for (int j = 2; j <= 8; j++) { reszty[i].push_back(i % j); } } } void read_input() { for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { cin >> s; int l = s.size(); period = l * period / __gcd(l, period); for (int k = 0; k < s.size(); k++) { if (s[k] == '1') { poziom[l-2][k][i][j]++; } } } } } void calc_sums() { for (int len = 0; len <= 6; len++) { for (int idx = 0; idx < len + 2; idx++) { for (int i1 = 1; i1 <= n; i1++) { for (int j1 = 1; j1 <= m; j1++) { poziom[len][idx][i1][j1] += poziom[len][idx][i1-1][j1] + poziom[len][idx][i1][j1-1] - poziom[len][idx][i1-1][j1-1]; } } } } } int ile_poziomych(int t, int lgx, int lgy, int pdx, int pdy) { int res = 0; for (int len = 0; len <= 6; len++) { int idx = reszty[t][len]; res += poziom[len][idx][pdx][pdy] - poziom[len][idx][lgx-1][pdy] - poziom[len][idx][pdx][lgy-1] + poziom[len][idx][lgx-1][lgy-1]; } return res; } void find_rectangles() { for (int t = 0; t < period; t++) { int j = 0, cnt = 0; for (int i = 1; i <= n; i++) { if (ile_poziomych(t, i, 1, i, m) == m) { if (rectangles[t].empty()) recttype[t] = POZIOMY; while (j < i) { rectangles[t].push_back(cnt); j++; } cnt = j; } } if (!rectangles[t].empty()) { while (j <= n) { rectangles[t].push_back(cnt); j++; } continue; } for (int i = 1; i <= m; i++) { if (ile_poziomych(t, 1, i, n, i) == 0) { if (rectangles[t].empty()) recttype[t] = PIONOWY; while (j < i) { rectangles[t].push_back(cnt); j++; } cnt = j; } } if (!rectangles[t].empty()) { while (j <= m) { rectangles[t].push_back(cnt); j++; } continue; } recttype[t] = TRYWIALNY; } } |