// {{{ #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define FOR(i, b, e) for (int i = (b); i <= (e); i++) #define FORD(i, b, e) for (int i = (e); i >= (b); i--) #define MP make_pair #define FS first #define ND second #define PB push_back #define ALL(x) x.begin(), x.end() using namespace std; using LL = long long; using LD = long double; using PII = pair<int,int>; using PLL = pair<LL,LL>; using PLD = pair<LD,LD>; using VI = vector<int>; using VLL = vector<LL>; using VLD = vector<LD>; using VB = vector<bool>; using VS = vector<string>; template<class T> using V = vector<T>; template<class T1, class T2> using P = pair<T1,T2>; template<class T,class Comp=greater<T>> using PQ = priority_queue<T,V<T>,Comp>; template<class T> int sz(const T& a) { return (int)a.size(); } template<class T> void amin(T& a, T b) { a = min(a, b); } template<class T> void amax(T& a, T b) { a = max(a, b); } /* const size_t rseed = std::chrono::high_resolution_clock::now().time_since_epoch().count(); mt19937 rnd(rseed); template<class T> T randint(T lo, T hi) { return uniform_int_distribution<T>{lo,hi}(rnd); } */ /* find_by_order(k) - k'th largest counting from 0; order_of_key(k) - number of items strictly smaller than k; join(other), split(k, other) - all keys in other greater than in *(this). */ template<class K,class V,class Comp=less<K>> using ordered_map = __gnu_pbds::tree<K,V,Comp, __gnu_pbds::rb_tree_tag,__gnu_pbds::tree_order_statistics_node_update>; template<class T,class Comp=less<T>> using ordered_set = ordered_map<T,__gnu_pbds::null_type,Comp>; const int inf = 1e9 + 1; const LL infl = 1e18 + 1; void solve(); // }}} int main() { int t = 1; // scanf("%d", &t); FOR(i, 1, t) { // printf("Case #%d: ", i); solve(); } } const int T = 3 * 5 * 7 * 8; const int N = 1e6; int n, m, q; int mask[2 * N + 1]; int cpos(int i, int j) { return (i - 1) * m + j - 1; } int vpos(int i, int j) { return i * (m + 1) + j; } int tr_cpos(int i, int j) { return (j - 1) * n + i - 1; } int tr_vpos(int i, int j) { return j * (n + 1) + i; } int t0; bool is_vert(int x, int y) { int l = (mask[cpos(x, y)] >> 8) & ((1 << 8) - 1); int msk = mask[cpos(x, y)] & ((1 << 8) - 1); return (msk & (1 << (t0 % l))) == 0; } bool is_horiz(int x, int y) { int l = mask[tr_cpos(x, y)] >> 24; int msk = (mask[tr_cpos(x, y)] >> 16) & ((1 << 8) - 1); return (msk & (1 << (t0 % l))) > 0; } V<PII> a[T]; VI dp[20][T]; const int Ve = 0; const int Ho = 1; const int L = 0; const int R = 1; int index(int t, int x) { t %= T; int l = 0, r = sz(a[t]) - 1; for (;;) { int i = (l + r) / 2; if (a[t][i].ND < x) { l = i + 1; } else if (a[t][i].FS > x) { r = i - 1; } else { return i; } } } int query(int t, int i, int x) { t %= T; int k = -1; if (x < a[t][i].FS) { int l = 0, r = 19; while (l <= r) { int s = (l + r) / 2; int j = dp[s][t][i]; if (x < a[(t + (1 << s)) % T][j].FS) { l = s + 1; } else { k = s; r = s - 1; } } } else if (x > a[t][i].ND) { int l = 0, r = 19; while (l <= r) { int s = (l + r) / 2; int j = dp[s][t][i]; if (x > a[(t + (1 << s)) % T][j].ND) { l = s + 1; } else { k = s; r = s - 1; } } } else { return 0; } if (k) { int j = dp[k - 1][t][i]; return (1 << (k - 1)) + query(t + (1 << (k - 1)), j, x); } else { return 1; } } struct Q { int ans, t, x1, x2, y1, y2; }; Q qr[N + 1]; template < class result_t = std::chrono::milliseconds, class clock_t = std::chrono::steady_clock, class duration_t = std::chrono::milliseconds > auto since(std::chrono::time_point<clock_t, duration_t> const& start) { return std::chrono::duration_cast<result_t>(clock_t::now() - start); } void solve() { scanf("%d %d %d", &n, &m, &q); FOR(x, 1, n) FOR(y, 1, m) { char s[9]; scanf("%s", s); int l = strlen(s), msk = 0; FOR(i, 0, l - 1) { if (s[i] == '1') { msk |= (1 << i); } } msk |= (l << 8); mask[cpos(x, y)] |= msk; mask[tr_cpos(x, y)] |= (msk << 16); } FOR(i, 1, q) { scanf("%d %d %d %d %d", &qr[i].t, &qr[i].x1, &qr[i].y1, &qr[i].x2, &qr[i].y2); } FOR(o, 0, 1) { FOR(t, 0, T - 1) { a[t].clear(); t0 = t; if (o == Ve) { int x_prev = 0; FOR(x, 1, n) { bool ok = true; FOR(y, 1, m) if (is_vert(x, y)) { ok = false; break; } if (ok) { a[t].PB(MP(x_prev, x - 1)); x_prev = x; } } a[t].PB(MP(x_prev, n)); } else { int y_prev = 0; FOR(y, 1, m) { bool ok = true; FOR(x, 1, n) if (is_horiz(x, y)) { ok = false; break; } if (ok) { a[t].PB(MP(y_prev, y - 1)); y_prev = y; } } a[t].PB(MP(y_prev, m)); } } FOR(d, 0, 1) { FOR(t, 0, T - 1) { t0 = (t + 1) % T; if (d == L) { int j = 0; dp[0][t].resize(sz(a[t])); FOR(i, 0, sz(a[t]) - 1) { while (a[t0][j].ND < a[t][i].FS) j++; dp[0][t][i] = j; } } else { int j = sz(a[t0]) - 1; dp[0][t].resize(sz(a[t])); FORD(i, 0, sz(a[t]) - 1) { while (a[t0][j].FS > a[t][i].ND) j--; dp[0][t][i] = j; } } } FOR(k, 1, 19) FOR(t, 0, T - 1) { dp[k][t].resize(sz(a[t])); FOR(i, 0, sz(a[t]) - 1) { int i1 = dp[k - 1][t][i]; int i2 = dp[k - 1][(t + (1 << (k - 1))) % T][i1]; dp[k][t][i] = i2; } } FOR(i, 1, q) { if (o == Ve) { if ((d == R && qr[i].x1 < qr[i].x2) || (d == L && qr[i].x1 > qr[i].x2)) { int j = index(qr[i].t, qr[i].x1); qr[i].ans = query(qr[i].t, j, qr[i].x2); } } else { if ((d == R && qr[i].y1 < qr[i].y2) || (d == L && qr[i].y1 > qr[i].y2)) { int j = index(qr[i].t, qr[i].y1); amax(qr[i].ans, query(qr[i].t, j, qr[i].y2)); } } } } } FOR(i, 1, q) { printf("%d\n", qr[i].t + qr[i].ans); } // std::cerr << "Elapsed(ms)=" << since(start).count() << std::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 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 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 | // {{{ #include <bits/stdc++.h> #include <ext/pb_ds/assoc_container.hpp> #include <ext/pb_ds/tree_policy.hpp> #define FOR(i, b, e) for (int i = (b); i <= (e); i++) #define FORD(i, b, e) for (int i = (e); i >= (b); i--) #define MP make_pair #define FS first #define ND second #define PB push_back #define ALL(x) x.begin(), x.end() using namespace std; using LL = long long; using LD = long double; using PII = pair<int,int>; using PLL = pair<LL,LL>; using PLD = pair<LD,LD>; using VI = vector<int>; using VLL = vector<LL>; using VLD = vector<LD>; using VB = vector<bool>; using VS = vector<string>; template<class T> using V = vector<T>; template<class T1, class T2> using P = pair<T1,T2>; template<class T,class Comp=greater<T>> using PQ = priority_queue<T,V<T>,Comp>; template<class T> int sz(const T& a) { return (int)a.size(); } template<class T> void amin(T& a, T b) { a = min(a, b); } template<class T> void amax(T& a, T b) { a = max(a, b); } /* const size_t rseed = std::chrono::high_resolution_clock::now().time_since_epoch().count(); mt19937 rnd(rseed); template<class T> T randint(T lo, T hi) { return uniform_int_distribution<T>{lo,hi}(rnd); } */ /* find_by_order(k) - k'th largest counting from 0; order_of_key(k) - number of items strictly smaller than k; join(other), split(k, other) - all keys in other greater than in *(this). */ template<class K,class V,class Comp=less<K>> using ordered_map = __gnu_pbds::tree<K,V,Comp, __gnu_pbds::rb_tree_tag,__gnu_pbds::tree_order_statistics_node_update>; template<class T,class Comp=less<T>> using ordered_set = ordered_map<T,__gnu_pbds::null_type,Comp>; const int inf = 1e9 + 1; const LL infl = 1e18 + 1; void solve(); // }}} int main() { int t = 1; // scanf("%d", &t); FOR(i, 1, t) { // printf("Case #%d: ", i); solve(); } } const int T = 3 * 5 * 7 * 8; const int N = 1e6; int n, m, q; int mask[2 * N + 1]; int cpos(int i, int j) { return (i - 1) * m + j - 1; } int vpos(int i, int j) { return i * (m + 1) + j; } int tr_cpos(int i, int j) { return (j - 1) * n + i - 1; } int tr_vpos(int i, int j) { return j * (n + 1) + i; } int t0; bool is_vert(int x, int y) { int l = (mask[cpos(x, y)] >> 8) & ((1 << 8) - 1); int msk = mask[cpos(x, y)] & ((1 << 8) - 1); return (msk & (1 << (t0 % l))) == 0; } bool is_horiz(int x, int y) { int l = mask[tr_cpos(x, y)] >> 24; int msk = (mask[tr_cpos(x, y)] >> 16) & ((1 << 8) - 1); return (msk & (1 << (t0 % l))) > 0; } V<PII> a[T]; VI dp[20][T]; const int Ve = 0; const int Ho = 1; const int L = 0; const int R = 1; int index(int t, int x) { t %= T; int l = 0, r = sz(a[t]) - 1; for (;;) { int i = (l + r) / 2; if (a[t][i].ND < x) { l = i + 1; } else if (a[t][i].FS > x) { r = i - 1; } else { return i; } } } int query(int t, int i, int x) { t %= T; int k = -1; if (x < a[t][i].FS) { int l = 0, r = 19; while (l <= r) { int s = (l + r) / 2; int j = dp[s][t][i]; if (x < a[(t + (1 << s)) % T][j].FS) { l = s + 1; } else { k = s; r = s - 1; } } } else if (x > a[t][i].ND) { int l = 0, r = 19; while (l <= r) { int s = (l + r) / 2; int j = dp[s][t][i]; if (x > a[(t + (1 << s)) % T][j].ND) { l = s + 1; } else { k = s; r = s - 1; } } } else { return 0; } if (k) { int j = dp[k - 1][t][i]; return (1 << (k - 1)) + query(t + (1 << (k - 1)), j, x); } else { return 1; } } struct Q { int ans, t, x1, x2, y1, y2; }; Q qr[N + 1]; template < class result_t = std::chrono::milliseconds, class clock_t = std::chrono::steady_clock, class duration_t = std::chrono::milliseconds > auto since(std::chrono::time_point<clock_t, duration_t> const& start) { return std::chrono::duration_cast<result_t>(clock_t::now() - start); } void solve() { scanf("%d %d %d", &n, &m, &q); FOR(x, 1, n) FOR(y, 1, m) { char s[9]; scanf("%s", s); int l = strlen(s), msk = 0; FOR(i, 0, l - 1) { if (s[i] == '1') { msk |= (1 << i); } } msk |= (l << 8); mask[cpos(x, y)] |= msk; mask[tr_cpos(x, y)] |= (msk << 16); } FOR(i, 1, q) { scanf("%d %d %d %d %d", &qr[i].t, &qr[i].x1, &qr[i].y1, &qr[i].x2, &qr[i].y2); } FOR(o, 0, 1) { FOR(t, 0, T - 1) { a[t].clear(); t0 = t; if (o == Ve) { int x_prev = 0; FOR(x, 1, n) { bool ok = true; FOR(y, 1, m) if (is_vert(x, y)) { ok = false; break; } if (ok) { a[t].PB(MP(x_prev, x - 1)); x_prev = x; } } a[t].PB(MP(x_prev, n)); } else { int y_prev = 0; FOR(y, 1, m) { bool ok = true; FOR(x, 1, n) if (is_horiz(x, y)) { ok = false; break; } if (ok) { a[t].PB(MP(y_prev, y - 1)); y_prev = y; } } a[t].PB(MP(y_prev, m)); } } FOR(d, 0, 1) { FOR(t, 0, T - 1) { t0 = (t + 1) % T; if (d == L) { int j = 0; dp[0][t].resize(sz(a[t])); FOR(i, 0, sz(a[t]) - 1) { while (a[t0][j].ND < a[t][i].FS) j++; dp[0][t][i] = j; } } else { int j = sz(a[t0]) - 1; dp[0][t].resize(sz(a[t])); FORD(i, 0, sz(a[t]) - 1) { while (a[t0][j].FS > a[t][i].ND) j--; dp[0][t][i] = j; } } } FOR(k, 1, 19) FOR(t, 0, T - 1) { dp[k][t].resize(sz(a[t])); FOR(i, 0, sz(a[t]) - 1) { int i1 = dp[k - 1][t][i]; int i2 = dp[k - 1][(t + (1 << (k - 1))) % T][i1]; dp[k][t][i] = i2; } } FOR(i, 1, q) { if (o == Ve) { if ((d == R && qr[i].x1 < qr[i].x2) || (d == L && qr[i].x1 > qr[i].x2)) { int j = index(qr[i].t, qr[i].x1); qr[i].ans = query(qr[i].t, j, qr[i].x2); } } else { if ((d == R && qr[i].y1 < qr[i].y2) || (d == L && qr[i].y1 > qr[i].y2)) { int j = index(qr[i].t, qr[i].y1); amax(qr[i].ans, query(qr[i].t, j, qr[i].y2)); } } } } } FOR(i, 1, q) { printf("%d\n", qr[i].t + qr[i].ans); } // std::cerr << "Elapsed(ms)=" << since(start).count() << std::endl; } |