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

using namespace std;

#define getBlockIndex(index) (index / 64)
#define getBitIndex(index) (index % 64)

class BitVector {
private:
    std::vector<unsigned long long> data;
    int length;
public:
    BitVector(int n) : length(n), data((n + 63) / 64, 0) {}
    BitVector() : length(0) {}
	BitVector(int n, std::vector<unsigned long long> d)
        : length(n), data(std::move(d)) {
        int lastBlockIndex = data.size() - 1;
        int lastBitIndex = length % 64;
        if (lastBitIndex > 0) {
            data[lastBlockIndex] &= ((1ULL << lastBitIndex) - 1);
        }
    }

    void set(int index, bool value) {
        int blockIndex = getBlockIndex(index);
		int bitIndex = getBitIndex(index);
        if (value) {
            data[blockIndex] |= (1ULL << bitIndex);
        } else {
            data[blockIndex] &= ~(1ULL << bitIndex);
        }
    }

    bool getValue(int index) const {
		int blockIndex = getBlockIndex(index);
		int bitIndex = getBitIndex(index);
        return (data[blockIndex] & (1ULL << bitIndex)) != 0;
    }
    
    bool contains(int v) const {
		return v < length && getValue(v);
	}

    void negate() {
        for (auto& block : data) {
            block = ~block;
        }
        int lastBlockIndex = data.size() - 1;
        int lastBitIndex = length % 64;
        if (lastBitIndex > 0) {
            data[lastBlockIndex] &= ((1ULL << lastBitIndex) - 1);
        }
    }

    BitVector operator+(const BitVector& other) const {
		int minLength = std::min(length, other.length);
        BitVector result(minLength);

        int minBlocks = std::min(data.size(), other.data.size());

        for (int i = 0; i < minBlocks; ++i) {
            result.data[i] = data[i] & other.data[i];
        }

		int lastBlockIndex = result.data.size() - 1;
        int lastBitIndex = minLength % 64;
        if (lastBitIndex > 0) {
            result.data[lastBlockIndex] &= ((1ULL << lastBitIndex) - 1);
        }

        return result;
    }
    
    BitVector operator~() const {
		BitVector result(length, data);
		result.negate();
		return result;
	}

    BitVector operator-(const BitVector& other) const {
        int maxLength = std::max(length, other.length);
        BitVector result(maxLength);

        int minBlocks = std::min(data.size(), other.data.size());
        int maxBlocks = std::max(data.size(), other.data.size());

        for (int i = 0; i < minBlocks; ++i) {
			result.data[i] = data[i] | other.data[i];
        }

        if (data.size() > other.data.size()) {
            for (int i = minBlocks; i < maxBlocks; ++i) {
                result.data[i] = data[i];
            }
        } else {
            for (int i = minBlocks; i < maxBlocks; ++i) {
                result.data[i] = other.data[i];
            }
        }

        int lastBlockIndex = result.data.size() - 1;
        int lastBitIndex = maxLength % 64;
        if (lastBitIndex > 0) {
            result.data[lastBlockIndex] &= ((1ULL << lastBitIndex) - 1);
        }

        return result;
    }
};

int main() {
    int n, m, q;
    scanf("%d %d %d", &n, &m, &q);
    vector<BitVector> sets(n+m);
    for(int i = 0; i < n; i++) {
		int a = i + 1;
		sets[i] = BitVector(n);
		int l = a; 
		while (l <= n) {
			sets[i].set(l - 1, true);
			l += a;
		}
	}
	for(int i = 0; i < m; i++) {
		int t;
		scanf("%d", &t);
		if (t == 1) {
			int x, y;
			scanf("%d %d", &x, &y);
			sets[i + n] = sets[x - 1] - sets[y - 1];
		} else if (t == 2) {
			int x, y;
			scanf("%d %d", &x, &y);
			sets[i + n] = sets[x - 1] + sets[y - 1];
		} else {
			int x;
			scanf("%d", &x);
			sets[i + n] = ~sets[x - 1];
		}		
	}
	for(int i = 0; i < q; i++) {
		int x, v;
		scanf("%d %d", &x, &v);
		puts(sets[x - 1].contains(v - 1) ? "TAK" : "NIE");		
	}
    return 0;
}