#include <bits/stdc++.h> using namespace std; #define fastIO ios_base::sync_with_stdio(0); cin.tie(0) #define dbg(...) "[" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " #define fi first #define se second #define all(x) begin(x), end(x) #define rall(x) rbegin(x), rend(x) #define mt make_tuple #define readRepeatInt int _t; cin >> _t; while (_t--) #define repeat(i, x) for (int i = 0; i < (x); ++i) #define repeatRev(i, x) for (int i = (x) - 1; i >= 0; --i) using ll = long long; using vb = vector<bool>; // using i128 = __int128; using vi128 = vector<i128>; using ull = unsigned long long; using vull = vector<ull>; using vll = vector<ll>; using vvll = vector<vll>; using vi = vector<int>; using vvi = vector<vi>; using pi = pair<int, int>; using vpi = vector<pi>; using pll = pair<ll, ll>; using vpll = vector<pll>; using si = set<int>; using sll = set<ll>; using usi = unordered_set<int>; using usll = unordered_set<ll>; using di = deque<int>; using dll = deque<ll>; using mi = map<int, int>; using mll = map<ll, ll>; using umi = unordered_map<int, int>; using umll = unordered_map<ll, ll>; using vsi = vector<si>; using vsll = vector<sll>; using vusi = vector<usi>; using vusll = vector<usll>; template<typename A, typename B> inline A& maxi(A& a, B b) {return a = max(a, A(b)); } template<typename A, typename B> inline A& mini(A& a, B b) {return a = min(a, A(b)); } template <typename T> struct reversion_wrapper {T& iterable; }; template <typename T> reversion_wrapper<T> reverse(T&& iterable) {return {iterable}; } template <typename T> auto begin(reversion_wrapper<T> w) {return rbegin(w.iterable); } template <typename T> auto end(reversion_wrapper<T> w) {return rend(w.iterable); } template<typename Iter> inline ostream& streamByIterators(ostream &os, Iter beg, Iter end) {os << '{'; if (beg != end) {os << *beg; while (++beg != end) os << ", " << *beg; }; return os << '}'; } template<typename T> inline ostream& operator<<(ostream &os, const vector<T> &v) {return streamByIterators(os, v.begin(), v.end()); } template<typename T> inline ostream& operator<<(ostream &os, const deque<T> &d) {return streamByIterators(os, d.begin(), d.end()); } template<typename T> inline ostream& operator<<(ostream &os, const set<T> &s) {return streamByIterators(os, s.begin(), s.end()); } template<typename T, typename V> inline ostream& operator<<(ostream &os, const map<T, V> &m) {return streamByIterators(os, m.begin(), m.end()); } template<typename T> inline ostream& operator<<(ostream &os, const unordered_set<T> &s) {return streamByIterators(os, s.begin(), s.end()); } template<typename T, typename V> inline ostream& operator<<(ostream &os, const unordered_map<T, V> &m) {return streamByIterators(os, m.begin(), m.end()); } template<typename T, size_t size> inline ostream& operator<<(ostream &os, const array<T, size> &a) {return streamByIterators(os, a.begin(), a.end()); } template<typename A, typename B> inline ostream& operator<<(ostream &os, const pair<A, B> &p) {return os << '(' << p.first << ", " << p.second << ')'; } template<typename T> inline istream& operator>>(istream &is, vector<T> &v) {for (T& t : v) is >> t; return is; } template<typename T> inline istream& operator>>(istream &is, deque<T> &d) {for (T& t : d) is >> t; return is;} template<typename T, size_t size> inline istream& operator>>(istream &is, array<T, size> &a) {for (T& t : a) is >> t; return is; } template<typename A, typename B> inline istream& operator>>(istream &is, pair<A, B> &p) {return is >> p.first >> p.second; } template<typename T, typename V> T constexpr myPow(T base, V exp) {T ans(1); while (exp > 0) {if (exp & 1) ans *= base; base *= base; exp >>= 1; } return ans; } template<typename T, typename V> T constexpr myPowMod(T base, T mod, V exp) {T ans(1); while (exp > 0) {if (exp & 1) ans = (ans * base) % mod; base = (base * base) % mod; exp >>= 1; } return ans; } template<typename T> inline constexpr T myMod(T val, T mod) {val %= mod; if (val < 0) val += mod; return val; } // mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); // uniform_int_distribution<int> dist(1, MX); using psh = pair<short, short>; using vpsh = vector<psh>; int32_t main() { fastIO; int n, m, q; cin >> n >> m >> q; vector<vector<vb>> openN(9), openM(9); for (int i = 2; i <= 8; ++i) { openN[i] = vector<vb>(i, vb(n)); openM[i] = vector<vb>(i, vb(m)); } repeat(in, n) repeat(im, m) { string tmp; cin >> tmp; int sz = (int)tmp.size(); for (int k = 0; k < sz; ++k) { if (tmp[k] == '0') // !! openN[sz][k][in] = true; else openM[sz][k][im] = true; } } vi lift = {1, 2, 4, 8, 24, 120, 840}; repeat(_, 10) lift.push_back(lift.back() * 2); array<vector<vpsh>, 840> dataN, dataM; repeat(i, 840) { dataN[i] = vector<vpsh>(1, vpsh(n+1)); dataM[i] = vector<vpsh>(1, vpsh(m+1)); } auto setupSingle = [](vpsh &vx, vector<vector<vb>> &openx, int i) { for (int k = 0; k < (int)vx.size(); ++k) { int r = k; while (r+1 != (int)vx.size()) { bool skip = false; for (int mm = 2; mm <= 8; ++mm) { if (openx[mm][i%mm][r]) { skip = true; break; } } if (!skip) break; ++r; } for (int t = k; t <= r; ++t) vx[t] = {k, r}; k = r; } }; auto setup = [&setupSingle, &lift](array<vector<vpsh>, 840> &datax, vector<vector<vb>> &openx) { int nn = (int)datax[0][0].size(); repeat(i, 840) setupSingle(datax[i][0], openx, i); for (int i = 1; i < (int)lift.size(); ++i) { int thisLift = lift[i], befLift = lift[i-1], ratio = thisLift/befLift; befLift %= 840; for (int k = 0; k < 840; k += lift[i]) { datax[k].push_back(vpsh(nn)); repeat(l, nn) { int ml = l, mr = l; repeat(s, ratio) { ml = datax[k+s*befLift][i-1][ml].fi; mr = datax[k+s*befLift][i-1][mr].se; } datax[k][i][l] = {ml, mr}; } } } }; setup(dataN, openN); setup(dataM, openM); openN = vector<vector<vb>>(); openM = vector<vector<vb>>(); vb fullN(840), fullM(840); repeat(i, 840) { fullN[i] = (dataN[i][0].back().fi == 0); fullM[i] = (dataM[i][0].back().fi == 0); } auto minDist = [&lift](array<vector<vpsh>, 840> &datax, short x, short y, int t) { psh access = datax[t][0][x]; int ret = 0; if (access.fi <= y && y <= access.se) return 0; while (y < access.fi || access.se < y) { int i = int(datax[t].size())-1; for (; i >= 0; --i) if (y < datax[t][i][access.fi].fi || datax[t][i][access.se].se < y) break; if (i < 0) i = 0; // !! access = {datax[t][i][access.fi].fi, datax[t][i][access.se].se}; t = (t + lift[i]) % 840; ret += lift[i]; } return ret-1; }; repeat(_, q) { int t, ax, ay, bx, by; cin >> t >> ax >> ay >> bx >> by; if (fullN[t%840]) cout << minDist(dataM, (short)ay, (short)by, t%840)+t << '\n'; else cout << minDist(dataN, (short)ax, (short)bx, t%840)+t << '\n'; } return 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 206 207 208 209 210 211 212 213 214 215 216 217 | #include <bits/stdc++.h> using namespace std; #define fastIO ios_base::sync_with_stdio(0); cin.tie(0) #define dbg(...) "[" << #__VA_ARGS__ ": " << (__VA_ARGS__) << "] " #define fi first #define se second #define all(x) begin(x), end(x) #define rall(x) rbegin(x), rend(x) #define mt make_tuple #define readRepeatInt int _t; cin >> _t; while (_t--) #define repeat(i, x) for (int i = 0; i < (x); ++i) #define repeatRev(i, x) for (int i = (x) - 1; i >= 0; --i) using ll = long long; using vb = vector<bool>; // using i128 = __int128; using vi128 = vector<i128>; using ull = unsigned long long; using vull = vector<ull>; using vll = vector<ll>; using vvll = vector<vll>; using vi = vector<int>; using vvi = vector<vi>; using pi = pair<int, int>; using vpi = vector<pi>; using pll = pair<ll, ll>; using vpll = vector<pll>; using si = set<int>; using sll = set<ll>; using usi = unordered_set<int>; using usll = unordered_set<ll>; using di = deque<int>; using dll = deque<ll>; using mi = map<int, int>; using mll = map<ll, ll>; using umi = unordered_map<int, int>; using umll = unordered_map<ll, ll>; using vsi = vector<si>; using vsll = vector<sll>; using vusi = vector<usi>; using vusll = vector<usll>; template<typename A, typename B> inline A& maxi(A& a, B b) {return a = max(a, A(b)); } template<typename A, typename B> inline A& mini(A& a, B b) {return a = min(a, A(b)); } template <typename T> struct reversion_wrapper {T& iterable; }; template <typename T> reversion_wrapper<T> reverse(T&& iterable) {return {iterable}; } template <typename T> auto begin(reversion_wrapper<T> w) {return rbegin(w.iterable); } template <typename T> auto end(reversion_wrapper<T> w) {return rend(w.iterable); } template<typename Iter> inline ostream& streamByIterators(ostream &os, Iter beg, Iter end) {os << '{'; if (beg != end) {os << *beg; while (++beg != end) os << ", " << *beg; }; return os << '}'; } template<typename T> inline ostream& operator<<(ostream &os, const vector<T> &v) {return streamByIterators(os, v.begin(), v.end()); } template<typename T> inline ostream& operator<<(ostream &os, const deque<T> &d) {return streamByIterators(os, d.begin(), d.end()); } template<typename T> inline ostream& operator<<(ostream &os, const set<T> &s) {return streamByIterators(os, s.begin(), s.end()); } template<typename T, typename V> inline ostream& operator<<(ostream &os, const map<T, V> &m) {return streamByIterators(os, m.begin(), m.end()); } template<typename T> inline ostream& operator<<(ostream &os, const unordered_set<T> &s) {return streamByIterators(os, s.begin(), s.end()); } template<typename T, typename V> inline ostream& operator<<(ostream &os, const unordered_map<T, V> &m) {return streamByIterators(os, m.begin(), m.end()); } template<typename T, size_t size> inline ostream& operator<<(ostream &os, const array<T, size> &a) {return streamByIterators(os, a.begin(), a.end()); } template<typename A, typename B> inline ostream& operator<<(ostream &os, const pair<A, B> &p) {return os << '(' << p.first << ", " << p.second << ')'; } template<typename T> inline istream& operator>>(istream &is, vector<T> &v) {for (T& t : v) is >> t; return is; } template<typename T> inline istream& operator>>(istream &is, deque<T> &d) {for (T& t : d) is >> t; return is;} template<typename T, size_t size> inline istream& operator>>(istream &is, array<T, size> &a) {for (T& t : a) is >> t; return is; } template<typename A, typename B> inline istream& operator>>(istream &is, pair<A, B> &p) {return is >> p.first >> p.second; } template<typename T, typename V> T constexpr myPow(T base, V exp) {T ans(1); while (exp > 0) {if (exp & 1) ans *= base; base *= base; exp >>= 1; } return ans; } template<typename T, typename V> T constexpr myPowMod(T base, T mod, V exp) {T ans(1); while (exp > 0) {if (exp & 1) ans = (ans * base) % mod; base = (base * base) % mod; exp >>= 1; } return ans; } template<typename T> inline constexpr T myMod(T val, T mod) {val %= mod; if (val < 0) val += mod; return val; } // mt19937_64 rng(chrono::high_resolution_clock::now().time_since_epoch().count()); // uniform_int_distribution<int> dist(1, MX); using psh = pair<short, short>; using vpsh = vector<psh>; int32_t main() { fastIO; int n, m, q; cin >> n >> m >> q; vector<vector<vb>> openN(9), openM(9); for (int i = 2; i <= 8; ++i) { openN[i] = vector<vb>(i, vb(n)); openM[i] = vector<vb>(i, vb(m)); } repeat(in, n) repeat(im, m) { string tmp; cin >> tmp; int sz = (int)tmp.size(); for (int k = 0; k < sz; ++k) { if (tmp[k] == '0') // !! openN[sz][k][in] = true; else openM[sz][k][im] = true; } } vi lift = {1, 2, 4, 8, 24, 120, 840}; repeat(_, 10) lift.push_back(lift.back() * 2); array<vector<vpsh>, 840> dataN, dataM; repeat(i, 840) { dataN[i] = vector<vpsh>(1, vpsh(n+1)); dataM[i] = vector<vpsh>(1, vpsh(m+1)); } auto setupSingle = [](vpsh &vx, vector<vector<vb>> &openx, int i) { for (int k = 0; k < (int)vx.size(); ++k) { int r = k; while (r+1 != (int)vx.size()) { bool skip = false; for (int mm = 2; mm <= 8; ++mm) { if (openx[mm][i%mm][r]) { skip = true; break; } } if (!skip) break; ++r; } for (int t = k; t <= r; ++t) vx[t] = {k, r}; k = r; } }; auto setup = [&setupSingle, &lift](array<vector<vpsh>, 840> &datax, vector<vector<vb>> &openx) { int nn = (int)datax[0][0].size(); repeat(i, 840) setupSingle(datax[i][0], openx, i); for (int i = 1; i < (int)lift.size(); ++i) { int thisLift = lift[i], befLift = lift[i-1], ratio = thisLift/befLift; befLift %= 840; for (int k = 0; k < 840; k += lift[i]) { datax[k].push_back(vpsh(nn)); repeat(l, nn) { int ml = l, mr = l; repeat(s, ratio) { ml = datax[k+s*befLift][i-1][ml].fi; mr = datax[k+s*befLift][i-1][mr].se; } datax[k][i][l] = {ml, mr}; } } } }; setup(dataN, openN); setup(dataM, openM); openN = vector<vector<vb>>(); openM = vector<vector<vb>>(); vb fullN(840), fullM(840); repeat(i, 840) { fullN[i] = (dataN[i][0].back().fi == 0); fullM[i] = (dataM[i][0].back().fi == 0); } auto minDist = [&lift](array<vector<vpsh>, 840> &datax, short x, short y, int t) { psh access = datax[t][0][x]; int ret = 0; if (access.fi <= y && y <= access.se) return 0; while (y < access.fi || access.se < y) { int i = int(datax[t].size())-1; for (; i >= 0; --i) if (y < datax[t][i][access.fi].fi || datax[t][i][access.se].se < y) break; if (i < 0) i = 0; // !! access = {datax[t][i][access.fi].fi, datax[t][i][access.se].se}; t = (t + lift[i]) % 840; ret += lift[i]; } return ret-1; }; repeat(_, q) { int t, ax, ay, bx, by; cin >> t >> ax >> ay >> bx >> by; if (fullN[t%840]) cout << minDist(dataM, (short)ay, (short)by, t%840)+t << '\n'; else cout << minDist(dataN, (short)ax, (short)bx, t%840)+t << '\n'; } return 0; } |