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
#include <bits/stdc++.h>
using namespace std;
const int maxn = 7e7 + 1;
int n, m, q;

int T_SUM = 1;
int T_DIFF = 2;
int T_FLP = 3;

struct SetDefinition {
    int operation; // 1 - sum; 2 - diff, 3 - flip
    int arg0;
    int arg1;
};

SetDefinition definitions[maxn];

unordered_map<int, unordered_map<int, bool> > cache;

int calls = 0;

bool testFor(int x, int v) {
    calls++;
    if (x <= n) {
        // cout << "Below n \n" << x << " " << v << "\n";
        return v % x == 0;
    }

    if (cache.find(x) != cache.end()) {
        if (cache[x].find(v) != cache[x].end()) {
            return cache[x][v];
        }
    }

    bool result = false;
    // cout << x << " - czy tutaj?\n";
    SetDefinition def = definitions[x];
    if (def.operation == T_SUM) {
        result = testFor(def.arg0, v) || testFor(def.arg1, v);
    }

    if (def.operation == T_DIFF) {
        result = testFor(def.arg0, v) && testFor(def.arg1, v);
    }

    if (def.operation == T_FLP) {
        result = !testFor(def.arg0, v);
    }

    // cout << x << "nie\n";
    cache[x][v] = result;
    return result;
}

int main() {
    cin >> n >> m >> q;
    // for(int i = 1; i<=n; i++ ) {

    // }
    for (int i = n+1; i<=n+m; i++) {
        int t; cin >> t;
        int arg0; cin >> arg0;
        definitions[i].operation = t;
        definitions[i].arg0 = arg0;
        if(t!=3) {
            int arg1; cin >> arg1;
            definitions[i].arg1 = arg1;
        }
    }
    while(q-->0) {
        int x, v; cin >> x >> v;
        // is v in A_x
        cout << (testFor(x, v) ? "TAK" : "NIE") << "\n";
    }
    // cout << "Finished with " << calls << " testFor calls\n";
}