#include <stdio.h> #include <string.h> #include <stdbool.h> #define MAX_N 15000 #define MAX_S 8 #define PERIOD 840 //#define LOG 10 #define LOG 2 #define TYPE_N 0 #define TYPE_H 1 #define TYPE_V 2 #define max(a, b) ((a) > (b) ? (a) : (b)) bool free_h[PERIOD][MAX_N]; bool free_v[PERIOD][MAX_N]; bool isfree[PERIOD][MAX_N]; int mask_h[MAX_N][MAX_S+1]; int mask_v[MAX_N][MAX_S+1]; int type[PERIOD]; struct block { int this_high; int this_low; int next_high[LOG+1]; int next_low[LOG+1]; }; struct block block[PERIOD][MAX_N+1]; void scan_mask(int *len, int *mask) { char str[MAX_S+1]; int i; scanf("%s", str); *len = 0; *mask = 0; for (i = 0; str[i]; i++) { *mask |= (str[i] == '1') << i; (*len)++; } } void scan_masks(int n, int m) { int len, mask; int x, y; for (y = 0; y < n; y++) { for (x = 0; x < m; x++) { scan_mask(&len, &mask); mask_h[y][len] |= ~mask; mask_v[x][len] |= mask; } } } void do_free(int n, int m) { int nm = max(n, m); int p, s; int i, j; for (i = 0; i < n; i++) { for (s = 2; s <= MAX_S; s++) { for (p = 0; p < PERIOD; p += s) { for (j = 0; j < s; j++) free_h[p+j][i] |= mask_h[i][s] & (1 << j); } } } for (i = 0; i < m; i++) { for (s = 2; s <= MAX_S; s++) { for (p = 0; p < PERIOD; p += s) { for (j = 0; j < s; j++) free_v[p+j][i] |= mask_v[i][s] & (1 << j); } } } for (p = 0; p < PERIOD; p++) { for (i = 0; i < n; i++) { if (!free_h[p][i]) type[p] = TYPE_H; } for (i = 0; i < m; i++) { if (!free_v[p][i]) type[p] = TYPE_V; } for (i = 0; i < nm; i++) isfree[p][i] = true; if (type[p] == TYPE_H) { for (i = 0; i < n; i++) isfree[p][i] = free_h[p][i]; } else { for (i = 0; i < m; i++) isfree[p][i] = free_v[p][i]; } } } void do_blocks(int n, int m) { int nm = max(n, m); int p, pp; //int k; int i; for (p = 0; p < PERIOD; p++) { for (i = 1; i <= nm; i++) { if (isfree[p][i-1]) block[p][i].this_low = block[p][i-1].this_low; else block[p][i].this_low = i; } block[p][nm].this_high = nm; for (i = nm - 1; i >= 0; i--) { if (isfree[p][i]) block[p][i].this_high = block[p][i+1].this_high; else block[p][i].this_high = i; } } for (p = 0; p < PERIOD; p++) { pp = (p + 1) % PERIOD; if (type[p] == type[pp]) { for (i = 0; i <= nm; i++) { block[p][i].next_low[0] = block[pp][i].this_low; block[p][i].next_high[0] = block[pp][i].this_high; } } else { for (i = 0; i <= nm; i++) { block[p][i].next_low[0] = 0; block[p][i].next_high[0] = nm; } } } /* for (k = 0; k < LOG; k++) { for (p = 0; p < PERIOD; p++) { pp = (p + (1 << k)) % PERIOD; for (i = 0; i <= nm; i++) { block[p][i].next_low[k+1] = block[pp][ block[p][i].next_low[k] ].next_low[k]; block[p][i].next_high[k+1] = block[pp][ block[p][i].next_high[k] ].next_high[k]; } } } */ } int do_query_low(int p, int x0, int x1) { /* int ans = 0; int k; int x; x0 = block[p][x0].this_low; for (k = LOG; k >= 0; k--) { x = block[p][x0].next_low[k]; //printf("%d\n", x); if (x > x1) { x0 = x; p = (p + (1 << k)) % PERIOD; ans += 1 << k; } } if (x0 > x1) ans++; return ans; */ int ans = 0; x0 = block[p][x0].this_low; while (1) { if (x0 <= x1) return ans; x0 = block[p][x0].next_low[0]; ans++; if (++p == PERIOD) p = 0; } } int do_query_high(int p, int x0, int x1) { /* int ans = 0; int k; int x; x0 = block[p][x0].this_high; for (k = LOG; k >= 0; k--) { x = block[p][x0].next_high[k]; //printf("%d\n", x); if (x < x1) { x0 = x; p = (p + (1 << k)) % PERIOD; ans += 1 << k; } } if (x0 < x1) ans++; return ans; */ int ans = 0; x0 = block[p][x0].this_high; while (1) { if (x0 >= x1) return ans; x0 = block[p][x0].next_high[0]; ans++; if (++p == PERIOD) p = 0; } } int do_query(int p, int x0, int x1) { if (type[p] == TYPE_N) return 0; if (x0 < x1) return do_query_high(p, x0, x1); else return do_query_low(p, x0, x1); } void do_queries(int q) { int i; int t, x0, y0, x1, y1; int p; for (i = 0; i < q; i++) { scanf("%d%d%d%d%d", &t, &y0, &x0, &y1, &x1); p = t % PERIOD; if (type[p] == TYPE_H) printf("%d\n", t + do_query(p, y0, y1)); else printf("%d\n", t + do_query(p, x0, x1)); } } int main(void) { int n, m, q; //int p, i, k; scanf("%d%d%d", &n, &m, &q); scan_masks(n, m); do_free(n, m); do_blocks(n, m); /* for (p = 0; p < 4; p++) { printf("%d: %c\n", p, type[p] ? (type[p] == TYPE_H ? 'H' : 'V') : '-'); for (i = 0; i < n; i++) printf("%d ", isfree[p][i]); printf("\n"); for (k = 0; k < 3; k++) { for (i = 0; i <= n; i++) printf("%d ", block[p][i].next_low[k]); printf("\n"); } for (k = 0; k < 3; k++) { for (i = 0; i <= n; i++) printf("%d ", block[p][i].next_high[k]); printf("\n"); } } */ do_queries(q); 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 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 | #include <stdio.h> #include <string.h> #include <stdbool.h> #define MAX_N 15000 #define MAX_S 8 #define PERIOD 840 //#define LOG 10 #define LOG 2 #define TYPE_N 0 #define TYPE_H 1 #define TYPE_V 2 #define max(a, b) ((a) > (b) ? (a) : (b)) bool free_h[PERIOD][MAX_N]; bool free_v[PERIOD][MAX_N]; bool isfree[PERIOD][MAX_N]; int mask_h[MAX_N][MAX_S+1]; int mask_v[MAX_N][MAX_S+1]; int type[PERIOD]; struct block { int this_high; int this_low; int next_high[LOG+1]; int next_low[LOG+1]; }; struct block block[PERIOD][MAX_N+1]; void scan_mask(int *len, int *mask) { char str[MAX_S+1]; int i; scanf("%s", str); *len = 0; *mask = 0; for (i = 0; str[i]; i++) { *mask |= (str[i] == '1') << i; (*len)++; } } void scan_masks(int n, int m) { int len, mask; int x, y; for (y = 0; y < n; y++) { for (x = 0; x < m; x++) { scan_mask(&len, &mask); mask_h[y][len] |= ~mask; mask_v[x][len] |= mask; } } } void do_free(int n, int m) { int nm = max(n, m); int p, s; int i, j; for (i = 0; i < n; i++) { for (s = 2; s <= MAX_S; s++) { for (p = 0; p < PERIOD; p += s) { for (j = 0; j < s; j++) free_h[p+j][i] |= mask_h[i][s] & (1 << j); } } } for (i = 0; i < m; i++) { for (s = 2; s <= MAX_S; s++) { for (p = 0; p < PERIOD; p += s) { for (j = 0; j < s; j++) free_v[p+j][i] |= mask_v[i][s] & (1 << j); } } } for (p = 0; p < PERIOD; p++) { for (i = 0; i < n; i++) { if (!free_h[p][i]) type[p] = TYPE_H; } for (i = 0; i < m; i++) { if (!free_v[p][i]) type[p] = TYPE_V; } for (i = 0; i < nm; i++) isfree[p][i] = true; if (type[p] == TYPE_H) { for (i = 0; i < n; i++) isfree[p][i] = free_h[p][i]; } else { for (i = 0; i < m; i++) isfree[p][i] = free_v[p][i]; } } } void do_blocks(int n, int m) { int nm = max(n, m); int p, pp; //int k; int i; for (p = 0; p < PERIOD; p++) { for (i = 1; i <= nm; i++) { if (isfree[p][i-1]) block[p][i].this_low = block[p][i-1].this_low; else block[p][i].this_low = i; } block[p][nm].this_high = nm; for (i = nm - 1; i >= 0; i--) { if (isfree[p][i]) block[p][i].this_high = block[p][i+1].this_high; else block[p][i].this_high = i; } } for (p = 0; p < PERIOD; p++) { pp = (p + 1) % PERIOD; if (type[p] == type[pp]) { for (i = 0; i <= nm; i++) { block[p][i].next_low[0] = block[pp][i].this_low; block[p][i].next_high[0] = block[pp][i].this_high; } } else { for (i = 0; i <= nm; i++) { block[p][i].next_low[0] = 0; block[p][i].next_high[0] = nm; } } } /* for (k = 0; k < LOG; k++) { for (p = 0; p < PERIOD; p++) { pp = (p + (1 << k)) % PERIOD; for (i = 0; i <= nm; i++) { block[p][i].next_low[k+1] = block[pp][ block[p][i].next_low[k] ].next_low[k]; block[p][i].next_high[k+1] = block[pp][ block[p][i].next_high[k] ].next_high[k]; } } } */ } int do_query_low(int p, int x0, int x1) { /* int ans = 0; int k; int x; x0 = block[p][x0].this_low; for (k = LOG; k >= 0; k--) { x = block[p][x0].next_low[k]; //printf("%d\n", x); if (x > x1) { x0 = x; p = (p + (1 << k)) % PERIOD; ans += 1 << k; } } if (x0 > x1) ans++; return ans; */ int ans = 0; x0 = block[p][x0].this_low; while (1) { if (x0 <= x1) return ans; x0 = block[p][x0].next_low[0]; ans++; if (++p == PERIOD) p = 0; } } int do_query_high(int p, int x0, int x1) { /* int ans = 0; int k; int x; x0 = block[p][x0].this_high; for (k = LOG; k >= 0; k--) { x = block[p][x0].next_high[k]; //printf("%d\n", x); if (x < x1) { x0 = x; p = (p + (1 << k)) % PERIOD; ans += 1 << k; } } if (x0 < x1) ans++; return ans; */ int ans = 0; x0 = block[p][x0].this_high; while (1) { if (x0 >= x1) return ans; x0 = block[p][x0].next_high[0]; ans++; if (++p == PERIOD) p = 0; } } int do_query(int p, int x0, int x1) { if (type[p] == TYPE_N) return 0; if (x0 < x1) return do_query_high(p, x0, x1); else return do_query_low(p, x0, x1); } void do_queries(int q) { int i; int t, x0, y0, x1, y1; int p; for (i = 0; i < q; i++) { scanf("%d%d%d%d%d", &t, &y0, &x0, &y1, &x1); p = t % PERIOD; if (type[p] == TYPE_H) printf("%d\n", t + do_query(p, y0, y1)); else printf("%d\n", t + do_query(p, x0, x1)); } } int main(void) { int n, m, q; //int p, i, k; scanf("%d%d%d", &n, &m, &q); scan_masks(n, m); do_free(n, m); do_blocks(n, m); /* for (p = 0; p < 4; p++) { printf("%d: %c\n", p, type[p] ? (type[p] == TYPE_H ? 'H' : 'V') : '-'); for (i = 0; i < n; i++) printf("%d ", isfree[p][i]); printf("\n"); for (k = 0; k < 3; k++) { for (i = 0; i <= n; i++) printf("%d ", block[p][i].next_low[k]); printf("\n"); } for (k = 0; k < 3; k++) { for (i = 0; i <= n; i++) printf("%d ", block[p][i].next_high[k]); printf("\n"); } } */ do_queries(q); return 0; } |