#include "happy.h"
#include <bits/stdc++.h>

using namespace std;

int n;

vector <long long> fen, fen1, pre, suf;

vector <bool> b;

vector <vector <pair <int, long long> > > adj;

long long answer = 0;

void change( int j, long long d ) {
	
	while ( j < n ) {
		
		fen[j] += d;
		j = ( j | ( j + 1 ) );
		
	}
	
	return;
	
}

void change1( int j, long long d ) {
	
	while ( j < n ) {
		
		fen1[j] += d;
		j = ( j | ( j + 1 ) );
		
	}
	
	return;
	
}

long long fsum( int j ) {
	
	long long sum = 0;
	
	while ( j >= 0 ) {
		
		sum += fen[j];
		j = ( j & ( j + 1 ) ) - 1;
		
	}
	
	return sum;
	
}

long long fsum1( int j ) {
	
	long long sum = 0;
	
	while ( j >= 0 ) {
		
		sum += fen1[j];
		j = ( j & ( j + 1 ) ) - 1;
		
	}
	
	return sum;
	
}

void f( int u, int pr, long long sum ) {
	
	for ( int i = 0; i < (int)adj[u].size(); i++ ) {
		
		int x = adj[u][i].first;
		
		if ( x != pr ) {
			
			sum = ( sum ^ adj[u][i].second );
			if ( b[x] == true ) {
				answer += sum;
			}
			f( x, u, sum );
			sum = ( sum ^ adj[u][i].second );
			
		}
		
	}
	
	return;
	
}

std::vector <long long int> happy (int N, int Q, std::vector <int> u, std::vector <int> v, std::vector <int> h, std::vector <int> t, std::vector <int> l, std::vector <int> r) {

	n = N;
	
	b.resize( n );
	adj.resize( n );
	fen.resize( n ), fen1.resize( n );
	pre.resize( n ), suf.resize( n );
	
	vector <long long> ans( Q );
	
	answer = 0;
	
	for ( int i = 0; i < n; i++ ) {
		
		adj[i].clear();
		b[i] = false;
		pre[i] = suf[i] = fen[i] = fen1[i] = 0;
		
		if ( i != 0 ) {
			
			pre[i] = h[i - 1] + pre[i - 1];
			
		}
	
	}
	
	for ( int i = n - 1; i >= 0; i-- ) {
		
		if ( i != n - 1 ) {
			
			suf[i] = h[i] + suf[i + 1];
		
		}
		
	}
	
	for ( int i = 0; i < n - 1; i++ ) {
		
		adj[u[i]].push_back( { v[i], h[i] } );
		adj[v[i]].push_back( { u[i], h[i] } );
		
	}
	
	if ( n <= 100 and Q <= 100 ) {
		
		for ( int i = 0; i < Q; i++ ) {
		
			answer = 0;
		
			for ( int j = l[i]; j <= r[i]; j++ ) {
			
				if ( t[i] == 1 ) {
					b[j] = true;
				} else {
					b[j] = false;
				}
			
			}
		
			for ( int j = 0; j < n; j++ ) {
				if ( b[j] == true ) {
					f( j, -1, 0 );
				}
			}
		
			ans[i] = answer / 2;
		
		}
		
	} else {
		
		for ( int i = 0; i < Q; i++ ) {
			
			long long xr = 0, xr1 = 0;
			
			ans[i] = answer / 2;
			
			if ( t[i] == 1 ) {
				
				if ( b[l[i]] == true ) {
					
					continue;
					
				}
				
				if ( i != 0 ) {
					change( l[i], pre[l[i]] );
					xr = pre[i];
				}
				
				if ( i != n - 1 ) {
					
					change1( l[i], suf[l[i]] );
					xr1 = suf[l[i]];
					
				}
				
				answer += ( ( fsum( n - 1 ) - fsum( l[i] ) ) ^ xr );
				answer += ( fsum1( l[i] - 1 ) ^ xr1 );
				
			} else {
				
				if ( b[l[i]] == false ) {
					
					continue;
					
				}
				
				if ( i != 0 ) {
					change( l[i], -pre[l[i]] );
					xr = pre[i];
				}
				
				if ( i != n - 1 ) {
					change1( l[i], -suf[l[i]] );
					xr1 = suf[l[i]];
				}
				
				answer -= ( ( fsum( n - 1 ) - fsum( l[i] ) ) ^ xr );
				answer -= ( fsum1( l[i] - 1 ) ^ xr1 );
				
			}
			
			ans[i] = answer / 2;
			
		}
		
	}

	return ans;
	
}

/*extern vector <long long int> happy (int N, int Q, vector <int> u, vector <int> v, vector <int> h, vector <int> t, vector <int> l, vector <int> r);
int main () {
    ios_base::sync_with_stdio(false);
    cin.tie(NULL);
    int n,q;
    cin >> n >> q ;
    vector <int> u(n-1),v(n-1),h(n-1);
    for (int i=0; i<n-1; i++) {
        cin >> u[i] >> v[i] >> h[i] ;
    }
    vector <int> t(q),l(q),r(q);
    for (int i=0; i<q; i++) {
        cin >> t[i] >> l[i] >> r[i] ;
    }
    
    vector <long long> sum = happy(n,q,u,v,h,t,l,r);
    
	for ( int i = 0; i < (int)sum.size(); i++ ) {
		cout << sum[i] << "\n";
	}
    return 0;
}*/