#include "happy.h"
#include <bits/stdc++.h>
using namespace std;
vector <pair <int,int>> v[333333];
long long sum=0,op[333333];
void dfs(int beg,int par,int x)
{
    if(op[beg]&&x!=-1)sum+=x;
    for(auto nb:v[beg])
    {
        if(nb.first!=par)
        {
            int x1=x;
            if(x==-1)x1=nb.second;
            else x1=(x1^nb.second);
            dfs(nb.first,beg,x1);
        }
    }
}
vector <long long> happy(int n, int q, vector <int> u, vector <int> V, vector <int> h, vector <int> t, vector <int> l, vector <int> r)
{
    for(int i=0;i<n-1;i++)
    {
        v[u[i]].push_back({V[i],h[i]});
        v[V[i]].push_back({u[i],h[i]});
    }
    vector <long long> 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;
        }
        sum=0;
        for(int j=0;j<n;j++)
        {
            if(op[j])
            {
                dfs(j,-1,-1);
            }
        }
        ans.push_back(sum/2);
    }
    return ans;
}
