#define _USE_MATH_DEFINES #include <bits/stdc++.h> #ifdef LOC #include "debuglib.h" #else #define deb(...) #define DBP(...) #endif using namespace std; using ll = long long; using Vi = vector<int>; using Pii = pair<int, int>; #define pb push_back #define mp make_pair #define x first #define y second #define rep(i, b, e) for (int i = (b); i < (e); i++) #define each(a, x) for (auto& a : (x)) #define all(x) (x).begin(), (x).end() #define sz(x) int((x).size()) constexpr int LCM = 840; constexpr int INF = 1e9; struct Jumps { Vi reach[LCM], steps[LCM]; vector<Pii> nxt[LCM], jmp[LCM]; // (v, t) -> (u, +dt) Jumps() {} Jumps(const vector<array<bool, LCM>>& pass) { int n = sz(pass); rep(t, 0, LCM) { auto& r = reach[t]; r.resize(n+1); r[n] = n; for (int i = n-1; i >= 0; i--) { r[i] = (pass[i][t] ? r[i+1] : i); } nxt[t].resize(n+1, {INF, INF}); jmp[t].resize(n+1, {n, 0}); steps[t].resize(n+1, 0); } rep(times, 0, 2) { for (int t = LCM-1; t >= 0; t--) { int nt = (t+1) % LCM; rep(i, 0, n+1) { if (reach[t][i] > i) { nxt[t][i] = {reach[t][i], 0}; } else { nxt[t][i] = {nxt[nt][i].x, nxt[nt][i].y+1}; } } } } for (int i = n-1; i >= 0; i--) { rep(t, 0, LCM) { int pi, ptd; tie(pi, ptd) = nxt[t][i]; int pt = (t+ptd) % LCM; int jpi, jptd, jps = steps[pt][pi]; tie(jpi, jptd) = jmp[pt][pi]; int jpt = (pt+jptd) % LCM; int jps2 = steps[jpt][jpi]; if (jps == jps2) { int jpi2, jptd2; tie(jpi2, jptd2) = jmp[jpt][jpi]; jmp[t][i] = {jpi2, ptd+jptd+jptd2}; steps[t][i] = jps*2+1; } else { jmp[t][i] = nxt[t][i]; steps[t][i] = 1; } } } } int query(int t, int from, int to) const { int cur = t; while (from < to) { Pii p = jmp[cur%LCM][from]; if (p.x > to) p = nxt[cur%LCM][from]; from = p.x; cur += p.y; } return cur - t; } }; struct BiJumps { Jumps fwd, bck; int n; BiJumps() {} BiJumps(vector<array<bool, LCM>> pass) { n = sz(pass); fwd = {pass}; reverse(all(pass)); bck = {pass}; } int query(int t, int from, int to) const { if (from == to) return 0; if (from < to) return fwd.query(t, from, to); return bck.query(t, n-from, n-to); } }; int type[LCM]; // 0 = everywhere, 1 = rows, 2 = cols int fastTrip[LCM]; BiJumps jmpRow, jmpCol; int solve(int t, int a, int b, int c, int d) { if (type[t] == 1) { return min(jmpRow.query(t, a, c), fastTrip[t]); } else if (type[t] == 2) { return min(jmpCol.query(t, b, d), fastTrip[t]); } else { return 0; } } int main() { cin.sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(12); int n, m, q; cin >> n >> m >> q; vector<array<array<bool, 8>, 8>> passRow2(n), passCol2(m); char tmp[16]; rep(i, 0, n) { rep(j, 0, m) { cin >> tmp; int len = int(strlen(tmp)); auto &row = passRow2[i][len-1]; auto &col = passCol2[j][len-1]; rep(k, 0, len) { bool val = (tmp[k] == '1'); (val ? col : row)[k] = 1; } } } vector<array<bool, LCM>> passRow(n), passCol(m); rep(i, 0, n) { auto &row = passRow[i]; rep(len, 1, 9) { auto &row2 = passRow2[i][len-1]; rep(k, 0, LCM) { if (row2[k%len]) row[k] = 1; } } } rep(i, 0, m) { auto &col = passCol[i]; rep(len, 1, 9) { auto &col2 = passCol2[i][len-1]; rep(k, 0, LCM) { if (col2[k%len]) col[k] = 1; } } } rep(k, 0, LCM) { bool hor = 1, ver = 1; rep(i, 0, n) if (!passRow[i][k]) ver = 0; rep(i, 0, m) if (!passCol[i][k]) hor = 0; type[k] = (hor && ver ? 0 : (hor ? 1 : 2)); fastTrip[k] = INF; } rep(i, 0, 2) { for (int k = LCM-1; k >= 0; k--) { if (type[k] == 0) { fastTrip[k] = 0; } else { int nxt = (k+1) % LCM; fastTrip[k] = (type[k] == type[nxt] ? fastTrip[nxt]+1 : 1); } } } jmpRow = {move(passRow)}; jmpCol = {move(passCol)}; while (q--) { int t, a, b, c, d; cin >> t >> a >> b >> c >> d; int ans = solve(t%LCM, a, b, c, d) + t; cout << ans << '\n'; } cout << flush; _Exit(0); }
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 | #define _USE_MATH_DEFINES #include <bits/stdc++.h> #ifdef LOC #include "debuglib.h" #else #define deb(...) #define DBP(...) #endif using namespace std; using ll = long long; using Vi = vector<int>; using Pii = pair<int, int>; #define pb push_back #define mp make_pair #define x first #define y second #define rep(i, b, e) for (int i = (b); i < (e); i++) #define each(a, x) for (auto& a : (x)) #define all(x) (x).begin(), (x).end() #define sz(x) int((x).size()) constexpr int LCM = 840; constexpr int INF = 1e9; struct Jumps { Vi reach[LCM], steps[LCM]; vector<Pii> nxt[LCM], jmp[LCM]; // (v, t) -> (u, +dt) Jumps() {} Jumps(const vector<array<bool, LCM>>& pass) { int n = sz(pass); rep(t, 0, LCM) { auto& r = reach[t]; r.resize(n+1); r[n] = n; for (int i = n-1; i >= 0; i--) { r[i] = (pass[i][t] ? r[i+1] : i); } nxt[t].resize(n+1, {INF, INF}); jmp[t].resize(n+1, {n, 0}); steps[t].resize(n+1, 0); } rep(times, 0, 2) { for (int t = LCM-1; t >= 0; t--) { int nt = (t+1) % LCM; rep(i, 0, n+1) { if (reach[t][i] > i) { nxt[t][i] = {reach[t][i], 0}; } else { nxt[t][i] = {nxt[nt][i].x, nxt[nt][i].y+1}; } } } } for (int i = n-1; i >= 0; i--) { rep(t, 0, LCM) { int pi, ptd; tie(pi, ptd) = nxt[t][i]; int pt = (t+ptd) % LCM; int jpi, jptd, jps = steps[pt][pi]; tie(jpi, jptd) = jmp[pt][pi]; int jpt = (pt+jptd) % LCM; int jps2 = steps[jpt][jpi]; if (jps == jps2) { int jpi2, jptd2; tie(jpi2, jptd2) = jmp[jpt][jpi]; jmp[t][i] = {jpi2, ptd+jptd+jptd2}; steps[t][i] = jps*2+1; } else { jmp[t][i] = nxt[t][i]; steps[t][i] = 1; } } } } int query(int t, int from, int to) const { int cur = t; while (from < to) { Pii p = jmp[cur%LCM][from]; if (p.x > to) p = nxt[cur%LCM][from]; from = p.x; cur += p.y; } return cur - t; } }; struct BiJumps { Jumps fwd, bck; int n; BiJumps() {} BiJumps(vector<array<bool, LCM>> pass) { n = sz(pass); fwd = {pass}; reverse(all(pass)); bck = {pass}; } int query(int t, int from, int to) const { if (from == to) return 0; if (from < to) return fwd.query(t, from, to); return bck.query(t, n-from, n-to); } }; int type[LCM]; // 0 = everywhere, 1 = rows, 2 = cols int fastTrip[LCM]; BiJumps jmpRow, jmpCol; int solve(int t, int a, int b, int c, int d) { if (type[t] == 1) { return min(jmpRow.query(t, a, c), fastTrip[t]); } else if (type[t] == 2) { return min(jmpCol.query(t, b, d), fastTrip[t]); } else { return 0; } } int main() { cin.sync_with_stdio(0); cin.tie(0); cout << fixed << setprecision(12); int n, m, q; cin >> n >> m >> q; vector<array<array<bool, 8>, 8>> passRow2(n), passCol2(m); char tmp[16]; rep(i, 0, n) { rep(j, 0, m) { cin >> tmp; int len = int(strlen(tmp)); auto &row = passRow2[i][len-1]; auto &col = passCol2[j][len-1]; rep(k, 0, len) { bool val = (tmp[k] == '1'); (val ? col : row)[k] = 1; } } } vector<array<bool, LCM>> passRow(n), passCol(m); rep(i, 0, n) { auto &row = passRow[i]; rep(len, 1, 9) { auto &row2 = passRow2[i][len-1]; rep(k, 0, LCM) { if (row2[k%len]) row[k] = 1; } } } rep(i, 0, m) { auto &col = passCol[i]; rep(len, 1, 9) { auto &col2 = passCol2[i][len-1]; rep(k, 0, LCM) { if (col2[k%len]) col[k] = 1; } } } rep(k, 0, LCM) { bool hor = 1, ver = 1; rep(i, 0, n) if (!passRow[i][k]) ver = 0; rep(i, 0, m) if (!passCol[i][k]) hor = 0; type[k] = (hor && ver ? 0 : (hor ? 1 : 2)); fastTrip[k] = INF; } rep(i, 0, 2) { for (int k = LCM-1; k >= 0; k--) { if (type[k] == 0) { fastTrip[k] = 0; } else { int nxt = (k+1) % LCM; fastTrip[k] = (type[k] == type[nxt] ? fastTrip[nxt]+1 : 1); } } } jmpRow = {move(passRow)}; jmpCol = {move(passCol)}; while (q--) { int t, a, b, c, d; cin >> t >> a >> b >> c >> d; int ans = solve(t%LCM, a, b, c, d) + t; cout << ans << '\n'; } cout << flush; _Exit(0); } |