#include <cstdio> #include <cstring> #include <bitset> #include <vector> const int LCM = 840; const int MELEM = 1<<14; const int TSIZE = 2*MELEM; std::bitset<LCM> ZEROS("000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"); std::bitset<LCM> ONES("111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111"); std::vector<bool> horizontal[LCM]; std::vector<bool> vertical[LCM]; std::vector<int> horizontal_wait[LCM]; std::vector<int> vertical_wait[LCM]; int horizontal_tree[LCM][TSIZE]; int vertical_tree[LCM][TSIZE]; int horizontal_tree_rev[LCM][TSIZE]; int vertical_tree_rev[LCM][TSIZE]; void update_wait(std::vector<int> wait[LCM], const std::vector<bool> blocked[LCM]) { int N = wait[0].size()-1; for (int i=1; i<=N; ++i) { int cnt = 1; while (blocked[cnt-1][i]) { cnt++; } for (int ts=LCM-1; ts>=0; ts--) { if (blocked[ts][i] == false) { wait[ts][i] = 0; cnt = 1; } else { wait[ts][i] = cnt; cnt++; } } } } void build_tree(int tree[LCM][TSIZE], int revtree[LCM][TSIZE], const std::vector<int> wait[LCM], int node, int left, int right) { int N = wait[0].size()-1; if (left >= N) { return; } if (left == right) { for (int i=0; i<LCM; ++i) { tree[i][node] = 0; revtree[i][node] = 0; } return; } build_tree(tree, revtree, wait, 2*node, left, (left+right)/2); build_tree(tree, revtree, wait, 2*node+1, (left+right)/2 + 1, right); for (int i=0; i<LCM; ++i) { int mid = (left+right)/2; int x = tree[i][2*node]; int w = 0; if (mid+1 <= N) { w = wait[(i+x) % LCM][mid+1]; } tree[i][node] = x+w+tree[(i+x+w) % LCM][2*node+1]; mid = (left+right)/2 + 1; x = revtree[i][2*node+1]; w = 0; if (mid <= N) { w = wait[(i+x) % LCM][mid]; } revtree[i][node] = x+w+revtree[(i+x+w) % LCM][2*node]; } } int check(int tree[LCM][TSIZE], std::vector<int> wait[LCM], int t, int a, int b, bool rev, int node, int left, int right) { //printf("CHECK a=%d b=%d left=%d right=%d val=%d\n", a, b, left, right, tree[t][node]); if (a>=right || left>=b) { return 0; } if (left == right || a == b) { return 0; } else if (b-a == 1) { //printf("SINGLE t=%d a=%d b=%d %d %d\n", t, a, b, rev?a:b, wait[t % LCM][rev?b:a]); return wait[t % LCM][b]; } else if (a==left && b==right) { return tree[t % LCM][node]; } else { int mid = (left+right)/2; if (b<=mid) { return check(tree, wait, t, a, b, rev, 2*node, left, mid); } else if (a>=mid+1) { return check(tree, wait, t, a, b, rev, 2*node+1, mid+1, right); } if (!rev) { int x = check(tree, wait, t, a, mid, rev, 2*node, left, mid); int w = wait[(t+x) % LCM][mid+1]; //printf("WAIT %d %d %d\n", t+x, mid+1, w); return x+w+check(tree, wait, (t+x+w) % LCM, mid+1, b, rev, 2*node+1, mid+1, right); } else { int x = check(tree, wait, t, mid+1, b, rev, 2*node+1, mid+1, right); int w = wait[(t+x) % LCM][mid+1]; //printf("WAIT %d %d %d\n", t+x, mid+1, w); return x+w+check(tree, wait, (t+x+w) % LCM, a, mid, rev, 2*node, left, mid); } } } int main() { int N,M,Q; //w dol, w prawo, zapytania char mode[LCM+2]; std::vector<std::vector<std::bitset<LCM> > > signals; memset(mode, 0, LCM+1); scanf("%d %d %d\n", &N, &M, &Q); signals.resize(N+1); for (int i=1; i<=N; ++i) { signals[i].resize(M+1); for (int j=1; j<=M; j++) { scanf("%s",mode); int len = strlen(mode); while (len < LCM) { int s = std::min(len, LCM-len); memcpy(mode+len, mode, s); len += s; } signals[i][j] = std::bitset<LCM>(mode); } } for (int i=0; i<LCM; ++i) { horizontal[i].resize(M+1, false); vertical[i].resize(N+1, false); horizontal_wait[i].resize(M+1, 0); vertical_wait[i].resize(N+1, 0); } //gdzie sa pionowe barykady (same zera w kolumnie) for (int i=1; i<=M; ++i) { std::bitset<LCM> value = ZEROS; for (int j=1; j<=N; ++j) { value |= signals[j][i]; } for (int b=0; b<LCM; b++) { horizontal[b][i] = !value.test(LCM-b-1); } } //gdzie poziome (same jedynki w rzedzie) for (int i=1; i<=N; ++i) { std::bitset<LCM> value = ONES; for (int j=1; j<=M; ++j) { value &= signals[i][j]; } for (int b=0; b<LCM; b++) { vertical[b][i] = value.test(LCM-b-1); } } //kiedy pierwszy wolny update_wait(horizontal_wait, horizontal); update_wait(vertical_wait, vertical); //budowanie drzewka build_tree(horizontal_tree, horizontal_tree_rev, horizontal_wait, 1, 0, MELEM-1); build_tree(vertical_tree, vertical_tree_rev, vertical_wait, 1, 0, MELEM-1); //zapytania for (int i=0; i<Q; ++i) { int t,a,b,c,d; scanf("%d %d %d %d %d", &t, &a, &b, &c, &d); int res = 0; if (c>a) { res = check(vertical_tree, vertical_wait, t % LCM, a, c, false, 1, 0, MELEM-1); } else if (a>c) { res = check(vertical_tree_rev, vertical_wait, t % LCM, c, a, true, 1, 0, MELEM-1); } int res2 = 0; if (d>b) { res2 = check(horizontal_tree, horizontal_wait, t % LCM, b, d, false, 1, 0, MELEM-1); } else if (b>d) { res2 = check(horizontal_tree_rev, horizontal_wait, t % LCM, d, b, true, 1, 0, MELEM-1); } printf("%d\n", t+std::max(res, res2)); } 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 | #include <cstdio> #include <cstring> #include <bitset> #include <vector> const int LCM = 840; const int MELEM = 1<<14; const int TSIZE = 2*MELEM; std::bitset<LCM> ZEROS("000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000"); std::bitset<LCM> ONES("111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111"); std::vector<bool> horizontal[LCM]; std::vector<bool> vertical[LCM]; std::vector<int> horizontal_wait[LCM]; std::vector<int> vertical_wait[LCM]; int horizontal_tree[LCM][TSIZE]; int vertical_tree[LCM][TSIZE]; int horizontal_tree_rev[LCM][TSIZE]; int vertical_tree_rev[LCM][TSIZE]; void update_wait(std::vector<int> wait[LCM], const std::vector<bool> blocked[LCM]) { int N = wait[0].size()-1; for (int i=1; i<=N; ++i) { int cnt = 1; while (blocked[cnt-1][i]) { cnt++; } for (int ts=LCM-1; ts>=0; ts--) { if (blocked[ts][i] == false) { wait[ts][i] = 0; cnt = 1; } else { wait[ts][i] = cnt; cnt++; } } } } void build_tree(int tree[LCM][TSIZE], int revtree[LCM][TSIZE], const std::vector<int> wait[LCM], int node, int left, int right) { int N = wait[0].size()-1; if (left >= N) { return; } if (left == right) { for (int i=0; i<LCM; ++i) { tree[i][node] = 0; revtree[i][node] = 0; } return; } build_tree(tree, revtree, wait, 2*node, left, (left+right)/2); build_tree(tree, revtree, wait, 2*node+1, (left+right)/2 + 1, right); for (int i=0; i<LCM; ++i) { int mid = (left+right)/2; int x = tree[i][2*node]; int w = 0; if (mid+1 <= N) { w = wait[(i+x) % LCM][mid+1]; } tree[i][node] = x+w+tree[(i+x+w) % LCM][2*node+1]; mid = (left+right)/2 + 1; x = revtree[i][2*node+1]; w = 0; if (mid <= N) { w = wait[(i+x) % LCM][mid]; } revtree[i][node] = x+w+revtree[(i+x+w) % LCM][2*node]; } } int check(int tree[LCM][TSIZE], std::vector<int> wait[LCM], int t, int a, int b, bool rev, int node, int left, int right) { //printf("CHECK a=%d b=%d left=%d right=%d val=%d\n", a, b, left, right, tree[t][node]); if (a>=right || left>=b) { return 0; } if (left == right || a == b) { return 0; } else if (b-a == 1) { //printf("SINGLE t=%d a=%d b=%d %d %d\n", t, a, b, rev?a:b, wait[t % LCM][rev?b:a]); return wait[t % LCM][b]; } else if (a==left && b==right) { return tree[t % LCM][node]; } else { int mid = (left+right)/2; if (b<=mid) { return check(tree, wait, t, a, b, rev, 2*node, left, mid); } else if (a>=mid+1) { return check(tree, wait, t, a, b, rev, 2*node+1, mid+1, right); } if (!rev) { int x = check(tree, wait, t, a, mid, rev, 2*node, left, mid); int w = wait[(t+x) % LCM][mid+1]; //printf("WAIT %d %d %d\n", t+x, mid+1, w); return x+w+check(tree, wait, (t+x+w) % LCM, mid+1, b, rev, 2*node+1, mid+1, right); } else { int x = check(tree, wait, t, mid+1, b, rev, 2*node+1, mid+1, right); int w = wait[(t+x) % LCM][mid+1]; //printf("WAIT %d %d %d\n", t+x, mid+1, w); return x+w+check(tree, wait, (t+x+w) % LCM, a, mid, rev, 2*node, left, mid); } } } int main() { int N,M,Q; //w dol, w prawo, zapytania char mode[LCM+2]; std::vector<std::vector<std::bitset<LCM> > > signals; memset(mode, 0, LCM+1); scanf("%d %d %d\n", &N, &M, &Q); signals.resize(N+1); for (int i=1; i<=N; ++i) { signals[i].resize(M+1); for (int j=1; j<=M; j++) { scanf("%s",mode); int len = strlen(mode); while (len < LCM) { int s = std::min(len, LCM-len); memcpy(mode+len, mode, s); len += s; } signals[i][j] = std::bitset<LCM>(mode); } } for (int i=0; i<LCM; ++i) { horizontal[i].resize(M+1, false); vertical[i].resize(N+1, false); horizontal_wait[i].resize(M+1, 0); vertical_wait[i].resize(N+1, 0); } //gdzie sa pionowe barykady (same zera w kolumnie) for (int i=1; i<=M; ++i) { std::bitset<LCM> value = ZEROS; for (int j=1; j<=N; ++j) { value |= signals[j][i]; } for (int b=0; b<LCM; b++) { horizontal[b][i] = !value.test(LCM-b-1); } } //gdzie poziome (same jedynki w rzedzie) for (int i=1; i<=N; ++i) { std::bitset<LCM> value = ONES; for (int j=1; j<=M; ++j) { value &= signals[i][j]; } for (int b=0; b<LCM; b++) { vertical[b][i] = value.test(LCM-b-1); } } //kiedy pierwszy wolny update_wait(horizontal_wait, horizontal); update_wait(vertical_wait, vertical); //budowanie drzewka build_tree(horizontal_tree, horizontal_tree_rev, horizontal_wait, 1, 0, MELEM-1); build_tree(vertical_tree, vertical_tree_rev, vertical_wait, 1, 0, MELEM-1); //zapytania for (int i=0; i<Q; ++i) { int t,a,b,c,d; scanf("%d %d %d %d %d", &t, &a, &b, &c, &d); int res = 0; if (c>a) { res = check(vertical_tree, vertical_wait, t % LCM, a, c, false, 1, 0, MELEM-1); } else if (a>c) { res = check(vertical_tree_rev, vertical_wait, t % LCM, c, a, true, 1, 0, MELEM-1); } int res2 = 0; if (d>b) { res2 = check(horizontal_tree, horizontal_wait, t % LCM, b, d, false, 1, 0, MELEM-1); } else if (b>d) { res2 = check(horizontal_tree_rev, horizontal_wait, t % LCM, d, b, true, 1, 0, MELEM-1); } printf("%d\n", t+std::max(res, res2)); } return 0; } |