#include<bits/stdc++.h> using namespace std; #ifdef LOCAL template <class T, class U> auto &operator<<(ostream &out, pair<T, U> a) { return out << "(" << a.first << ", " << a.second << ")"; } template <class T, class = class enable_if<!is_same<T, string>(), class T::iterator>::type> auto &operator<<(ostream &out, T a) { out << "{"; bool fi = 1; for(auto b : a) { if(fi) {out<<b; fi=0;} else out<<", "<<b; } return out << "}"; } template <class T, class X, class Y> auto &operator<<(ostream &out, const priority_queue<T,X,Y>& a) {auto b = a; vector<T> v; while(!b.empty()) {v.push_back(b.top()); b.pop();} return out<<v; } void dump(){ cerr << "\e[39m"<<"\n"; } template <class T, class... Ts> void dump(T a, Ts... x) { cerr << a << ", "; dump(x...); } #define debug(...) cerr << "\e[91m"<<__func__<<":"<<__LINE__<<"\t"<<"[" #__VA_ARGS__ "]: ", dump(__VA_ARGS__) #else #define debug(...) ; #endif template<class C> C reversed(C c) {reverse(c.begin(),c.end()); return c;} #define rep(i, n) for(int i=0;i<(int)(n);i++) #define all(X) (X).begin(), (X).end() #define mp make_pair #define st first #define nd second typedef long long ll; typedef pair<int,int> pii; const int MAXW = 840; const int MAXN = 15004; typedef bitset<MAXW> BS; typedef vector<vector<int> > vvi; BS pre(int n, const vvi& bn, vector<BS>& out_stripes1) { BS hasHstripe; out_stripes1.clear(); out_stripes1.resize(n); for(int i=0;i<n;i++) { BS bb; for(int j=2;j<=8;j++) { BS b(bn[i][j]); for(int len = j; len < MAXW; len*=2) b = (b<<len)|b; bb |= b; } out_stripes1[i] = bb.flip(); hasHstripe |= out_stripes1[i]; } return hasHstripe; } vector<pair<int,int> > qss [MAXN][MAXW]; void solve(const vector<pair<pii,pii> >& qs, vector<BS> stripes, vector<int>& ans) { int n = stripes.size(); for(int i=0;i<=n;i++) for(int j=0;j<MAXW;j++) { qss[i][j].clear(); qss[i][j].shrink_to_fit(); } for(const auto& a:qs) qss[a.st.st][a.st.nd].push_back(a.nd); vector<int> scores(n+1); int i,j,s; function<void()> dfs = [&]() { int scoreSwap = scores[i]; scores[i] = s; for(const auto& a:qss[i][j]) { ans[a.st] = min(ans[a.st],s-scores[a.nd]); assert(ans[a.st] >= 0); } if(i > 0 && stripes[i-1][j] == 0) { i--; dfs(); i++; } if(i != n && stripes[i][j == 0 ? MAXW-1 : j-1] == 1) { j--; if(j < 0) j += MAXW; s++; dfs(); s--; j++; if(j == MAXW) j = 0; } scores[i] = scoreSwap; }; for(int x=0;x<MAXW;x++) { i = n; j = x; s = 0; dfs(); } } int32_t main(){ ios::sync_with_stdio(false); cin.tie(0); int n,m,q; cin >> n >> m >> q; vector<vector<int> > bn(n,vector<int>(9)); vector<vector<int> > bm(m,vector<int>(9)); for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { string s; cin >> s; reverse(s.begin(),s.end()); bn[i][s.size()] |= (((1<<s.size())-1)^bitset<8>(s).to_ulong()); bm[j][s.size()] |= bitset<8>(s).to_ulong(); } } vector<BS> stripes1, stripes2; BS p1 = pre(n, bn, stripes1); BS p2 = pre(m, bm, stripes2); vector<int> nxtH(MAXW, 1e9), nxtV(MAXW, 1e9); for(int i=0;i<MAXW;i++) { for(int j=i, cnt=0; cnt<MAXW;cnt++, j = (j+1)%MAXW) { if(p1[j] == 0) nxtH[i] = min(nxtH[i],cnt); if(p2[j] == 0) nxtV[i] = min(nxtV[i],cnt); } } vector<pair<pii,pii> > qsjD, qsjU, qsiD, qsiU; vector<int> base(q), ans(q); for(int i=0;i<q;i++) { int t,a,b,c,d; cin >> t >> a >> b >> c >> d; base[i] = t; int t2 = t%MAXW; if(nxtV[t2] == 0 && nxtH[t2] == 0) continue; if(nxtH[t2] == 0) { if(b == d) continue; ans[i] = nxtV[t2]; if(b < d) qsjD.push_back({{b,t2},{i,d}}); else qsjU.push_back({{m-b,t2},{i,m-d}}); } else { if(a == c) continue; assert(nxtV[t2] == 0); ans[i] = nxtH[t2]; if(a <= c) qsiD.push_back({{a,t2},{i,c}}); else qsiU.push_back({{n-a,t2},{i,n-c}}); } } solve(qsjD, stripes2, ans); reverse(all(stripes2)); solve(qsjU, stripes2, ans); solve(qsiD, stripes1, ans); reverse(all(stripes1)); solve(qsiU, stripes1, ans); for(int i=0;i<q;i++) cout<<base[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 | #include<bits/stdc++.h> using namespace std; #ifdef LOCAL template <class T, class U> auto &operator<<(ostream &out, pair<T, U> a) { return out << "(" << a.first << ", " << a.second << ")"; } template <class T, class = class enable_if<!is_same<T, string>(), class T::iterator>::type> auto &operator<<(ostream &out, T a) { out << "{"; bool fi = 1; for(auto b : a) { if(fi) {out<<b; fi=0;} else out<<", "<<b; } return out << "}"; } template <class T, class X, class Y> auto &operator<<(ostream &out, const priority_queue<T,X,Y>& a) {auto b = a; vector<T> v; while(!b.empty()) {v.push_back(b.top()); b.pop();} return out<<v; } void dump(){ cerr << "\e[39m"<<"\n"; } template <class T, class... Ts> void dump(T a, Ts... x) { cerr << a << ", "; dump(x...); } #define debug(...) cerr << "\e[91m"<<__func__<<":"<<__LINE__<<"\t"<<"[" #__VA_ARGS__ "]: ", dump(__VA_ARGS__) #else #define debug(...) ; #endif template<class C> C reversed(C c) {reverse(c.begin(),c.end()); return c;} #define rep(i, n) for(int i=0;i<(int)(n);i++) #define all(X) (X).begin(), (X).end() #define mp make_pair #define st first #define nd second typedef long long ll; typedef pair<int,int> pii; const int MAXW = 840; const int MAXN = 15004; typedef bitset<MAXW> BS; typedef vector<vector<int> > vvi; BS pre(int n, const vvi& bn, vector<BS>& out_stripes1) { BS hasHstripe; out_stripes1.clear(); out_stripes1.resize(n); for(int i=0;i<n;i++) { BS bb; for(int j=2;j<=8;j++) { BS b(bn[i][j]); for(int len = j; len < MAXW; len*=2) b = (b<<len)|b; bb |= b; } out_stripes1[i] = bb.flip(); hasHstripe |= out_stripes1[i]; } return hasHstripe; } vector<pair<int,int> > qss [MAXN][MAXW]; void solve(const vector<pair<pii,pii> >& qs, vector<BS> stripes, vector<int>& ans) { int n = stripes.size(); for(int i=0;i<=n;i++) for(int j=0;j<MAXW;j++) { qss[i][j].clear(); qss[i][j].shrink_to_fit(); } for(const auto& a:qs) qss[a.st.st][a.st.nd].push_back(a.nd); vector<int> scores(n+1); int i,j,s; function<void()> dfs = [&]() { int scoreSwap = scores[i]; scores[i] = s; for(const auto& a:qss[i][j]) { ans[a.st] = min(ans[a.st],s-scores[a.nd]); assert(ans[a.st] >= 0); } if(i > 0 && stripes[i-1][j] == 0) { i--; dfs(); i++; } if(i != n && stripes[i][j == 0 ? MAXW-1 : j-1] == 1) { j--; if(j < 0) j += MAXW; s++; dfs(); s--; j++; if(j == MAXW) j = 0; } scores[i] = scoreSwap; }; for(int x=0;x<MAXW;x++) { i = n; j = x; s = 0; dfs(); } } int32_t main(){ ios::sync_with_stdio(false); cin.tie(0); int n,m,q; cin >> n >> m >> q; vector<vector<int> > bn(n,vector<int>(9)); vector<vector<int> > bm(m,vector<int>(9)); for(int i=0;i<n;i++) { for(int j=0;j<m;j++) { string s; cin >> s; reverse(s.begin(),s.end()); bn[i][s.size()] |= (((1<<s.size())-1)^bitset<8>(s).to_ulong()); bm[j][s.size()] |= bitset<8>(s).to_ulong(); } } vector<BS> stripes1, stripes2; BS p1 = pre(n, bn, stripes1); BS p2 = pre(m, bm, stripes2); vector<int> nxtH(MAXW, 1e9), nxtV(MAXW, 1e9); for(int i=0;i<MAXW;i++) { for(int j=i, cnt=0; cnt<MAXW;cnt++, j = (j+1)%MAXW) { if(p1[j] == 0) nxtH[i] = min(nxtH[i],cnt); if(p2[j] == 0) nxtV[i] = min(nxtV[i],cnt); } } vector<pair<pii,pii> > qsjD, qsjU, qsiD, qsiU; vector<int> base(q), ans(q); for(int i=0;i<q;i++) { int t,a,b,c,d; cin >> t >> a >> b >> c >> d; base[i] = t; int t2 = t%MAXW; if(nxtV[t2] == 0 && nxtH[t2] == 0) continue; if(nxtH[t2] == 0) { if(b == d) continue; ans[i] = nxtV[t2]; if(b < d) qsjD.push_back({{b,t2},{i,d}}); else qsjU.push_back({{m-b,t2},{i,m-d}}); } else { if(a == c) continue; assert(nxtV[t2] == 0); ans[i] = nxtH[t2]; if(a <= c) qsiD.push_back({{a,t2},{i,c}}); else qsiU.push_back({{n-a,t2},{i,n-c}}); } } solve(qsjD, stripes2, ans); reverse(all(stripes2)); solve(qsjU, stripes2, ans); solve(qsiD, stripes1, ans); reverse(all(stripes1)); solve(qsiU, stripes1, ans); for(int i=0;i<q;i++) cout<<base[i]+ans[i]<<"\n"; } |