#include <iostream> #include <vector> #include <set> #include <algorithm> #include <iterator> std::vector<std::set<int>> generateInitialSets(int n) { std::vector<std::set<int>> result{}; for(int i=1; i<=n; i++) { std::set<int> newSet{}; for(int j=i; j<=n; j+=i) { newSet.insert(j); } result.push_back(newSet); } return result; } std::set<int> performSum(const std::vector<std::set<int>>& sets) { int i,j; std::cin >> i >> j; std::set<int> result; std::set_union(sets[i-1].begin(), sets[i-1].end(), sets[j-1].begin(), sets[j-1].end(), std::inserter(result, result.begin())); return result; } std::set<int> performIntersection(const std::vector<std::set<int>>& sets) { int i,j; std::cin >> i >> j; std::set<int> result; std::set_intersection(sets[i-1].begin(), sets[i-1].end(), sets[j-1].begin(), sets[j-1].end(), std::inserter(result, result.begin())); return result; } std::set<int> performNegate(const std::vector<std::set<int>>& sets, int n) { int i; std::cin >> i; std::set<int> result; std::set_difference(sets[0].begin(), sets[0].end(), sets[i-1].begin(), sets[i-1].end(), std::inserter(result, result.begin())); return result; } std::set<int> createNewSet(int operation, const std::vector<std::set<int>>& sets, int n) { switch (operation) { case 1: return performSum(sets); case 2: return performIntersection(sets); case 3: return performNegate(sets, n); default: std::cout << "unrecognized option " << operation << std::endl; return {}; } } int main() { int n, m, q; std::cin >> n >> m >> q; //std::cout << "First line read.\n"; auto sets = generateInitialSets(n); //std::cout << "Sets generated"; int opeartion; for(int i=0; i<m; i++) { std::cin >> opeartion; //std::cout << "Operation read.\n"; sets.push_back(createNewSet(opeartion, sets, n)); //std::cout << "Operation " << i << " performed.\n"; } int x,v; for(int i=0; i<q; i++) { std::cin >> x >> v; if(sets[x-1].count(v)) { std::cout << "TAK" << std::endl; } else { std::cout << "NIE" << std::endl; } } //std::cin >> 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 | #include <iostream> #include <vector> #include <set> #include <algorithm> #include <iterator> std::vector<std::set<int>> generateInitialSets(int n) { std::vector<std::set<int>> result{}; for(int i=1; i<=n; i++) { std::set<int> newSet{}; for(int j=i; j<=n; j+=i) { newSet.insert(j); } result.push_back(newSet); } return result; } std::set<int> performSum(const std::vector<std::set<int>>& sets) { int i,j; std::cin >> i >> j; std::set<int> result; std::set_union(sets[i-1].begin(), sets[i-1].end(), sets[j-1].begin(), sets[j-1].end(), std::inserter(result, result.begin())); return result; } std::set<int> performIntersection(const std::vector<std::set<int>>& sets) { int i,j; std::cin >> i >> j; std::set<int> result; std::set_intersection(sets[i-1].begin(), sets[i-1].end(), sets[j-1].begin(), sets[j-1].end(), std::inserter(result, result.begin())); return result; } std::set<int> performNegate(const std::vector<std::set<int>>& sets, int n) { int i; std::cin >> i; std::set<int> result; std::set_difference(sets[0].begin(), sets[0].end(), sets[i-1].begin(), sets[i-1].end(), std::inserter(result, result.begin())); return result; } std::set<int> createNewSet(int operation, const std::vector<std::set<int>>& sets, int n) { switch (operation) { case 1: return performSum(sets); case 2: return performIntersection(sets); case 3: return performNegate(sets, n); default: std::cout << "unrecognized option " << operation << std::endl; return {}; } } int main() { int n, m, q; std::cin >> n >> m >> q; //std::cout << "First line read.\n"; auto sets = generateInitialSets(n); //std::cout << "Sets generated"; int opeartion; for(int i=0; i<m; i++) { std::cin >> opeartion; //std::cout << "Operation read.\n"; sets.push_back(createNewSet(opeartion, sets, n)); //std::cout << "Operation " << i << " performed.\n"; } int x,v; for(int i=0; i<q; i++) { std::cin >> x >> v; if(sets[x-1].count(v)) { std::cout << "TAK" << std::endl; } else { std::cout << "NIE" << std::endl; } } //std::cin >> q; return 0; } |