package adv2019; import java.io.ByteArrayInputStream; import java.io.IOException; import java.io.InputStream; import java.io.PrintWriter; import java.util.Arrays; import java.util.Comparator; import java.util.InputMismatchException; public class D24 { InputStream is; PrintWriter out; String INPUT = ""; void solve() { int n = ni(); long[] h = new long[n]; for(int i = 0;i < n;i++){ h[i] = nl(); } int B = (int)Math.sqrt(n); int[][] co = new int[n][]; // int[] f = new int[n]; for(int i = 0;i < n;i++){ co[i] = na(5); co[i][4]--; // f[co[i][4]]++; } Arrays.sort(co, new Comparator() { public int compare(int[] a, int[] b) { if(a[4] != b[4])return a[4] - b[4]; return a[0] - b[0]; } }); int Q = ni(); int[][] qs = new int[Q][]; for(int i = 0;i < Q;i++){ qs[i] = new int[]{ni(), ni(), i}; } Arrays.sort(qs, new Comparator() { public int compare(int[] a, int[] b) { return a[0] - b[0]; } }); long[] anss = new long[Q]; // int[] big = new int[n/B+1]; // int bp = 0; int[][] es = new int[23000000][]; int p = 0; for(int i = 0;i < n;){ int j = i; while(j < n && co[j][4] == co[i][4]){ j++; } if(j-i >= B){ solve(Arrays.copyOfRange(co, i, j), qs, anss, h[co[i][4]], 2*n); }else{ int[][] les = expand(Arrays.copyOfRange(co, i, j)); for(int[] le : les){ es[p++] = le; } } i = j; } es = Arrays.copyOf(es, p); Arrays.sort(es, new Comparator() { public int compare(int[] a, int[] b) { return a[0] - b[0]; } }); long[] ft = new long[2*n+5]; int qp = 0; for(int[] e : es){ while(qp < Q && qs[qp][0] < e[0]){ anss[qs[qp][2]] ^= sumFenwick(ft, qs[qp][1]); qp++; } addFenwick(ft, e[1], e[2], h[e[3]]); } for(long v : anss){ out.println(v); } } int[][] expand(int[][] co) { int m = co.length; int[] xs = new int[2*m]; { int p = 0; for(int i = 0;i < m;i++){ xs[p++] = co[i][0]; xs[p++] = co[i][2]; } Arrays.sort(xs); xs = uniq(xs); } int[] ys = new int[2*m]; { int p = 0; for(int i = 0;i < m;i++){ ys[p++] = co[i][1]; ys[p++] = co[i][3]; } Arrays.sort(ys); ys = uniq(ys); } int[][] g = new int[xs.length+1][ys.length+1]; for(int i = 0;i < m;i++){ int xl = Arrays.binarySearch(xs, co[i][0]); int xr = Arrays.binarySearch(xs, co[i][2]); int yl = Arrays.binarySearch(ys, co[i][1]); int yr = Arrays.binarySearch(ys, co[i][3]); g[xl][yl]++; g[xl][yr]--; g[xr][yl]--; g[xr][yr]++; } for(int i = 0;i <= xs.length;i++){ for(int j = 0;j <= ys.length;j++){ if(i-1 >= 0)g[i][j] += g[i-1][j]; if(j-1 >= 0)g[i][j] += g[i][j-1]; if(i-1 >= 0 && j-1 >= 0){ g[i][j] -= g[i-1][j-1]; } } } int[][] ret = new int[xs.length*ys.length*2][]; int p = 0; for(int i = 0;i < xs.length;i++){ for(int j = 0;j < ys.length;j++){ if(g[i][j] > 0){ ret[p++] = new int[]{xs[i], ys[j], ys[j+1], co[0][4]}; ret[p++] = new int[]{xs[i+1], ys[j], ys[j+1], co[0][4]}; } } } return Arrays.copyOf(ret, p); } public static int[] uniq(int[] a) { int n = a.length; int p = 0; for(int i = 0;i < n;i++) { if(i == 0 || a[i] != a[i-1])a[p++] = a[i]; } return Arrays.copyOf(a, p); } void solve(int[][] co, int[][] qs, long[] anss, long h, int H) { int m = co.length; int[][] es = new int[m*2][]; int p = 0; for(int i = 0;i < m;i++){ es[p++] = new int[]{co[i][0], co[i][1], co[i][3], 1}; es[p++] = new int[]{co[i][2], co[i][1], co[i][3], -1}; } Arrays.sort(es, new Comparator() { public int compare(int[] a, int[] b) { return a[0] - b[0]; } }); int[] ft = new int[H+5]; int qp = 0; for(int[] e : es){ while(qp < qs.length && qs[qp][0] < e[0]){ if(sumFenwick(ft, qs[qp][1]) >= 1){ anss[qs[qp][2]] ^= h; } qp++; } addFenwick(ft, e[1], e[2], e[3]); } } public static int sumFenwick(int[] ft, int i) { int sum = 0; for(i++;i > 0;i -= i&-i)sum += ft[i]; return sum; } public static void addFenwick(int[] ft, int l, int r, int v) { addFenwick(ft, l, v); addFenwick(ft, r, -v); } public static void addFenwick(int[] ft, int i, int v) { if(v == 0 || i < 0)return; int n = ft.length; for(i++;i < n;i += i&-i)ft[i] += v; } public static long sumFenwick(long[] ft, int i) { long sum = 0; for(i++;i > 0;i -= i&-i)sum ^= ft[i]; return sum; } public static void addFenwick(long[] ft, int l, int r, long v) { addFenwick(ft, l, v); addFenwick(ft, r, v); } public static void addFenwick(long[] ft, int i, long v) { if(v == 0)return; int n = ft.length; for(i++;i < n;i += i&-i)ft[i] ^= v; } void run() throws Exception { // int n = 50000, m = 99999; // Random gen = new Random(); // StringBuilder sb = new StringBuilder(); // sb.append(n + " "); // for(int i = 0 // for (int i = 0; i < n; i++) { // int v1 = gen.nextInt(2*n); // int v2 = gen.nextInt(2*n); // int w1 = gen.nextInt(2*n); // int w2 = gen.nextInt(2*n); // sb.append(Math.min(v1, v2) + 1 + " "); // sb.append(Math.min(w1, w2) + 1 + " "); // sb.append(Math.max(v1, v2) + 1 + " "); // sb.append(Math.max(w1, w2) + 1 + " "); // sb.append(gen.nextInt(200000) + " "); // } // INPUT = sb.toString(); is = INPUT.isEmpty() ? System.in : new ByteArrayInputStream(INPUT.getBytes()); out = new PrintWriter(System.out); long s = System.currentTimeMillis(); solve(); out.flush(); if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms"); // Thread t = new Thread(null, null, "~", Runtime.getRuntime().maxMemory()){ // @Override // public void run() { // long s = System.currentTimeMillis(); // solve(); // out.flush(); // if(!INPUT.isEmpty())tr(System.currentTimeMillis()-s+"ms"); // } // }; // t.start(); // t.join(); } public static void main(String[] args) throws Exception { new D24().run(); } private byte[] inbuf = new byte[1024]; public int lenbuf = 0, ptrbuf = 0; private int readByte() { if(lenbuf == -1)throw new InputMismatchException(); if(ptrbuf >= lenbuf){ ptrbuf = 0; try { lenbuf = is.read(inbuf); } catch (IOException e) { throw new InputMismatchException(); } if(lenbuf <= 0)return -1; } return inbuf[ptrbuf++]; } private boolean isSpaceChar(int c) { return !(c >= 33 && c <= 126); } private int skip() { int b; while((b = readByte()) != -1 && isSpaceChar(b)); return b; } private double nd() { return Double.parseDouble(ns()); } private char nc() { return (char)skip(); } private String ns() { int b = skip(); StringBuilder sb = new StringBuilder(); while(!(isSpaceChar(b))){ // when nextLine, (isSpaceChar(b) && b != ' ') sb.appendCodePoint(b); b = readByte(); } return sb.toString(); } private char[] ns(int n) { char[] buf = new char[n]; int b = skip(), p = 0; while(p < n && !(isSpaceChar(b))){ buf[p++] = (char)b; b = readByte(); } return n == p ? buf : Arrays.copyOf(buf, p); } private int[] na(int n) { int[] a = new int[n]; for(int i = 0;i < n;i++)a[i] = ni(); return a; } private long[] nal(int n) { long[] a = new long[n]; for(int i = 0;i < n;i++)a[i] = nl(); return a; } private char[][] nm(int n, int m) { char[][] map = new char[n][]; for(int i = 0;i < n;i++)map[i] = ns(m); return map; } private int[][] nmi(int n, int m) { int[][] map = new int[n][]; for(int i = 0;i < n;i++)map[i] = na(m); return map; } private int ni() { return (int)nl(); } private long nl() { long num = 0; int b; boolean minus = false; while((b = readByte()) != -1 && !((b >= '0' && b <= '9') || b == '-')); if(b == '-'){ minus = true; b = readByte(); } while(true){ if(b >= '0' && b <= '9'){ num = num * 10 + (b - '0'); }else{ return minus ? -num : num; } b = readByte(); } } private static void tr(Object... o) { System.out.println(Arrays.deepToString(o)); } }