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

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

int n, q;
int pref[N];

#define ff first
#define ss second

vector<pair<int, long long>>G[N];
bool open[N];
int cnt[LOG];

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);
	}
}

ll answ, c;

void del(int i)
{
	c -= open[i];
	for (int b = 0; b < LOG; b++)
	{
		if (pref[i] & (1 << b))
		{
			cnt[b] -= (open[i]);
		}
	}
}

void add(int i)
{
	c += open[i];
	for (int b = 0; b < LOG; b++)
	{
		if (pref[i] & (1 << b))
		{
			cnt[b] += (open[i]);
		}
	}
}

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++)
			{
				del(j);
				open[j] = true;
				add(j);
			}
		}
		else
		{
			int L = l[i];
			int R = r[i];
			for (int j = L; j <= R; j++)
			{
				del(j);
				open[j] = false;
				add(j);
			}
		}

		ll answer = 0;
		for (int b = 0; b < LOG; b++)
		{
			answer += (1ll << b) * (cnt[b]) * (c - cnt[b]);
		}

		ans.push_back(answer);
	}
	return ans;
}
