#include <bits/stdc++.h> #include <math.h> using namespace std; #define endl "\n" #define mp make_pair #define st first #define nd second #define pii pair<int, int> #define pb push_back #define _upgrade ios_base::sync_with_stdio(0), cout.setf(ios::fixed), cout.precision(10), cin.tie(0), cout.tie(0); #define rep(i, n) for (int i = 0; i < (n); ++i) #define fwd(i, a, b) for (int i = (a); i < (b); ++i) #define trav(a, x) for (auto &a : x) #define all(c) (c).begin(), (c).end() #define sz(X) (int)((X).size()) typedef long double ld; typedef unsigned long long ull; typedef long long ll; #ifdef LOCAL ostream &operator<<(ostream &out, string str) { for (char c : str) out << c; return out; } template <class L, class R> ostream &operator<<(ostream &out, pair<L, R> p) { return out << "(" << p.st << ", " << p.nd << ")"; } template <class L, class R, class S> ostream &operator<<(ostream &out, tuple<L, R, S> p) { auto &[a, b, c] = p; return out << "(" << a << ", " << b << ", " << c << ")"; } template <class T> auto operator<<(ostream &out, T a) -> decltype(a.begin(), out) { out << '{'; for (auto it = a.begin(); it != a.end(); it = next(it)) out << (it != a.begin() ? ", " : "") << *it; return out << '}'; } void dump() { cerr << "\n"; } template <class T, class... Ts> void dump(T a, Ts... x) { cerr << a << ", "; dump(x...); } #define debug(...) cerr << "[" #__VA_ARGS__ "]: ", dump(__VA_ARGS__) #else #define debug(...) 42 #endif typedef tuple<int, int, int, int, int> tpl; const int LCM = 8 * 7 * 5 * 3; typedef array<int, LCM> Arr; const Arr emp = {}; const int inf = 2e9; int n, m, q; vector<int> get(string S, char t) { int l = sz(S); S += S; vector<int> T(l); rep(i, l) T[i] = find(S.begin() + i, S.end(), t) - (S.begin() + i); return T; } void min_conv(Arr &A, const vector<int> &X) { int l = sz(X); rep(i, A.size()) A[i] = min(A[i], X[i % l]); } void min_conv(vector<int> &A, const vector<int> &X) { rep(i, A.size()) A[i] = min(A[i], X[i]); } const int MAXS = 8; vector<vector<int>> get_emp() { vector<vector<int>> res(MAXS + 1); rep(i, MAXS + 1) res[i].resize(i, inf); return res; } void min_conv(Arr &A, const Arr &B) { rep(i, LCM) A[i] = min(A[i], B[i]); } int plus_conv(int t, const Arr &B) { return t + B[t % LCM]; } void plus_conv(Arr &C, const Arr &A, const Arr &B) { rep(i, LCM) C[i] = A[i] + B[(i + A[i]) % LCM]; } Arr get(vector<vector<int>> &V) { Arr A; A.fill(inf); for (int i = 2; i <= MAXS; i++) min_conv(A, V[i]); return A; } #define r1 ((l + r) / 2) // ulatwia branie srodka przedzialu #define l2 (r1 + 1) struct SegmentTree { // drzewo przedzial-przedzial (add,sum) vector<Arr> C; int size; SegmentTree(int x, vector<Arr> &A) { size = (1 << (32 - __builtin_clz(x))), C.resize(size * 2, emp); rep(i, sz(A)) C[i + size] = A[i]; for (int i = size - 1; i > 0; i--) plus_conv(C[i], C[2 * i], C[2 * i + 1]); } // drzewo przedzialowe dla zakresu [0,x), int query(int p, int q, int time, int l, int r, int wezel = 1) { // suma na przedzial [p,q] if (p > q) return time; if (p == l && q == r) return plus_conv(time, C[wezel]); time = query(p, min(r1, q), time, l, r1, 2 * wezel); time = query(max(l2, p), q, time, l2, r, 2 * wezel + 1); return time; } int query(int p, int q, int time) { return query(p, q, time, 0, size - 1); } }; int get(int a, int b, int time, SegmentTree &P, SegmentTree &R, int n) { if (a < b) return P.query(a, b - 1, time); else { a = n - a; b = n - b; return R.query(a, b - 1, time); } } vector<int> solve(const vector<vector<string>> &T, const vector<tpl> &Q) { vector<Arr> N, M; rep(i, n) { auto P = get_emp(); rep(j, m) { auto t = get(T[i][j], '0'); min_conv(P[sz(t)], t); } N.push_back(get(P)); } rep(j, m) { auto P = get_emp(); rep(i, n) { auto t = get(T[i][j], '1'); min_conv(P[sz(t)], t); } M.push_back(get(P)); } // *1 zmiany po 1 wspolrzednej, *2 to zmiany po 2 SegmentTree P1(n, N), P2(m, M); reverse(all(N)); reverse(all(M)); SegmentTree R1(n, N), R2(m, M); vector<int> res; for (auto [t, a, b, c, d] : Q) { int t1 = get(a, c, t, P1, R1, n); int t2 = get(b, d, t, P2, R2, m); int d1 = t1 - t; int d2 = t2 - t; res.push_back(max(t1, t2)); } return res; } int32_t main() { _upgrade; cin >> n >> m >> q; vector<vector<string>> T(n, vector<string>(m)); rep(i, n) rep(j, m) cin >> T[i][j]; vector<tpl> Q(q); rep(i, q) { int a, b, c, d, t; cin >> t >> a >> b >> c >> d; Q[i] = {t, a, b, c, d}; } auto R = solve(T, Q); for (auto r : R) cout << r << endl; }
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 | #include <bits/stdc++.h> #include <math.h> using namespace std; #define endl "\n" #define mp make_pair #define st first #define nd second #define pii pair<int, int> #define pb push_back #define _upgrade ios_base::sync_with_stdio(0), cout.setf(ios::fixed), cout.precision(10), cin.tie(0), cout.tie(0); #define rep(i, n) for (int i = 0; i < (n); ++i) #define fwd(i, a, b) for (int i = (a); i < (b); ++i) #define trav(a, x) for (auto &a : x) #define all(c) (c).begin(), (c).end() #define sz(X) (int)((X).size()) typedef long double ld; typedef unsigned long long ull; typedef long long ll; #ifdef LOCAL ostream &operator<<(ostream &out, string str) { for (char c : str) out << c; return out; } template <class L, class R> ostream &operator<<(ostream &out, pair<L, R> p) { return out << "(" << p.st << ", " << p.nd << ")"; } template <class L, class R, class S> ostream &operator<<(ostream &out, tuple<L, R, S> p) { auto &[a, b, c] = p; return out << "(" << a << ", " << b << ", " << c << ")"; } template <class T> auto operator<<(ostream &out, T a) -> decltype(a.begin(), out) { out << '{'; for (auto it = a.begin(); it != a.end(); it = next(it)) out << (it != a.begin() ? ", " : "") << *it; return out << '}'; } void dump() { cerr << "\n"; } template <class T, class... Ts> void dump(T a, Ts... x) { cerr << a << ", "; dump(x...); } #define debug(...) cerr << "[" #__VA_ARGS__ "]: ", dump(__VA_ARGS__) #else #define debug(...) 42 #endif typedef tuple<int, int, int, int, int> tpl; const int LCM = 8 * 7 * 5 * 3; typedef array<int, LCM> Arr; const Arr emp = {}; const int inf = 2e9; int n, m, q; vector<int> get(string S, char t) { int l = sz(S); S += S; vector<int> T(l); rep(i, l) T[i] = find(S.begin() + i, S.end(), t) - (S.begin() + i); return T; } void min_conv(Arr &A, const vector<int> &X) { int l = sz(X); rep(i, A.size()) A[i] = min(A[i], X[i % l]); } void min_conv(vector<int> &A, const vector<int> &X) { rep(i, A.size()) A[i] = min(A[i], X[i]); } const int MAXS = 8; vector<vector<int>> get_emp() { vector<vector<int>> res(MAXS + 1); rep(i, MAXS + 1) res[i].resize(i, inf); return res; } void min_conv(Arr &A, const Arr &B) { rep(i, LCM) A[i] = min(A[i], B[i]); } int plus_conv(int t, const Arr &B) { return t + B[t % LCM]; } void plus_conv(Arr &C, const Arr &A, const Arr &B) { rep(i, LCM) C[i] = A[i] + B[(i + A[i]) % LCM]; } Arr get(vector<vector<int>> &V) { Arr A; A.fill(inf); for (int i = 2; i <= MAXS; i++) min_conv(A, V[i]); return A; } #define r1 ((l + r) / 2) // ulatwia branie srodka przedzialu #define l2 (r1 + 1) struct SegmentTree { // drzewo przedzial-przedzial (add,sum) vector<Arr> C; int size; SegmentTree(int x, vector<Arr> &A) { size = (1 << (32 - __builtin_clz(x))), C.resize(size * 2, emp); rep(i, sz(A)) C[i + size] = A[i]; for (int i = size - 1; i > 0; i--) plus_conv(C[i], C[2 * i], C[2 * i + 1]); } // drzewo przedzialowe dla zakresu [0,x), int query(int p, int q, int time, int l, int r, int wezel = 1) { // suma na przedzial [p,q] if (p > q) return time; if (p == l && q == r) return plus_conv(time, C[wezel]); time = query(p, min(r1, q), time, l, r1, 2 * wezel); time = query(max(l2, p), q, time, l2, r, 2 * wezel + 1); return time; } int query(int p, int q, int time) { return query(p, q, time, 0, size - 1); } }; int get(int a, int b, int time, SegmentTree &P, SegmentTree &R, int n) { if (a < b) return P.query(a, b - 1, time); else { a = n - a; b = n - b; return R.query(a, b - 1, time); } } vector<int> solve(const vector<vector<string>> &T, const vector<tpl> &Q) { vector<Arr> N, M; rep(i, n) { auto P = get_emp(); rep(j, m) { auto t = get(T[i][j], '0'); min_conv(P[sz(t)], t); } N.push_back(get(P)); } rep(j, m) { auto P = get_emp(); rep(i, n) { auto t = get(T[i][j], '1'); min_conv(P[sz(t)], t); } M.push_back(get(P)); } // *1 zmiany po 1 wspolrzednej, *2 to zmiany po 2 SegmentTree P1(n, N), P2(m, M); reverse(all(N)); reverse(all(M)); SegmentTree R1(n, N), R2(m, M); vector<int> res; for (auto [t, a, b, c, d] : Q) { int t1 = get(a, c, t, P1, R1, n); int t2 = get(b, d, t, P2, R2, m); int d1 = t1 - t; int d2 = t2 - t; res.push_back(max(t1, t2)); } return res; } int32_t main() { _upgrade; cin >> n >> m >> q; vector<vector<string>> T(n, vector<string>(m)); rep(i, n) rep(j, m) cin >> T[i][j]; vector<tpl> Q(q); rep(i, q) { int a, b, c, d, t; cin >> t >> a >> b >> c >> d; Q[i] = {t, a, b, c, d}; } auto R = solve(T, Q); for (auto r : R) cout << r << endl; } |