#include <bitset> #include <iostream> #include <vector> enum class OperationType { Sum = 1, And = 2, Neg = 3 }; constexpr int MAX_N = 50000; constexpr int MAX_M = 400000; class InputParams { public: int n{}; int m{}; int q{}; }; InputParams loadData() { InputParams params; std::cin >> params.n; std::cin >> params.m; std::cin >> params.q; return params; } void loadNegOperation(std::vector<std::bitset<MAX_N>> &sets) { int x; std::cin >> x; sets.emplace_back(); sets[sets.size() - 1] = sets[x - 1]; sets[sets.size() - 1].flip(); } void loadAndOperation(std::vector<std::bitset<MAX_N>> &sets) { int x, y; std::cin >> x >> y; sets.emplace_back(); sets[sets.size() - 1] = sets[x - 1] & sets[y - 1]; } void loadOrOperation(std::vector<std::bitset<MAX_N>> &sets) { int x, y; std::cin >> x >> y; sets.emplace_back(); sets[sets.size() - 1] = sets[x - 1] | sets[y - 1]; } void loadNextOperation(std::vector<std::bitset<MAX_N>> &sets) { int type; std::cin >> type; if (type == 1) { loadOrOperation(sets); } else if (type == 2) { loadAndOperation(sets); } else { loadNegOperation(sets); } } void loadOperations(std::vector<std::bitset<MAX_N>> &sets, const InputParams ¶ms) { for(int m = 0; m < params.m; m++) { loadNextOperation(sets); } } void answerNextQuery(std::vector<std::bitset<MAX_N>> &sets) { int x, v; std::cin >> x >> v; bool doesXIncludeV = sets[x-1].test(v-1); std::cout << (doesXIncludeV ? "TAK\n" : "NIE\n"); } void answerQueries(std::vector<std::bitset<MAX_N>> &sets, const InputParams ¶ms) { for(int q = 0; q < params.q; q++) { answerNextQuery(sets); } } void loadInitialSets(std::vector<std::bitset<MAX_N>> &sets, const InputParams ¶ms) { sets.emplace_back(); sets[0].set(); for (int n = 2; n <= params.n; n++) { sets.emplace_back(); std::bitset<MAX_N> ¤tSet = sets[n - 1]; for (int i = n; i <= MAX_N; i += n) { currentSet.set(i - 1); } } } int main() { std::vector<std::bitset<MAX_N>> sets; sets.reserve(MAX_M + MAX_N); std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); InputParams params = loadData(); loadInitialSets(sets, params); loadOperations(sets, params); answerQueries(sets, params); 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 | #include <bitset> #include <iostream> #include <vector> enum class OperationType { Sum = 1, And = 2, Neg = 3 }; constexpr int MAX_N = 50000; constexpr int MAX_M = 400000; class InputParams { public: int n{}; int m{}; int q{}; }; InputParams loadData() { InputParams params; std::cin >> params.n; std::cin >> params.m; std::cin >> params.q; return params; } void loadNegOperation(std::vector<std::bitset<MAX_N>> &sets) { int x; std::cin >> x; sets.emplace_back(); sets[sets.size() - 1] = sets[x - 1]; sets[sets.size() - 1].flip(); } void loadAndOperation(std::vector<std::bitset<MAX_N>> &sets) { int x, y; std::cin >> x >> y; sets.emplace_back(); sets[sets.size() - 1] = sets[x - 1] & sets[y - 1]; } void loadOrOperation(std::vector<std::bitset<MAX_N>> &sets) { int x, y; std::cin >> x >> y; sets.emplace_back(); sets[sets.size() - 1] = sets[x - 1] | sets[y - 1]; } void loadNextOperation(std::vector<std::bitset<MAX_N>> &sets) { int type; std::cin >> type; if (type == 1) { loadOrOperation(sets); } else if (type == 2) { loadAndOperation(sets); } else { loadNegOperation(sets); } } void loadOperations(std::vector<std::bitset<MAX_N>> &sets, const InputParams ¶ms) { for(int m = 0; m < params.m; m++) { loadNextOperation(sets); } } void answerNextQuery(std::vector<std::bitset<MAX_N>> &sets) { int x, v; std::cin >> x >> v; bool doesXIncludeV = sets[x-1].test(v-1); std::cout << (doesXIncludeV ? "TAK\n" : "NIE\n"); } void answerQueries(std::vector<std::bitset<MAX_N>> &sets, const InputParams ¶ms) { for(int q = 0; q < params.q; q++) { answerNextQuery(sets); } } void loadInitialSets(std::vector<std::bitset<MAX_N>> &sets, const InputParams ¶ms) { sets.emplace_back(); sets[0].set(); for (int n = 2; n <= params.n; n++) { sets.emplace_back(); std::bitset<MAX_N> ¤tSet = sets[n - 1]; for (int i = n; i <= MAX_N; i += n) { currentSet.set(i - 1); } } } int main() { std::vector<std::bitset<MAX_N>> sets; sets.reserve(MAX_M + MAX_N); std::ios_base::sync_with_stdio(false); std::cin.tie(nullptr); InputParams params = loadData(); loadInitialSets(sets, params); loadOperations(sets, params); answerQueries(sets, params); return 0; } |