#include <iostream> #include <fstream> #include <vector> #include <algorithm> #include <map> #include <array> #include <string> #include <set> #include <optional> #include <queue> #include <stack> #include <array> #include <cstdlib> #include <ctime> #include <unordered_set> #include <unordered_map> #include <unistd.h> using namespace std; #ifdef USE_CERR_LOG #define LOG if (true) cerr const bool LogEnabled = true; #else #define LOG if (false) cerr const bool LogEnabled = false; #endif bool LogBigEnabled = true; #ifdef USE_FILE_CIN ifstream fin("../oiw.in"); #define cin fin #endif typedef unsigned uint; typedef long long ll; typedef unsigned long long ull; typedef pair<uint, uint> Point; namespace std { template<> struct hash<Point> { hash<uint> uintHash; size_t operator () (const Point& point) const noexcept { uint seed = uintHash(point.first); return uintHash(point.second) + 0x9e3779b9 + (seed << 6) + (seed >> 2); } }; } struct Path { unordered_set<Point> points; }; uint nRows; uint mCols; uint kAttacks; unordered_set<Point> strongholds; vector<Path> initialPaths; Path lastPath; ostream& separator(ostream& out) { return out << "--------------------------------------------" << endl; } ostream& operator << (ostream& out, const Path& path) { for (const Point& point : path.points) { out << "(" << point.first << "," << point.second << ") "; } return out << endl; } ostream& operator << (ostream& out, const vector<Path>& paths) { if (LogBigEnabled) { for (const auto& path : paths) { out << path; } } return out << separator; } void initializePaths() { uint pathSum = nRows + mCols; uint pathCount = min({500u, 10'000'000 / pathSum, 2 * pathSum}); double threshold = (double) nRows / pathSum; srand(time(nullptr)); for (uint i=0; i<pathCount; i++) { initialPaths.push_back({}); Path& path = initialPaths.back(); path.points.reserve(pathSum); Point position = {0, 0}; for (uint step=0; step<nRows+mCols-2; step++) { if (!(position.first == nRows - 1) && (position.second == mCols - 1 || (double)rand()/RAND_MAX < threshold)) { position.first ++; } else { position.second ++; } path.points.insert(position); } } LOG << initialPaths; } inline bool isPointOnPath(const Point& point, const Path& path) { return path.points.count(point) > 0; } optional<Path> computePathDfs() { Point start = {0, 0}; Point end = {nRows-1, mCols-1}; unordered_set<Point> visited; unordered_map<Point, Point> prevs; stack<Point> sta; bool found = false; sta.push(start); while (!sta.empty() && !found) { Point parent = sta.top(); sta.pop(); if (visited.count(parent) == 0) { visited.insert(parent); } array<Point, 2> nexts { Point{parent.first+1, parent.second}, Point{parent.first, parent.second+1} }; for (const Point& p : nexts) { if (p.first<nRows && p.second<mCols && strongholds.count(p)==0) { prevs[p] = parent; sta.push(p); found = p == end; } } if (LogEnabled && (rand() % 10000 == 0)) { LOG << visited.size() << "," << prevs.size() << " "; } } if (found) { Path path; for (Point p = prevs[end]; p != start; p = prevs[p]) { path.points.insert(p); } return path; } else { return nullopt; } } optional<Path> computePathBfs() { Point start = {0, 0}; Point end = {nRows-1, mCols-1}; unordered_map<Point, Point> prevs; unordered_set<Point> visited; queue<Point> que; bool found = false; visited.insert(start); que.push(start); while (!que.empty() && !found) { Point parent = que.front(); que.pop(); array<Point, 2> nexts { Point{parent.first+1, parent.second}, Point{parent.first, parent.second+1} }; for (const Point& p : nexts) { if (p.first<nRows && p.second<mCols && visited.count(p)==0 && strongholds.count(p)==0) { prevs[p] = parent; visited.insert(p); que.push(p); found = p == end; } } if (LogEnabled && (rand() % 10000 == 0)) { LOG << visited.size() << "," << prevs.size() << " "; } } if (found) { Path path; for (Point p = prevs[end]; p != start; p = prevs[p]) { path.points.insert(p); } return make_optional(path); } else { return nullopt; } } bool shouldDestroy(ull row, ull col) { Point point {row, col}; vector<uint> affectedPathsIdx; bool toDestroy; for (uint i=0; i < initialPaths.size(); i++) { const Path& path = initialPaths[i]; if (isPointOnPath(point, path)) { affectedPathsIdx.push_back(i); } } if (affectedPathsIdx.size() < initialPaths.size()) { for (int i=affectedPathsIdx.size()-1; i >= 0; i--) { initialPaths.erase(initialPaths.begin() + affectedPathsIdx[i]); } strongholds.insert(point); toDestroy = false; } else { strongholds.insert(point); auto optionalPath = computePathDfs(); if (optionalPath.has_value()) { initialPaths.clear(); initialPaths.push_back(optionalPath.value()); toDestroy = false; } else { strongholds.erase(point); toDestroy = true; } } LOG << "destroy: " << toDestroy << endl << initialPaths; return toDestroy; } void play() { ull r, c, z, x; x = 0; for (uint i=0; i < kAttacks; i++) { cin >> r >> c >> z; uint row = (r ^ x) % nRows; uint col = (c ^ x) % mCols; LOG << "attack: " << row << ", " << col << endl; if (shouldDestroy(row, col)) { cout << "TAK" << endl; x = x ^ z; } else { cout << "NIE" << endl; } } } int main() { ios_base::sync_with_stdio(false); cin >> nRows >> mCols >> kAttacks; LogBigEnabled = nRows + mCols < 100; initializePaths(); play(); 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 | #include <iostream> #include <fstream> #include <vector> #include <algorithm> #include <map> #include <array> #include <string> #include <set> #include <optional> #include <queue> #include <stack> #include <array> #include <cstdlib> #include <ctime> #include <unordered_set> #include <unordered_map> #include <unistd.h> using namespace std; #ifdef USE_CERR_LOG #define LOG if (true) cerr const bool LogEnabled = true; #else #define LOG if (false) cerr const bool LogEnabled = false; #endif bool LogBigEnabled = true; #ifdef USE_FILE_CIN ifstream fin("../oiw.in"); #define cin fin #endif typedef unsigned uint; typedef long long ll; typedef unsigned long long ull; typedef pair<uint, uint> Point; namespace std { template<> struct hash<Point> { hash<uint> uintHash; size_t operator () (const Point& point) const noexcept { uint seed = uintHash(point.first); return uintHash(point.second) + 0x9e3779b9 + (seed << 6) + (seed >> 2); } }; } struct Path { unordered_set<Point> points; }; uint nRows; uint mCols; uint kAttacks; unordered_set<Point> strongholds; vector<Path> initialPaths; Path lastPath; ostream& separator(ostream& out) { return out << "--------------------------------------------" << endl; } ostream& operator << (ostream& out, const Path& path) { for (const Point& point : path.points) { out << "(" << point.first << "," << point.second << ") "; } return out << endl; } ostream& operator << (ostream& out, const vector<Path>& paths) { if (LogBigEnabled) { for (const auto& path : paths) { out << path; } } return out << separator; } void initializePaths() { uint pathSum = nRows + mCols; uint pathCount = min({500u, 10'000'000 / pathSum, 2 * pathSum}); double threshold = (double) nRows / pathSum; srand(time(nullptr)); for (uint i=0; i<pathCount; i++) { initialPaths.push_back({}); Path& path = initialPaths.back(); path.points.reserve(pathSum); Point position = {0, 0}; for (uint step=0; step<nRows+mCols-2; step++) { if (!(position.first == nRows - 1) && (position.second == mCols - 1 || (double)rand()/RAND_MAX < threshold)) { position.first ++; } else { position.second ++; } path.points.insert(position); } } LOG << initialPaths; } inline bool isPointOnPath(const Point& point, const Path& path) { return path.points.count(point) > 0; } optional<Path> computePathDfs() { Point start = {0, 0}; Point end = {nRows-1, mCols-1}; unordered_set<Point> visited; unordered_map<Point, Point> prevs; stack<Point> sta; bool found = false; sta.push(start); while (!sta.empty() && !found) { Point parent = sta.top(); sta.pop(); if (visited.count(parent) == 0) { visited.insert(parent); } array<Point, 2> nexts { Point{parent.first+1, parent.second}, Point{parent.first, parent.second+1} }; for (const Point& p : nexts) { if (p.first<nRows && p.second<mCols && strongholds.count(p)==0) { prevs[p] = parent; sta.push(p); found = p == end; } } if (LogEnabled && (rand() % 10000 == 0)) { LOG << visited.size() << "," << prevs.size() << " "; } } if (found) { Path path; for (Point p = prevs[end]; p != start; p = prevs[p]) { path.points.insert(p); } return path; } else { return nullopt; } } optional<Path> computePathBfs() { Point start = {0, 0}; Point end = {nRows-1, mCols-1}; unordered_map<Point, Point> prevs; unordered_set<Point> visited; queue<Point> que; bool found = false; visited.insert(start); que.push(start); while (!que.empty() && !found) { Point parent = que.front(); que.pop(); array<Point, 2> nexts { Point{parent.first+1, parent.second}, Point{parent.first, parent.second+1} }; for (const Point& p : nexts) { if (p.first<nRows && p.second<mCols && visited.count(p)==0 && strongholds.count(p)==0) { prevs[p] = parent; visited.insert(p); que.push(p); found = p == end; } } if (LogEnabled && (rand() % 10000 == 0)) { LOG << visited.size() << "," << prevs.size() << " "; } } if (found) { Path path; for (Point p = prevs[end]; p != start; p = prevs[p]) { path.points.insert(p); } return make_optional(path); } else { return nullopt; } } bool shouldDestroy(ull row, ull col) { Point point {row, col}; vector<uint> affectedPathsIdx; bool toDestroy; for (uint i=0; i < initialPaths.size(); i++) { const Path& path = initialPaths[i]; if (isPointOnPath(point, path)) { affectedPathsIdx.push_back(i); } } if (affectedPathsIdx.size() < initialPaths.size()) { for (int i=affectedPathsIdx.size()-1; i >= 0; i--) { initialPaths.erase(initialPaths.begin() + affectedPathsIdx[i]); } strongholds.insert(point); toDestroy = false; } else { strongholds.insert(point); auto optionalPath = computePathDfs(); if (optionalPath.has_value()) { initialPaths.clear(); initialPaths.push_back(optionalPath.value()); toDestroy = false; } else { strongholds.erase(point); toDestroy = true; } } LOG << "destroy: " << toDestroy << endl << initialPaths; return toDestroy; } void play() { ull r, c, z, x; x = 0; for (uint i=0; i < kAttacks; i++) { cin >> r >> c >> z; uint row = (r ^ x) % nRows; uint col = (c ^ x) % mCols; LOG << "attack: " << row << ", " << col << endl; if (shouldDestroy(row, col)) { cout << "TAK" << endl; x = x ^ z; } else { cout << "NIE" << endl; } } } int main() { ios_base::sync_with_stdio(false); cin >> nRows >> mCols >> kAttacks; LogBigEnabled = nRows + mCols < 100; initializePaths(); play(); return 0; } |