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
#include <iostream>
#include <vector>

class Zbior
{
private:
  std::vector<unsigned long long> maska;
public:
  Zbior(int maxElement):
    maska((maxElement / 64) + 1, 0)
  {
  }

  Zbior suma(Zbior const& zbior) const
  {
    Zbior result = *this;
    for (int i=0; i < maska.size(); ++i)
      result.maska[i] = result.maska[i] | zbior.maska[i];
    return result;
  }

  Zbior przeciecie(Zbior const& zbior) const
  {
    Zbior result = *this;
    for (int i = 0; i < maska.size(); ++i)
      result.maska[i] = result.maska[i] & zbior.maska[i];
    return result;
  }

  Zbior negacja() const
  {
    Zbior result = *this;
    for (int i = 0; i < maska.size(); ++i)
      result.maska[i] = ~result.maska[i];
    return result;
  }

  void wstaw(int position)
  {
    int maskaPosition = position / 64;
    int offset = position - (maskaPosition * 64);
    maska[maskaPosition] |= 1ull << offset;
  }

  bool czyZawiera(int position)
  {
    int maskaPosition = position / 64;
    int offset = position - (maskaPosition * 64);
    return (maska[maskaPosition] & (1ull << offset)) > 0;
  }

};

int main()
{
  std::ios_base::sync_with_stdio(false);
  std::cin.tie(NULL);

  int n;
  int m;
  int q;
  std::cin >> n >> m >> q;

  std::vector<Zbior> zbiory(n+m+1, n+1);
  for (int i=1; i<=n; ++i)
  {
    for (int j=i; j<=n; j+=i)
      zbiory[i].wstaw(j);
  }
  for (int i = n + 1; i <= m + n; ++i)
  {
    int operacja;
    int x;
    int y;
    std::cin >> operacja;
    if (operacja == 1)
    {
      std::cin >> x >> y;
      zbiory[i] = zbiory[x].suma(zbiory[y]);
    }
    if (operacja == 2)
    {
      std::cin >> x >> y;
      zbiory[i] = zbiory[x].przeciecie(zbiory[y]);
    }
    if (operacja == 3)
    {
      std::cin >> x;
      zbiory[i] = zbiory[x].negacja();
    }
  }

  for (int i = 0; i < q; ++i)
  {
    int x;
    int v;
    std::cin >> x >> v;
    if (zbiory[x].czyZawiera(v))
      std::cout << "TAK\n";
    else
      std::cout << "NIE\n";
  }
}