/** * We will solve the problem for each bit separately. * For a fixed bit, the reccurence G[r, c] = G[r - 1, c] xor G[r, c - 1] is * the same as used in Sierpinski triangle (Pascal's triangle modulo 2), the * only difference is the base condition. The values in first row/column are * very regular in our case, first row has only zeroes (or only ones), and * first column has 2^k zeroes, 2^k ones, 2^k zeroes, 2^k ones, and so on. * We can notice that the column number 2^k of the original Sierpinski triangle * has exactly the same pattern. Another thing about the original Sierpinski * triangle is that the row number (2^k - 1) has the following structure: * 1 0 0 0 0 0 ... 0 0 0 * <--- (2^k - 1) ---> * So, our Xor Grid (for k-th bit) is just a part of the Sierpinski triangle. * * Then we will use the fact that Sierpinski triangle is a fractal and to find * the xor sum of a rectangle, we just want to find the parity of number of ones. **/ #include using namespace std; // Calculates the i-th bit of G[-inf..r, -inf..c] long long f(long long r, long long c, int i) { r += (1ll << 62) - (1ll << i) + 1; c += (1ll << i); if (r & c) return (1ll << i); return 0; } int main() { int t; scanf("%d", &t); while (t--) { long long r1, r2, c1, c2; scanf("%lld %lld %lld %lld", &r1, &r2, &c1, &c2); r1--; c1--; long long ans = 0; for (int i = 0; i < 60; i++) { ans ^= f(r2, c2, i); ans ^= f(r1, c2, i); ans ^= f(r2, c1, i); ans ^= f(r1, c1, i); } printf("%lld\n", ans); } return 0; }