#include "happy.h"
#include <iostream>
#include <iomanip>
#include <vector>
#include <set>

using namespace std;
const int N = 3e5 + 67;
typedef long long ll;

int n, q;
int pref[N];

#define ff first
#define ss second

vector<pair<int, long long>>G[N];
bool is_open[N];

void dfs(int node, int parent)
{
	for (auto i : G[node])
	{
		if (i.ff == parent)continue;
		pref[i.ff] = pref[node] ^ i.ss;
		dfs(i.ff, node);
	}
}

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;
	q = Q;
	vector<ll>ans;
	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] });
	}

	dfs(0, -1);
	for (int i = 0; i < q; i++)
	{
		int s = t[i];
		if (s == 1)
		{
			int L = l[i];
			int R = r[i];
			for (int j = L; j <= R; j++)
			{
				is_open[j] = true;
			}
		}
		else
		{
			int L = l[i];
			int R = r[i];
			for (int j = L; j <= R; j++)
			{
				is_open[j] = false;
			}
		}

		ll answ = 0;
		for (int j = 0; j < n; j++)
		{
			for (int k = j + 1; k < n; k++)
			{
				if(is_open[k] && is_open[j])
					answ += pref[j] ^ pref[k];
			}
		}
		ans.push_back(answ);
	}
	return ans;
}
