#include <bits/stdc++.h> #define FOR(i, n) for(int i = 0; i < (n); ++i) #define REP(i, a, b) for(int i = (a); i < (b); ++i) #define TRAV(i, a) for(auto & i : (a)) #define SZ(x) ((int)(x).size()) #define X first #define Y second #define PR std::pair #define MP std::make_pair typedef long long ll; typedef std::pair<int, int> PII; typedef std::vector<int> VI; constexpr uint8_t ONE = 1; constexpr uint8_t ALL = -1; constexpr int DIM = 15'005; constexpr int N = 1'123'456; constexpr int MOD = 840; struct Query{ int t, a, b, c, d; Query(){} Query(int t_, int a_, int b_, int c_, int d_) : t(t_), a(a_), b(b_), c(c_), d(d_) {} }; uint8_t schedule[N]; uint8_t period[N]; int ans[N]; Query que[N]; constexpr int LOG = 18; int jmp[LOG][DIM * MOD]; // TODO: swap dims int n, m, q; int ind(int i, int j, int co){ return co * i + j; } uint8_t to_uint(const std::string& s){ uint8_t ret = 0; FOR(i, SZ(s)) if(s[i] == '1') ret |= (ONE<<i); return ret; } uint8_t rot_temp[N]; void rotuj(){ auto plac = [&](int i, int j){ return MP(j, n - 1 - i); }; auto krzyz = [&](int i, int j){ return MP(j, n - 2 - i); }; FOR(i, q){ auto& x = que[i]; std::tie(x.a, x.b) = plac(x.a, x.b); std::tie(x.c, x.d) = plac(x.c, x.d); } std::fill(rot_temp, rot_temp + N, 0); FOR(i, n-1){ FOR(j, m-1){ auto nw = krzyz(i, j); rot_temp[ind(nw.X, nw.Y, n)] = period[ind(i, j, m)]; } } FOR(i, N) period[i] = rot_temp[i]; std::fill(rot_temp, rot_temp + N, 0); FOR(i, n-1){ FOR(j, m-1){ auto nw = krzyz(i, j); rot_temp[ind(nw.X, nw.Y, n)] = schedule[ind(i, j, m)] ^ ALL; } } FOR(i, N) schedule[i] = rot_temp[i]; std::swap(n, m); } int ktory[MOD][10]; void prepro(){ FOR(i, MOD) REP(j, 2, 9) ktory[i][j] = i%j; FOR(i, n-1){ uint8_t mask[9] = {}; FOR(j, m-1){ mask[period[i*m+j]] |= schedule[i*m+j]; } FOR(j, MOD){ bool good = false; REP(b, 2, 9){ if(mask[b] & (1<<ktory[j][b])){ // TODO: modulo szybsze? good = true; break; } } jmp[0][i*MOD+j] = (good ? ((i+1)*MOD+j) : (i*MOD + (j+1)%MOD)); } } FOR(j, MOD) jmp[0][(n-1)*MOD+j] = (n-1)*MOD + j; REP(l, 1, LOG){ FOR(i, n * MOD){ jmp[l][i] = jmp[l-1][jmp[l-1][i]]; } } } int policz(int a, int b, int t){ int start = a; int ret = 0; a = a*MOD + t; for(int l = LOG-1; l >= 0; --l){ if(jmp[l][a] / MOD < b){ ret += (1<<l); a = jmp[l][a]; } } // assert(a / MOD < b); // assert(jmp[0][a] / MOD == b); return ret - (a / MOD - start); } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(0); std::cin >> n >> m >> q; n++;m++; std::string s; FOR(i, n-1){ FOR(j, m-1){ std::cin >> s; period[ind(i, j, m)] = SZ(s); schedule[ind(i, j, m)] = to_uint(s) ^ ALL; } } FOR(i, q){ auto& x = que[i]; std::cin >> x.t >> x.a >> x.b >> x.c >> x.d; ans[i] = x.t; } FOR(_, 4){ rotuj(); prepro(); FOR(i, q){ auto& x = que[i]; if(x.a < x.c){ ans[i] = std::max(ans[i], x.t + policz(x.a, x.c, x.t % MOD)); } } } FOR(i, q) std::cout << ans[i] << "\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 | #include <bits/stdc++.h> #define FOR(i, n) for(int i = 0; i < (n); ++i) #define REP(i, a, b) for(int i = (a); i < (b); ++i) #define TRAV(i, a) for(auto & i : (a)) #define SZ(x) ((int)(x).size()) #define X first #define Y second #define PR std::pair #define MP std::make_pair typedef long long ll; typedef std::pair<int, int> PII; typedef std::vector<int> VI; constexpr uint8_t ONE = 1; constexpr uint8_t ALL = -1; constexpr int DIM = 15'005; constexpr int N = 1'123'456; constexpr int MOD = 840; struct Query{ int t, a, b, c, d; Query(){} Query(int t_, int a_, int b_, int c_, int d_) : t(t_), a(a_), b(b_), c(c_), d(d_) {} }; uint8_t schedule[N]; uint8_t period[N]; int ans[N]; Query que[N]; constexpr int LOG = 18; int jmp[LOG][DIM * MOD]; // TODO: swap dims int n, m, q; int ind(int i, int j, int co){ return co * i + j; } uint8_t to_uint(const std::string& s){ uint8_t ret = 0; FOR(i, SZ(s)) if(s[i] == '1') ret |= (ONE<<i); return ret; } uint8_t rot_temp[N]; void rotuj(){ auto plac = [&](int i, int j){ return MP(j, n - 1 - i); }; auto krzyz = [&](int i, int j){ return MP(j, n - 2 - i); }; FOR(i, q){ auto& x = que[i]; std::tie(x.a, x.b) = plac(x.a, x.b); std::tie(x.c, x.d) = plac(x.c, x.d); } std::fill(rot_temp, rot_temp + N, 0); FOR(i, n-1){ FOR(j, m-1){ auto nw = krzyz(i, j); rot_temp[ind(nw.X, nw.Y, n)] = period[ind(i, j, m)]; } } FOR(i, N) period[i] = rot_temp[i]; std::fill(rot_temp, rot_temp + N, 0); FOR(i, n-1){ FOR(j, m-1){ auto nw = krzyz(i, j); rot_temp[ind(nw.X, nw.Y, n)] = schedule[ind(i, j, m)] ^ ALL; } } FOR(i, N) schedule[i] = rot_temp[i]; std::swap(n, m); } int ktory[MOD][10]; void prepro(){ FOR(i, MOD) REP(j, 2, 9) ktory[i][j] = i%j; FOR(i, n-1){ uint8_t mask[9] = {}; FOR(j, m-1){ mask[period[i*m+j]] |= schedule[i*m+j]; } FOR(j, MOD){ bool good = false; REP(b, 2, 9){ if(mask[b] & (1<<ktory[j][b])){ // TODO: modulo szybsze? good = true; break; } } jmp[0][i*MOD+j] = (good ? ((i+1)*MOD+j) : (i*MOD + (j+1)%MOD)); } } FOR(j, MOD) jmp[0][(n-1)*MOD+j] = (n-1)*MOD + j; REP(l, 1, LOG){ FOR(i, n * MOD){ jmp[l][i] = jmp[l-1][jmp[l-1][i]]; } } } int policz(int a, int b, int t){ int start = a; int ret = 0; a = a*MOD + t; for(int l = LOG-1; l >= 0; --l){ if(jmp[l][a] / MOD < b){ ret += (1<<l); a = jmp[l][a]; } } // assert(a / MOD < b); // assert(jmp[0][a] / MOD == b); return ret - (a / MOD - start); } int main() { std::ios_base::sync_with_stdio(false); std::cin.tie(0); std::cin >> n >> m >> q; n++;m++; std::string s; FOR(i, n-1){ FOR(j, m-1){ std::cin >> s; period[ind(i, j, m)] = SZ(s); schedule[ind(i, j, m)] = to_uint(s) ^ ALL; } } FOR(i, q){ auto& x = que[i]; std::cin >> x.t >> x.a >> x.b >> x.c >> x.d; ans[i] = x.t; } FOR(_, 4){ rotuj(); prepro(); FOR(i, q){ auto& x = que[i]; if(x.a < x.c){ ans[i] = std::max(ans[i], x.t + policz(x.a, x.c, x.t % MOD)); } } } FOR(i, q) std::cout << ans[i] << "\n"; return 0; } |