#pragma GCC optimize ("Ofast") #define _USE_MATH_DEFINES #include <bits/stdc++.h> #define FOR(i, a, b) for (auto i=(a); i<(b); i++) #define FORD(i, a, b) for (int i=(a); i>(b); i--) #define SZ(x) ((int)(x).size()) #define ALL(x) (x).begin(), (x).end() #define PPC(x) __builtin_popcountll(x) #define MSB(x) (63 - __builtin_clzll(x)) #define LSB(x) __builtin_ctz(x) #define ARG(x, i) (get<i>(x)) #define ithBit(m, i) ((m) >> (i) & 1) #define pb push_back #define ft first #define sd second #define kw(a) ((a) * (a)) #ifdef DEBUG #include "debug.h" #else #define dbg(...) 0 #endif using namespace std; template <typename T1, typename T2> inline void remin(T1& a, T2 b) { a = min(a, (T1)b); } template <typename T1, typename T2> inline void remax(T1& a, T2 b) { a = max(a, (T1)b); } const int maxN = 15'002, Lcm = 840, maxS = 9, maxL = 14, maxQ = 1 << 20, maxT = 1 << 18; int colAnd[maxN][maxS], rowAnd[maxN][maxS], colWait[maxN][Lcm], rowWait[maxN][Lcm]; bool rowDom[Lcm], colDom[Lcm]; vector <int> rowQ, colQ; int mod[Lcm][maxS], ful[maxS], Proj[maxT]; tuple <int, int, int> Q[maxQ]; int res[maxQ]; inline void pass(int& t, int dt) { t = Proj[t + dt]; } void init() { FOR(i, 1, maxS) ful[i] = ful[i-1] * 2 + 1; FOR(m, 0, Lcm) FOR(r, 1, maxS) mod[m][r] = m % r; FOR(i, 0, maxN) FOR(s, 0, maxS) rowAnd[i][s] = colAnd[i][s] = ful[s]; FOR(i, 0, maxT) Proj[i] = i % Lcm; } void fillWait(int And[maxN][maxS], int n, int Wait[maxN][Lcm], bool domain[Lcm]) { FOR(i, 0, n) { vector <int> temp; FOR(t, 0, Lcm) { temp.pb(t); int mask = 1; FOR(s, 2, maxS) mask &= And[i][s] >> mod[t][s]; if (mask == 0) { for (int d : temp) Wait[i][d] = t - d; temp.resize(0); } else domain[t] = true; } for (int d : temp) Wait[i][d] = Lcm-d + Wait[i][0]; } } int jp[maxL][maxN][Lcm]; void calcJp(int Wait[maxN][Lcm], int n) { FOR(i, 0, n+1) FOR(t, 0, Lcm) jp[0][i][t] = Wait[i][t]; FOR(l, 1, maxL) FOR(i, 0, n+1) { int j = min(n, i + (1 << l-1)); FOR(t, 0, Lcm) { int t1 = jp[l-1][i][t]; int t2 = jp[l-1][j][Proj[t + t1]]; jp[l][i][t] = t1 + t2; } } } int query(int a, const int b, int t) { int ret = 0; FORD(l, maxL-1, -1) { int c = a + (1 << l); if (c > b) continue; int dt = jp[l][a][t]; ret += dt; pass(t, dt); a = c; } //assert(a == b); return ret; } void proc(int Wait[maxN][Lcm], vector <int>& qs, int n) { calcJp(Wait, n); for (int i : qs) { auto [a, b, t] = Q[i]; if (a < b) res[i] += query(a, b, t); } FOR(i, 0, n/2) FOR(t, 0, Lcm) swap(Wait[i][t], Wait[n-1-i][t]); #define INV(i) (n-(i)) calcJp(Wait, n); for (int i : qs) { auto [a, b, t] = Q[i]; if (a > b) res[i] += query(INV(a), INV(b), t); } } void solve() { init(); int n, m, q; scanf ("%d%d%d", &n, &m, &q); FOR(i, 0, n) FOR(j, 0, m) { char in[20]; scanf ("%s", in); int x = 0, s = strlen(in); FOR(k, 0, s) x |= in[k]-'0' << k; rowAnd[i][s] &= x; colAnd[j][s] &= ful[s] ^ x; } fillWait(rowAnd, n, rowWait, rowDom); fillWait(colAnd, m, colWait, colDom); FOR(i, 0, q) { int t, a, b, c, d; scanf ("%d%d%d%d%d", &t, &a, &b, &c, &d); res[i] = t; t %= Lcm; bool vert = colDom[t]; Q[i] = vert ? make_tuple(b, d, t) : make_tuple(a, c, t); if (ARG(Q[i], 0) != ARG(Q[i], 1)) (vert ? colQ : rowQ).pb(i); } proc(rowWait, rowQ, n); proc(colWait, colQ, m); FOR(i, 0, q) printf("%d\n", res[i]); } int main() { int t = 1; //scanf ("%d", &t); FOR(tid, 1, t+1) { //printf("Case #%d: ", tid); solve(); } 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 | #pragma GCC optimize ("Ofast") #define _USE_MATH_DEFINES #include <bits/stdc++.h> #define FOR(i, a, b) for (auto i=(a); i<(b); i++) #define FORD(i, a, b) for (int i=(a); i>(b); i--) #define SZ(x) ((int)(x).size()) #define ALL(x) (x).begin(), (x).end() #define PPC(x) __builtin_popcountll(x) #define MSB(x) (63 - __builtin_clzll(x)) #define LSB(x) __builtin_ctz(x) #define ARG(x, i) (get<i>(x)) #define ithBit(m, i) ((m) >> (i) & 1) #define pb push_back #define ft first #define sd second #define kw(a) ((a) * (a)) #ifdef DEBUG #include "debug.h" #else #define dbg(...) 0 #endif using namespace std; template <typename T1, typename T2> inline void remin(T1& a, T2 b) { a = min(a, (T1)b); } template <typename T1, typename T2> inline void remax(T1& a, T2 b) { a = max(a, (T1)b); } const int maxN = 15'002, Lcm = 840, maxS = 9, maxL = 14, maxQ = 1 << 20, maxT = 1 << 18; int colAnd[maxN][maxS], rowAnd[maxN][maxS], colWait[maxN][Lcm], rowWait[maxN][Lcm]; bool rowDom[Lcm], colDom[Lcm]; vector <int> rowQ, colQ; int mod[Lcm][maxS], ful[maxS], Proj[maxT]; tuple <int, int, int> Q[maxQ]; int res[maxQ]; inline void pass(int& t, int dt) { t = Proj[t + dt]; } void init() { FOR(i, 1, maxS) ful[i] = ful[i-1] * 2 + 1; FOR(m, 0, Lcm) FOR(r, 1, maxS) mod[m][r] = m % r; FOR(i, 0, maxN) FOR(s, 0, maxS) rowAnd[i][s] = colAnd[i][s] = ful[s]; FOR(i, 0, maxT) Proj[i] = i % Lcm; } void fillWait(int And[maxN][maxS], int n, int Wait[maxN][Lcm], bool domain[Lcm]) { FOR(i, 0, n) { vector <int> temp; FOR(t, 0, Lcm) { temp.pb(t); int mask = 1; FOR(s, 2, maxS) mask &= And[i][s] >> mod[t][s]; if (mask == 0) { for (int d : temp) Wait[i][d] = t - d; temp.resize(0); } else domain[t] = true; } for (int d : temp) Wait[i][d] = Lcm-d + Wait[i][0]; } } int jp[maxL][maxN][Lcm]; void calcJp(int Wait[maxN][Lcm], int n) { FOR(i, 0, n+1) FOR(t, 0, Lcm) jp[0][i][t] = Wait[i][t]; FOR(l, 1, maxL) FOR(i, 0, n+1) { int j = min(n, i + (1 << l-1)); FOR(t, 0, Lcm) { int t1 = jp[l-1][i][t]; int t2 = jp[l-1][j][Proj[t + t1]]; jp[l][i][t] = t1 + t2; } } } int query(int a, const int b, int t) { int ret = 0; FORD(l, maxL-1, -1) { int c = a + (1 << l); if (c > b) continue; int dt = jp[l][a][t]; ret += dt; pass(t, dt); a = c; } //assert(a == b); return ret; } void proc(int Wait[maxN][Lcm], vector <int>& qs, int n) { calcJp(Wait, n); for (int i : qs) { auto [a, b, t] = Q[i]; if (a < b) res[i] += query(a, b, t); } FOR(i, 0, n/2) FOR(t, 0, Lcm) swap(Wait[i][t], Wait[n-1-i][t]); #define INV(i) (n-(i)) calcJp(Wait, n); for (int i : qs) { auto [a, b, t] = Q[i]; if (a > b) res[i] += query(INV(a), INV(b), t); } } void solve() { init(); int n, m, q; scanf ("%d%d%d", &n, &m, &q); FOR(i, 0, n) FOR(j, 0, m) { char in[20]; scanf ("%s", in); int x = 0, s = strlen(in); FOR(k, 0, s) x |= in[k]-'0' << k; rowAnd[i][s] &= x; colAnd[j][s] &= ful[s] ^ x; } fillWait(rowAnd, n, rowWait, rowDom); fillWait(colAnd, m, colWait, colDom); FOR(i, 0, q) { int t, a, b, c, d; scanf ("%d%d%d%d%d", &t, &a, &b, &c, &d); res[i] = t; t %= Lcm; bool vert = colDom[t]; Q[i] = vert ? make_tuple(b, d, t) : make_tuple(a, c, t); if (ARG(Q[i], 0) != ARG(Q[i], 1)) (vert ? colQ : rowQ).pb(i); } proc(rowWait, rowQ, n); proc(colWait, colQ, m); FOR(i, 0, q) printf("%d\n", res[i]); } int main() { int t = 1; //scanf ("%d", &t); FOR(tid, 1, t+1) { //printf("Case #%d: ", tid); solve(); } return 0; } |