|
| 1 | +```java |
| 2 | +import java.io.*; |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +class IOController { |
| 6 | + BufferedReader br; |
| 7 | + BufferedWriter bw; |
| 8 | + StringTokenizer st; |
| 9 | + |
| 10 | + public IOController() { |
| 11 | + br = new BufferedReader(new InputStreamReader(System.in)); |
| 12 | + bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 13 | + st = new StringTokenizer(""); |
| 14 | + } |
| 15 | + |
| 16 | + String nextLine() throws Exception { |
| 17 | + String line = br.readLine(); |
| 18 | + st = new StringTokenizer(line); |
| 19 | + return line; |
| 20 | + } |
| 21 | + |
| 22 | + String nextToken() throws Exception { |
| 23 | + while (!st.hasMoreTokens()) |
| 24 | + nextLine(); |
| 25 | + return st.nextToken(); |
| 26 | + } |
| 27 | + |
| 28 | + int nextInt() throws Exception { |
| 29 | + return Integer.parseInt(nextToken()); |
| 30 | + } |
| 31 | + |
| 32 | + long nextLong() throws Exception { |
| 33 | + return Long.parseLong(nextToken()); |
| 34 | + } |
| 35 | + |
| 36 | + double nextDouble() throws Exception { |
| 37 | + return Double.parseDouble(nextToken()); |
| 38 | + } |
| 39 | + |
| 40 | + void close() throws Exception { |
| 41 | + bw.flush(); |
| 42 | + bw.close(); |
| 43 | + } |
| 44 | + |
| 45 | + void write(String content) throws Exception { |
| 46 | + bw.write(content); |
| 47 | + } |
| 48 | + |
| 49 | +} |
| 50 | + |
| 51 | +public class Main { |
| 52 | + |
| 53 | + static IOController io; |
| 54 | + |
| 55 | + // |
| 56 | + |
| 57 | + static int N, Q; |
| 58 | + static int[][] seg, lazy; |
| 59 | + static int[] in, out, rev, d, par; |
| 60 | + static List<int[]>[] graph; |
| 61 | + static int order = 0; |
| 62 | + |
| 63 | + public static void main(String[] args) throws Exception { |
| 64 | + |
| 65 | + io = new IOController(); |
| 66 | + |
| 67 | + N = io.nextInt(); |
| 68 | + graph = new List[N+1]; |
| 69 | + in = new int[N+1]; |
| 70 | + out = new int[N+1]; |
| 71 | + rev = new int[N+1]; |
| 72 | + d = new int[N+1]; |
| 73 | + par = new int[N+1]; |
| 74 | + seg = new int[4*N][20]; |
| 75 | + lazy = new int[4*N][20]; |
| 76 | + for(int i=1;i<=N;i++) graph[i] = new ArrayList<>(); |
| 77 | + for(int i=1;i<N;i++) { |
| 78 | + int a = io.nextInt(); |
| 79 | + int b = io.nextInt(); |
| 80 | + int c = io.nextInt(); |
| 81 | + graph[a].add(new int[]{b,c}); |
| 82 | + graph[b].add(new int[]{a,c}); |
| 83 | + } |
| 84 | + |
| 85 | + dfs(1,0,0); |
| 86 | + |
| 87 | + for(int i=0;i<20;i++) init(1,N,1,i); |
| 88 | + |
| 89 | + for(Q=io.nextInt();Q-->0;) { |
| 90 | + int o = io.nextInt(); |
| 91 | + int x = io.nextInt(); |
| 92 | + int y = io.nextInt(); |
| 93 | + if(o == 1) { |
| 94 | + int g = par[x] ^ y; |
| 95 | + for(int k=0;k<20;k++) if((g & (1<<k)) != 0) update(1,N,in[x],out[x],1,k); |
| 96 | + par[x] = y; |
| 97 | + } |
| 98 | + else { |
| 99 | + long ans = 0; |
| 100 | + for(int k=0;k<20;k++) { |
| 101 | + long o1 = find(1,N,in[x],out[x],1,k), z1 = (out[x]-in[x]+1) - o1; |
| 102 | + long o2 = find(1,N,in[y],out[y],1,k), z2 = (out[y]-in[y]+1) - o2; |
| 103 | + ans += (o1*z2 + o2*z1) * (1L<<k); |
| 104 | + } |
| 105 | + io.write(ans + "\n"); |
| 106 | + } |
| 107 | + } |
| 108 | + |
| 109 | + io.close(); |
| 110 | + |
| 111 | + } |
| 112 | + |
| 113 | + static void dfs(int n, int p, int v) { |
| 114 | + d[n] = v; |
| 115 | + in[n] = ++order; |
| 116 | + rev[order] = n; |
| 117 | + for(int[] e:graph[n]) if(e[0] != p) { |
| 118 | + dfs(e[0],n,v^e[1]); |
| 119 | + par[e[0]] = e[1]; |
| 120 | + } |
| 121 | + out[n] = order; |
| 122 | + } |
| 123 | + |
| 124 | + static void init(int s, int e, int n, int x) { |
| 125 | + if(s == e) { |
| 126 | + seg[n][x] = (d[rev[s]] & (1<<x)) == 0 ? 0 : 1; |
| 127 | + return; |
| 128 | + } |
| 129 | + int m = (s+e)>>1; |
| 130 | + init(s,m,n*2,x); |
| 131 | + init(m+1,e,n*2+1,x); |
| 132 | + seg[n][x] = seg[n*2][x] + seg[n*2+1][x]; |
| 133 | + } |
| 134 | + |
| 135 | + static void prop(int s, int e, int n, int x) { |
| 136 | + if(lazy[n][x] != 0) { |
| 137 | + seg[n][x] = e-s+1 - seg[n][x]; |
| 138 | + if(s != e) { |
| 139 | + lazy[n*2][x] ^= 1; |
| 140 | + lazy[n*2+1][x] ^= 1; |
| 141 | + } |
| 142 | + lazy[n][x] = 0; |
| 143 | + } |
| 144 | + } |
| 145 | + |
| 146 | + static void update(int s, int e, int l, int r, int n, int x) { |
| 147 | + prop(s,e,n,x); |
| 148 | + if(l>r || l>e || r<s) return; |
| 149 | + if(l<=s && e<=r) { |
| 150 | + seg[n][x] = e-s+1 - seg[n][x]; |
| 151 | + if(s != e) { |
| 152 | + lazy[n*2][x] ^= 1; |
| 153 | + lazy[n*2+1][x] ^= 1; |
| 154 | + } |
| 155 | + return; |
| 156 | + } |
| 157 | + int m = (s+e)>>1; |
| 158 | + update(s,m,l,r,n*2,x); |
| 159 | + update(m+1,e,l,r,n*2+1,x); |
| 160 | + seg[n][x] = seg[n*2][x] + seg[n*2+1][x]; |
| 161 | + } |
| 162 | + |
| 163 | + static int find(int s, int e, int l, int r, int n, int x) { |
| 164 | + prop(s,e,n,x); |
| 165 | + if(l>r || l>e || r<s) return 0; |
| 166 | + if(l<=s && e<=r) return seg[n][x]; |
| 167 | + int m = (s+e)>>1; |
| 168 | + return find(s,m,l,r,n*2,x) + find(m+1,e,l,r,n*2+1,x); |
| 169 | + } |
| 170 | + |
| 171 | +} |
| 172 | +``` |
0 commit comments