#include "happy.h"
#include <bits/stdc++.h>
using namespace std;
const int N = 2002;
vector <array <int,2> > g[N];
long long xr[N][N];
void dfs(int v,int pr,int ver)
{
	for(int i = 0;i < g[v].size();i++)
	{
		int u = g[v][i][0];
		int h = g[v][i][1];
		if(u == pr)continue;
		xr[ver][u] = (xr[ver][v] ^ h);
		dfs(u,v,ver);
	}
}
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) {
	for(int i = 0;i < n;i++)g[i].clear();
	for(int i = 0;i < n - 1;i++)
	{
		g[u[i]].push_back({v[i],h[i]});
		g[v[i]].push_back({u[i],h[i]});
	}
	for(int i = 0;i < n;i++)dfs(i,-1,i);
	vector <int> op(n);
	vector <long long int> ans;
	for(int i = 0;i < q;i++)
	{
		if(t[i] == 1)
		{
			for(int j = l[i];j <= r[i];j++)op[j] = 1;
		}else
		{
			for(int j = l[i];j <= r[i];j++)op[j] = 0;
		}
		long long int sum = 0;
		for(int j = 0;j < n;j++)
		{
			for(int jj = j + 1;jj < n;jj++)
			{
				sum += op[j] * op[jj] * xr[j][jj];
			}
		}
		ans.push_back(sum);
	}
	return ans;
}