/* JAK WYGLADA PLANSZA? * * * =========================== * | | | | * | | | | * | | | | * | | | | * =========================== * * * zawsze przedzielona na pionowe albo poziome pasy * nigdy nie ma blokady krzyzowej * * obmotka na torusie */ #include <iostream> #include <algorithm> #include <string> #include <vector> #define REP(i,n) for(int i=0;i<(n);++i) using namespace std; const int MAXN = 15005; const int NWW = 840; const int ZAK = 7; const int MAXQ = 1000005; int n, m, q, poziome[MAXN][ZAK], pionowe[MAXN][ZAK]; bool poz[MAXN][NWW], odw[MAXN][NWW]; vector<int> query[MAXN][NWW]; int t[MAXQ], a[MAXQ], b[MAXQ], c[MAXQ], d[MAXQ], ans[MAXQ]; int dtime[MAXN]; int *arr; //int debug; int zak; void dfs_lewo(int cur, int res, int time) { dtime[cur] = time; odw[cur][res] = 1; for (int qn: query[cur][res]) { //if (debug) cerr << "XD "<<qn << endl; ans[qn] = max(ans[qn], time - dtime[arr[qn]]); } if (cur > 0 && poz[cur][res] && !odw[cur-1][res]) dfs_lewo(cur-1, res, time); int prev = res-1; if (prev < 0) prev += NWW; if (cur < zak && !poz[cur+1][prev] && !odw[cur][prev]) dfs_lewo(cur, prev, time + 1); } void dfs_prawo(int cur, int res, int time) { /*if (debug && res < 20) { cerr << cur << ' ' << res << ' ' << time << ' ' << (int)poz[cur][res] << ' ' << odw[cur][res] <<endl; }*/ dtime[cur] = time; odw[cur][res] = 1; for (int qn: query[cur][res]) { //if (debug)cerr << "XD "<<qn << endl; ans[qn] = max(ans[qn], time - dtime[arr[qn]]); } if (cur < zak && poz[cur][res] && !odw[cur+1][res]) dfs_prawo(cur+1, res, time); int prev = res - 1; if (prev < 0) prev += NWW; if (cur > 0 && !poz[cur-1][prev] && !odw[cur][prev]) dfs_prawo(cur, prev, time+1); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> m >> q; for (int i = 1; i <= max(n, m); i++) { for (int j = 0; j < ZAK; j++) { poziome[i][j] = (1 << (j + 2)) - 1; pionowe[i][j] = (1 << (j + 2)) - 1; } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { string s; cin >> s; int x = 0, len = s.length(); for (int k = len - 1; k >= 0; k--) { x = (x << 1) + s[k] - '0'; } poziome[i][len - 2] &= x; pionowe[j][len - 2] &= ~x; } } REP(i, q) cin >> t[i] >> a[i] >> b[i] >> c[i] >> d[i]; zak = n; REP(i, q) if (c[i] > a[i]) query[a[i]][t[i] % NWW].push_back(i); for (int i = 1; i <= n; i++) { fill(poz[i], poz[i] + NWW, true); for (int j = 0; j < ZAK; j++) { for (int k = 0, l = 0; k < NWW; k++, l++) { if (l == j + 2) l = 0; poz[i][k] &= (poziome[i][j] & (1 << l)) >> l; } } REP(j, NWW) poz[i][j] = !poz[i][j]; } arr = c; REP(i, NWW) dfs_lewo(n, i, 0); REP(i,q) if (c[i] > a[i]) query[a[i]][t[i] % NWW].clear(); REP(i,q) if (a[i] > c[i]) query[a[i]][t[i] % NWW].push_back(i); REP(i, n+1) REP(j, NWW) odw[i][j]=0; REP(i,n) REP(j,NWW) poz[i][j] = poz[i+1][j]; REP(i, NWW) dfs_prawo(0, i, 0); arr = d; zak = m; REP(i,q) if (a[i] > c[i]) query[a[i]][t[i] % NWW].clear(); REP(i,q) if (d[i] > b[i]) query[b[i]][t[i] % NWW].push_back(i); REP(i, n+1) REP(j, NWW) odw[i][j]=0; for (int i = 1; i <= m; i++) { fill(poz[i], poz[i] + NWW, true); for (int j = 0; j < ZAK; j++) { for (int k = 0, l = 0; k < NWW; k++, l++) { if (l == j+2) l=0; poz[i][k] &= (pionowe[i][j] & (1 << l)) >> l; } } REP(j, NWW) poz[i][j] = !poz[i][j];//(w[j] == 0); } REP(i,NWW) dfs_lewo(m, i, 0); REP(i,q) if (d[i] > b[i]) query[b[i]][t[i] % NWW].clear(); REP(i,q) if (b[i] > d[i]) query[b[i]][t[i] % NWW].push_back(i); //cerr<<"query " << b[i] << ' ' << t[i]%NWW << ' '<< i<<endl;}} REP(i,m+1) REP(j,NWW) odw[i][j]=0; REP(i,m) REP(j, NWW) poz[i][j] = poz[i+1][j]; REP(i,NWW) dfs_prawo(0, i, 0); REP(i,q) cout << ans[i] + t[i] <<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 | /* JAK WYGLADA PLANSZA? * * * =========================== * | | | | * | | | | * | | | | * | | | | * =========================== * * * zawsze przedzielona na pionowe albo poziome pasy * nigdy nie ma blokady krzyzowej * * obmotka na torusie */ #include <iostream> #include <algorithm> #include <string> #include <vector> #define REP(i,n) for(int i=0;i<(n);++i) using namespace std; const int MAXN = 15005; const int NWW = 840; const int ZAK = 7; const int MAXQ = 1000005; int n, m, q, poziome[MAXN][ZAK], pionowe[MAXN][ZAK]; bool poz[MAXN][NWW], odw[MAXN][NWW]; vector<int> query[MAXN][NWW]; int t[MAXQ], a[MAXQ], b[MAXQ], c[MAXQ], d[MAXQ], ans[MAXQ]; int dtime[MAXN]; int *arr; //int debug; int zak; void dfs_lewo(int cur, int res, int time) { dtime[cur] = time; odw[cur][res] = 1; for (int qn: query[cur][res]) { //if (debug) cerr << "XD "<<qn << endl; ans[qn] = max(ans[qn], time - dtime[arr[qn]]); } if (cur > 0 && poz[cur][res] && !odw[cur-1][res]) dfs_lewo(cur-1, res, time); int prev = res-1; if (prev < 0) prev += NWW; if (cur < zak && !poz[cur+1][prev] && !odw[cur][prev]) dfs_lewo(cur, prev, time + 1); } void dfs_prawo(int cur, int res, int time) { /*if (debug && res < 20) { cerr << cur << ' ' << res << ' ' << time << ' ' << (int)poz[cur][res] << ' ' << odw[cur][res] <<endl; }*/ dtime[cur] = time; odw[cur][res] = 1; for (int qn: query[cur][res]) { //if (debug)cerr << "XD "<<qn << endl; ans[qn] = max(ans[qn], time - dtime[arr[qn]]); } if (cur < zak && poz[cur][res] && !odw[cur+1][res]) dfs_prawo(cur+1, res, time); int prev = res - 1; if (prev < 0) prev += NWW; if (cur > 0 && !poz[cur-1][prev] && !odw[cur][prev]) dfs_prawo(cur, prev, time+1); } int main() { ios_base::sync_with_stdio(0); cin.tie(0); cin >> n >> m >> q; for (int i = 1; i <= max(n, m); i++) { for (int j = 0; j < ZAK; j++) { poziome[i][j] = (1 << (j + 2)) - 1; pionowe[i][j] = (1 << (j + 2)) - 1; } } for (int i = 1; i <= n; i++) { for (int j = 1; j <= m; j++) { string s; cin >> s; int x = 0, len = s.length(); for (int k = len - 1; k >= 0; k--) { x = (x << 1) + s[k] - '0'; } poziome[i][len - 2] &= x; pionowe[j][len - 2] &= ~x; } } REP(i, q) cin >> t[i] >> a[i] >> b[i] >> c[i] >> d[i]; zak = n; REP(i, q) if (c[i] > a[i]) query[a[i]][t[i] % NWW].push_back(i); for (int i = 1; i <= n; i++) { fill(poz[i], poz[i] + NWW, true); for (int j = 0; j < ZAK; j++) { for (int k = 0, l = 0; k < NWW; k++, l++) { if (l == j + 2) l = 0; poz[i][k] &= (poziome[i][j] & (1 << l)) >> l; } } REP(j, NWW) poz[i][j] = !poz[i][j]; } arr = c; REP(i, NWW) dfs_lewo(n, i, 0); REP(i,q) if (c[i] > a[i]) query[a[i]][t[i] % NWW].clear(); REP(i,q) if (a[i] > c[i]) query[a[i]][t[i] % NWW].push_back(i); REP(i, n+1) REP(j, NWW) odw[i][j]=0; REP(i,n) REP(j,NWW) poz[i][j] = poz[i+1][j]; REP(i, NWW) dfs_prawo(0, i, 0); arr = d; zak = m; REP(i,q) if (a[i] > c[i]) query[a[i]][t[i] % NWW].clear(); REP(i,q) if (d[i] > b[i]) query[b[i]][t[i] % NWW].push_back(i); REP(i, n+1) REP(j, NWW) odw[i][j]=0; for (int i = 1; i <= m; i++) { fill(poz[i], poz[i] + NWW, true); for (int j = 0; j < ZAK; j++) { for (int k = 0, l = 0; k < NWW; k++, l++) { if (l == j+2) l=0; poz[i][k] &= (pionowe[i][j] & (1 << l)) >> l; } } REP(j, NWW) poz[i][j] = !poz[i][j];//(w[j] == 0); } REP(i,NWW) dfs_lewo(m, i, 0); REP(i,q) if (d[i] > b[i]) query[b[i]][t[i] % NWW].clear(); REP(i,q) if (b[i] > d[i]) query[b[i]][t[i] % NWW].push_back(i); //cerr<<"query " << b[i] << ' ' << t[i]%NWW << ' '<< i<<endl;}} REP(i,m+1) REP(j,NWW) odw[i][j]=0; REP(i,m) REP(j, NWW) poz[i][j] = poz[i+1][j]; REP(i,NWW) dfs_prawo(0, i, 0); REP(i,q) cout << ans[i] + t[i] <<endl; } |