#include <bits/stdc++.h> using namespace std; struct Query { int time; int a, b; int c, d; }; struct Timings { int timings_size; bitset<8> timings; }; namespace wzor { struct JumpInstructions { int where_to_jump; int how_much_to_wait; }; struct BigJumpInstructions { int where_to_jump; int how_much_to_wait_to_get_here; int how_much_to_wait_for_next_jump; }; } vector<int> solve_wzor(int n, int m, vector<vector<Timings>> crossings, vector<Query> queries) { using namespace wzor; vector<array<bitset<8>, 9>> x_pos(m, {0, 0, 0, 0, 0, 0, 0, 0, 0}); vector<array<bitset<8>, 9>> y_pos(n, {0, 0, 0, 0, 0, 0, 0, 0, 0}); for(int i = 0; i < n; ++i) { for(int j = 0; j < m; ++j) { x_pos[j][crossings[i][j].timings_size] |= crossings[i][j].timings; crossings[i][j].timings.flip(); y_pos[i][crossings[i][j].timings_size] |= crossings[i][j].timings; } } const int lcm = 840; vector<bitset<lcm>> timings_without_secrets_x(n), timings_without_secrets_y(m); for(int i = 0; i < m; ++i) { for(int j = 2; j <= 8; ++j) { for(int k = 0; k < lcm; ++k) { if(x_pos[i][j][k % j]) timings_without_secrets_x[i][k] = true; } } } for(int i = 0; i < n; ++i) { for(int j = 2; j <= 8; ++j) { for(int k = 0; k < lcm; ++k) { if(y_pos[i][j][k % j]) timings_without_secrets_y[i][k] = true; } } } //for(int i = 0; i < n; ++i) cout << timings_without_secrets_y[i][1] << ' '; //cout << '\n'; vector<int> times(queries.size(), 0); for(int i = 0; i < (int)queries.size(); ++i) times[i] = queries[i].time; for(int iters = 0; iters < 4; ++iters) { const int BIG_JUMP_SIZE = 128; vector<vector<JumpInstructions>> small_jumps(lcm, vector<JumpInstructions>(m + 1)); vector<vector<BigJumpInstructions>> big_jumps(lcm, vector<BigJumpInstructions>((m + 1) / BIG_JUMP_SIZE + 10)); //Calculate small jumps for(int time = 0; time < lcm; ++time) { small_jumps[time][m] = {.where_to_jump = m, .how_much_to_wait = 0}; for(int pos = m - 1; pos >= 0; --pos) { if(timings_without_secrets_y[pos][time] == true) { small_jumps[time][pos] = small_jumps[time][pos + 1]; } else { ///We need to find new spot to jump int waiting_time = 0; int index = time; while(timings_without_secrets_y[pos][index] == false) { ++waiting_time; ++index; if(index == lcm) index = 0; } small_jumps[time][pos].where_to_jump = pos; small_jumps[time][pos].how_much_to_wait = waiting_time; } /*if(time == 1 && iters == 1) { cout << pos << ' ' << small_jumps[time][pos].where_to_jump << ' ' << small_jumps[time][pos].how_much_to_wait << '\n'; }*/ } } for(int jump_num = 0; jump_num * BIG_JUMP_SIZE <= m; ++jump_num) { for(int time = 0; time < lcm; ++time) { auto current_jump = small_jumps[time][jump_num * BIG_JUMP_SIZE]; big_jumps[time][jump_num] = {.where_to_jump = current_jump.where_to_jump, .how_much_to_wait_to_get_here = 0, .how_much_to_wait_for_next_jump = current_jump.how_much_to_wait}; while(big_jumps[time][jump_num].where_to_jump < min(m, (jump_num + 1) * BIG_JUMP_SIZE)) { auto prev_jump = big_jumps[time][jump_num]; auto& next_jump = small_jumps[(time + prev_jump.how_much_to_wait_to_get_here + prev_jump.how_much_to_wait_for_next_jump) % lcm][prev_jump.where_to_jump]; big_jumps[time][jump_num] = { .where_to_jump = next_jump.where_to_jump, .how_much_to_wait_to_get_here = prev_jump.how_much_to_wait_for_next_jump + prev_jump.how_much_to_wait_to_get_here, .how_much_to_wait_for_next_jump = next_jump.how_much_to_wait, }; } } } for(int i = 0; i < (int)queries.size(); ++i) { auto& query = queries[i]; //cout << i << ' ' << iters << ' ' << query.a << ' ' << query.b << ' ' << query.c << ' ' << query.d << '\n'; if(query.a < query.c) { int current_time = query.time; int current_pos = query.a; //if(i == 1) cout << "BEGIN: " << current_pos << ' ' << current_time << '\n'; while(true) { auto & current_big_jump = big_jumps[current_time % lcm][current_pos / BIG_JUMP_SIZE]; if(current_big_jump.where_to_jump < query.b) { current_pos = current_big_jump.where_to_jump; current_time += current_big_jump.how_much_to_wait_to_get_here; //if(i == 1) cout << "BIG JUMP: " << current_pos << ' ' << current_time << '\n'; } else break; } while(true) { auto& current_jump = small_jumps[current_time % lcm][current_pos]; if(current_pos < query.c) { //if(i == 1) cout << "PRE JUMP: " << current_pos << ' ' << current_time << ' ' << iters << '\n'; int wait = current_jump.how_much_to_wait; current_time += wait; current_pos = small_jumps[current_time % lcm][current_pos].where_to_jump; //if(i == 1) cout << "SMALL JUMP: " << current_pos << ' ' << current_time << ' ' << wait << '\n'; } else break; } times[i] = max(current_time, times[i]); } swap(query.a, query.b); swap(query.c, query.d); query.b = n - query.b; query.d = n - query.d; } swap(m, n); swap(timings_without_secrets_x, timings_without_secrets_y); reverse(timings_without_secrets_x.begin(), timings_without_secrets_x.end()); } return times; } #include <bits/stdc++.h> using namespace std; char text[100]; int main() { int n, m, q; std::cin >> n >> m >> q; vector<vector<Timings>> input(n, vector<Timings>(m)); for(int i = 0; i < n; ++i) { for(int j = 0; j < m; ++j) { cin >> text; int c = strlen(text); input[i][j].timings_size = c; for(int k = 0; k < c; ++k) input[i][j].timings[k] = text[k] == '1'; } } vector<Query> queries(q); for(int i = 0; i < q; ++i) { cin >> queries[i].time >> queries[i].a >> queries[i].b >> queries[i].c >> queries[i].d; } auto Result = solve_wzor(n, m, input, queries); for(auto p : Result) { cout << p << '\n'; } }
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 | #include <bits/stdc++.h> using namespace std; struct Query { int time; int a, b; int c, d; }; struct Timings { int timings_size; bitset<8> timings; }; namespace wzor { struct JumpInstructions { int where_to_jump; int how_much_to_wait; }; struct BigJumpInstructions { int where_to_jump; int how_much_to_wait_to_get_here; int how_much_to_wait_for_next_jump; }; } vector<int> solve_wzor(int n, int m, vector<vector<Timings>> crossings, vector<Query> queries) { using namespace wzor; vector<array<bitset<8>, 9>> x_pos(m, {0, 0, 0, 0, 0, 0, 0, 0, 0}); vector<array<bitset<8>, 9>> y_pos(n, {0, 0, 0, 0, 0, 0, 0, 0, 0}); for(int i = 0; i < n; ++i) { for(int j = 0; j < m; ++j) { x_pos[j][crossings[i][j].timings_size] |= crossings[i][j].timings; crossings[i][j].timings.flip(); y_pos[i][crossings[i][j].timings_size] |= crossings[i][j].timings; } } const int lcm = 840; vector<bitset<lcm>> timings_without_secrets_x(n), timings_without_secrets_y(m); for(int i = 0; i < m; ++i) { for(int j = 2; j <= 8; ++j) { for(int k = 0; k < lcm; ++k) { if(x_pos[i][j][k % j]) timings_without_secrets_x[i][k] = true; } } } for(int i = 0; i < n; ++i) { for(int j = 2; j <= 8; ++j) { for(int k = 0; k < lcm; ++k) { if(y_pos[i][j][k % j]) timings_without_secrets_y[i][k] = true; } } } //for(int i = 0; i < n; ++i) cout << timings_without_secrets_y[i][1] << ' '; //cout << '\n'; vector<int> times(queries.size(), 0); for(int i = 0; i < (int)queries.size(); ++i) times[i] = queries[i].time; for(int iters = 0; iters < 4; ++iters) { const int BIG_JUMP_SIZE = 128; vector<vector<JumpInstructions>> small_jumps(lcm, vector<JumpInstructions>(m + 1)); vector<vector<BigJumpInstructions>> big_jumps(lcm, vector<BigJumpInstructions>((m + 1) / BIG_JUMP_SIZE + 10)); //Calculate small jumps for(int time = 0; time < lcm; ++time) { small_jumps[time][m] = {.where_to_jump = m, .how_much_to_wait = 0}; for(int pos = m - 1; pos >= 0; --pos) { if(timings_without_secrets_y[pos][time] == true) { small_jumps[time][pos] = small_jumps[time][pos + 1]; } else { ///We need to find new spot to jump int waiting_time = 0; int index = time; while(timings_without_secrets_y[pos][index] == false) { ++waiting_time; ++index; if(index == lcm) index = 0; } small_jumps[time][pos].where_to_jump = pos; small_jumps[time][pos].how_much_to_wait = waiting_time; } /*if(time == 1 && iters == 1) { cout << pos << ' ' << small_jumps[time][pos].where_to_jump << ' ' << small_jumps[time][pos].how_much_to_wait << '\n'; }*/ } } for(int jump_num = 0; jump_num * BIG_JUMP_SIZE <= m; ++jump_num) { for(int time = 0; time < lcm; ++time) { auto current_jump = small_jumps[time][jump_num * BIG_JUMP_SIZE]; big_jumps[time][jump_num] = {.where_to_jump = current_jump.where_to_jump, .how_much_to_wait_to_get_here = 0, .how_much_to_wait_for_next_jump = current_jump.how_much_to_wait}; while(big_jumps[time][jump_num].where_to_jump < min(m, (jump_num + 1) * BIG_JUMP_SIZE)) { auto prev_jump = big_jumps[time][jump_num]; auto& next_jump = small_jumps[(time + prev_jump.how_much_to_wait_to_get_here + prev_jump.how_much_to_wait_for_next_jump) % lcm][prev_jump.where_to_jump]; big_jumps[time][jump_num] = { .where_to_jump = next_jump.where_to_jump, .how_much_to_wait_to_get_here = prev_jump.how_much_to_wait_for_next_jump + prev_jump.how_much_to_wait_to_get_here, .how_much_to_wait_for_next_jump = next_jump.how_much_to_wait, }; } } } for(int i = 0; i < (int)queries.size(); ++i) { auto& query = queries[i]; //cout << i << ' ' << iters << ' ' << query.a << ' ' << query.b << ' ' << query.c << ' ' << query.d << '\n'; if(query.a < query.c) { int current_time = query.time; int current_pos = query.a; //if(i == 1) cout << "BEGIN: " << current_pos << ' ' << current_time << '\n'; while(true) { auto & current_big_jump = big_jumps[current_time % lcm][current_pos / BIG_JUMP_SIZE]; if(current_big_jump.where_to_jump < query.b) { current_pos = current_big_jump.where_to_jump; current_time += current_big_jump.how_much_to_wait_to_get_here; //if(i == 1) cout << "BIG JUMP: " << current_pos << ' ' << current_time << '\n'; } else break; } while(true) { auto& current_jump = small_jumps[current_time % lcm][current_pos]; if(current_pos < query.c) { //if(i == 1) cout << "PRE JUMP: " << current_pos << ' ' << current_time << ' ' << iters << '\n'; int wait = current_jump.how_much_to_wait; current_time += wait; current_pos = small_jumps[current_time % lcm][current_pos].where_to_jump; //if(i == 1) cout << "SMALL JUMP: " << current_pos << ' ' << current_time << ' ' << wait << '\n'; } else break; } times[i] = max(current_time, times[i]); } swap(query.a, query.b); swap(query.c, query.d); query.b = n - query.b; query.d = n - query.d; } swap(m, n); swap(timings_without_secrets_x, timings_without_secrets_y); reverse(timings_without_secrets_x.begin(), timings_without_secrets_x.end()); } return times; } #include <bits/stdc++.h> using namespace std; char text[100]; int main() { int n, m, q; std::cin >> n >> m >> q; vector<vector<Timings>> input(n, vector<Timings>(m)); for(int i = 0; i < n; ++i) { for(int j = 0; j < m; ++j) { cin >> text; int c = strlen(text); input[i][j].timings_size = c; for(int k = 0; k < c; ++k) input[i][j].timings[k] = text[k] == '1'; } } vector<Query> queries(q); for(int i = 0; i < q; ++i) { cin >> queries[i].time >> queries[i].a >> queries[i].b >> queries[i].c >> queries[i].d; } auto Result = solve_wzor(n, m, input, queries); for(auto p : Result) { cout << p << '\n'; } } |