#include <algorithm> #include <cstdio> #include <string> #include <vector> using namespace std; #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define FORD(i,a,b) for(int i=(b)-1;i>=(a);--i) #define REP(i,n) FOR(i,0,n) #define REPD(i,n) FORD(i,0,n) #define VAR(v,w) __typeof(w) v=(w) #define FORE(it,c) for(VAR(it,(c).begin());it!=(c).end();++it) #define ALL(c) (c).begin(),(c).end() #define SIZE(c) ((int)(c).size()) #define INT(x) int x; scanf("%d", &x) #define STR(n,x) char x[n]; scanf("%s", x) typedef vector<string> VS; const int INF = 2000000000; int q; vector<VS> ss1, ss2; VS s1, s2; int t[1000000], a[1000000], b[1000000], c[1000000], d[1000000], r[1000000]; int g[15002][840]; int h[15002][840][14]; int xxx; int GCD(int a, int b) { return b ? GCD(b, a % b) : a; } int LCM(int a, int b) { return a / GCD(a, b) * b; } string inv(const string& s) { int xx = SIZE(s); string x(xx, ' '); REP(i,xx) x[i] = '1' - s[i] + '0'; return x; } void update(string& x, const string& y) { if (x.empty()) { x = y; return; } int xx = SIZE(x); REP(i,xx) if (y[i] == '0') x[i] = '0'; } void update2(string& x, const string& y) { if (y.empty()) return; if (x.empty()) { x = y; return; } int xx = SIZE(x), yy = SIZE(y); int zz = LCM(xx, yy); x.resize(zz); FOR(i,xx,zz) x[i] = x[i - xx]; REP(i,zz) if (y[i % yy] == '0') x[i] = '0'; } int encode(int i, int j) { return i * 840 + j; } void decode(int e, int& i, int& j) { i = e / 840; j = e % 840; } void prep(VS& s) { int n = SIZE(s); REP(j,840) g[n][j] = n; REPD(i,n) REP(j,840) if (s[i][j % SIZE(s[i])] == '0') g[i][j] = g[i + 1][j]; else g[i][j] = i; REP(j,840) h[n][j][0] = xxx; REPD(i,n) REP(j,840) { if (g[i][j] > i) h[i][j][0] = h[i + 1][j][0]; else { int j1 = (j + 1) % 840; h[i][j][0] = encode(g[i][j1], j1); } } REPD(i,n+1) FOR(k,1,14) REP(j,840) { if (h[i][j][k - 1] == xxx) { h[i][j][k] = xxx; continue; } int i1, j1; decode(h[i][j][k - 1], i1, j1); h[i][j][k] = h[i1][j1][k - 1]; } } int dist(int j, int i, int to) { j %= 840; if (g[i][j] >= to) return 0; int c = 0, r = INF, k = 13; REPD(k,14) { int e = h[i][j][k]; if (e == xxx) { r = min(r, c + (1 << k)); continue; } int i1, j1; decode(e, i1, j1); if (i1 >= to) { r = min(r, c + (1 << k)); continue; } c += 1 << k; i = i1; j = j1; } return r; } void go(VS& s, int from[], int to[]) { int n = SIZE(s); xxx = encode(n + 1, 0); prep(s); REP(qq,q) if (from[qq] < to[qq]) r[qq] = max(r[qq], dist(t[qq], from[qq], to[qq])); reverse(ALL(s)); prep(s); REP(qq,q) if (from[qq] > to[qq]) r[qq] = max(r[qq], dist(t[qq], n - from[qq], n - to[qq])); } int main() { INT(n); INT(m); INT(q1); q = q1; ss1.resize(n); ss2.resize(m); REP(i,n) REP(j,m) { STR(9, ss); string s = ss; ss1[i].resize(9); ss2[j].resize(9); update(ss1[i][SIZE(s)], s); update(ss2[j][SIZE(s)], inv(s)); } REP(qq,q) scanf("%d%d%d%d%d", &t[qq], &a[qq], &b[qq], &c[qq], &d[qq]); s1.resize(n); s2.resize(m); REP(i,n) FORE(it,ss1[i]) update2(s1[i], *it); REP(j,m) FORE(it,ss2[j]) update2(s2[j], *it); go(s1, a, c); go(s2, b, d); REP(qq,q) printf("%d\n", t[qq] + r[qq]); }
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 | #include <algorithm> #include <cstdio> #include <string> #include <vector> using namespace std; #define FOR(i,a,b) for(int i=(a);i<(b);++i) #define FORD(i,a,b) for(int i=(b)-1;i>=(a);--i) #define REP(i,n) FOR(i,0,n) #define REPD(i,n) FORD(i,0,n) #define VAR(v,w) __typeof(w) v=(w) #define FORE(it,c) for(VAR(it,(c).begin());it!=(c).end();++it) #define ALL(c) (c).begin(),(c).end() #define SIZE(c) ((int)(c).size()) #define INT(x) int x; scanf("%d", &x) #define STR(n,x) char x[n]; scanf("%s", x) typedef vector<string> VS; const int INF = 2000000000; int q; vector<VS> ss1, ss2; VS s1, s2; int t[1000000], a[1000000], b[1000000], c[1000000], d[1000000], r[1000000]; int g[15002][840]; int h[15002][840][14]; int xxx; int GCD(int a, int b) { return b ? GCD(b, a % b) : a; } int LCM(int a, int b) { return a / GCD(a, b) * b; } string inv(const string& s) { int xx = SIZE(s); string x(xx, ' '); REP(i,xx) x[i] = '1' - s[i] + '0'; return x; } void update(string& x, const string& y) { if (x.empty()) { x = y; return; } int xx = SIZE(x); REP(i,xx) if (y[i] == '0') x[i] = '0'; } void update2(string& x, const string& y) { if (y.empty()) return; if (x.empty()) { x = y; return; } int xx = SIZE(x), yy = SIZE(y); int zz = LCM(xx, yy); x.resize(zz); FOR(i,xx,zz) x[i] = x[i - xx]; REP(i,zz) if (y[i % yy] == '0') x[i] = '0'; } int encode(int i, int j) { return i * 840 + j; } void decode(int e, int& i, int& j) { i = e / 840; j = e % 840; } void prep(VS& s) { int n = SIZE(s); REP(j,840) g[n][j] = n; REPD(i,n) REP(j,840) if (s[i][j % SIZE(s[i])] == '0') g[i][j] = g[i + 1][j]; else g[i][j] = i; REP(j,840) h[n][j][0] = xxx; REPD(i,n) REP(j,840) { if (g[i][j] > i) h[i][j][0] = h[i + 1][j][0]; else { int j1 = (j + 1) % 840; h[i][j][0] = encode(g[i][j1], j1); } } REPD(i,n+1) FOR(k,1,14) REP(j,840) { if (h[i][j][k - 1] == xxx) { h[i][j][k] = xxx; continue; } int i1, j1; decode(h[i][j][k - 1], i1, j1); h[i][j][k] = h[i1][j1][k - 1]; } } int dist(int j, int i, int to) { j %= 840; if (g[i][j] >= to) return 0; int c = 0, r = INF, k = 13; REPD(k,14) { int e = h[i][j][k]; if (e == xxx) { r = min(r, c + (1 << k)); continue; } int i1, j1; decode(e, i1, j1); if (i1 >= to) { r = min(r, c + (1 << k)); continue; } c += 1 << k; i = i1; j = j1; } return r; } void go(VS& s, int from[], int to[]) { int n = SIZE(s); xxx = encode(n + 1, 0); prep(s); REP(qq,q) if (from[qq] < to[qq]) r[qq] = max(r[qq], dist(t[qq], from[qq], to[qq])); reverse(ALL(s)); prep(s); REP(qq,q) if (from[qq] > to[qq]) r[qq] = max(r[qq], dist(t[qq], n - from[qq], n - to[qq])); } int main() { INT(n); INT(m); INT(q1); q = q1; ss1.resize(n); ss2.resize(m); REP(i,n) REP(j,m) { STR(9, ss); string s = ss; ss1[i].resize(9); ss2[j].resize(9); update(ss1[i][SIZE(s)], s); update(ss2[j][SIZE(s)], inv(s)); } REP(qq,q) scanf("%d%d%d%d%d", &t[qq], &a[qq], &b[qq], &c[qq], &d[qq]); s1.resize(n); s2.resize(m); REP(i,n) FORE(it,ss1[i]) update2(s1[i], *it); REP(j,m) FORE(it,ss2[j]) update2(s2[j], *it); go(s1, a, c); go(s2, b, d); REP(qq,q) printf("%d\n", t[qq] + r[qq]); } |