// Olimpíadas Nacionais de Informática 2020 // Treino #01: Problema D - Torres // Transcrição da solução oficial (convertida de C++ para Java) import java.util.*; class Pair implements Comparable { int a, b; Pair(int a, int b) { this.a = a; this.b = b; } public int compareTo(Pair p) { if (a == p.a) return b - p.b; return a - p.a; } } class d { static int n, k, q; static long sol; static TreeMap rcnt = new TreeMap<>(); static TreeMap ccnt = new TreeMap<>(); static TreeMap rxor = new TreeMap<>(); static TreeMap cxor = new TreeMap<>(); static TreeMap rook = new TreeMap<>(); static int myget(TreeMap m, int key) { Integer i = m.get(key); if (i == null) return 0; return i; } static int mygetp(TreeMap m, Pair key) { Integer i = m.get(key); if (i == null) return 0; return i; } static void moveRook(int r, int c, int val) { sol -= n - myget(ccnt, myget(rxor, r)); sol -= n - myget(rcnt, myget(cxor, c)); if (myget(rxor, r) != myget(cxor, c)) sol += 1; int aux; aux = myget(rxor, r); rcnt.put(aux, myget(rcnt, aux) - 1); aux ^= val; rxor.put(r, aux); rcnt.put(aux, myget(rcnt, aux) + 1); aux = myget(cxor, c); ccnt.put(aux, myget(ccnt, aux) - 1); aux ^= val; cxor.put(c, aux); ccnt.put(aux, myget(ccnt, aux) + 1); sol += n - myget(ccnt, myget(rxor, r)); sol += n - myget(rcnt, myget(cxor, c)); if (myget(rxor, r) != myget(cxor, c)) sol -= 1; Pair p = new Pair(r, c); rook.put(p, mygetp(rook, p) ^ val); } static void init(Scanner in) { n = in.nextInt(); k = in.nextInt(); q = in.nextInt(); rcnt.put(0,n); ccnt.put(0,n); for (int i = 0; i < k; ++i) { int r = in.nextInt(); int c = in.nextInt(); int val = in.nextInt(); --r; --c; moveRook(r, c, val); } } static void solve(Scanner in) { while (q-- > 0) { int r1 = in.nextInt(); int c1 = in.nextInt(); int r2 = in.nextInt(); int c2 = in.nextInt(); --r1; --c1; --r2; --c2; int rookValue = mygetp(rook, new Pair(r1, c1)); moveRook(r1, c1, rookValue); moveRook(r2, c2, rookValue); System.out.println(sol); } } public static void main(String[] args) { Scanner in = new Scanner(System.in); init(in); solve(in); } }