#include "happy.h"
using namespace std;
int n,q;
struct edge
{
    int v,h;
};
const int MAXN=300100;
vector<edge> v[MAXN];
struct query
{
    int t,l,r;
};
vector<query> que;
int dist[MAXN];
void dfs(int beg,int par)
{
    for(edge nb:v[beg])
    {
        if(nb.v==par)continue;
        dist[nb.v]=dist[beg]^nb.h;
        dfs(nb.v,beg);
    }
}
bool ac[MAXN];
void solve(vector<long long int>&ans)
{
    dfs(0,-1);
    for(int i=0;i<n;i++)ac[i]=0;
    for(query cq:que)
    {
        for(int i=cq.l;i<=cq.r;i++)
        {
            ac[i]=(cq.t==1);
        }
        long long cans=0;
        for(int i=0;i<n;i++)
        {
            if(!ac[i])continue;
            for(int j=i+1;j<n;j++)
            {
                if(!ac[j])continue;
                cans+=dist[i]^dist[j];
            }
        }
        ans.push_back(cans);
    }
}
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)
{
    n=N;
    q=Q;
    for(int i=0;i<u.size();i++)
    {
        ::v[u[i]].push_back({v[i],h[i]});
        ::v[v[i]].push_back({u[i],h[i]});
    }
    for(int i=0;i<q;i++)
    {
        que.push_back({t[i],l[i],r[i]});
    }
    vector<long long int> ans;
    solve(ans);
    return ans;
}
