#include "happy.h"
#include <bits/stdc++.h>
using namespace std;

const long long MAXN=100, INF=LLONG_MAX;
int n, q;
vector<pair<int, int>> nbrs[MAXN];
long long dist[MAXN][MAXN];
bool open[MAXN];

void dfs(int curr, int from)
{
    for(auto [nbr, w]: nbrs[curr])
    {
        if(dist[from][nbr]==INF)
        {
            dist[from][nbr]=(dist[from][curr]^w);
            dfs(nbr, from);
        }
    }
}

long long calc()
{
    long long ans=0;
    for(int i=0; i<n; i++)
    {
        for(int j=i+1; j<n; j++)
        {
            if(open[i] && open[j])
            {
                ans+=dist[i][j];
            }
        }
    }

    return ans;
}

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<n-1; i++)
    {
        nbrs[u[i]].push_back({v[i], h[i]});
        nbrs[v[i]].push_back({u[i], h[i]});
    }

    for(int i=0; i<n; i++)
    {
        fill(dist[i], dist[i+1], INF);
        dist[i][i]=0;

        dfs(i, i);

    }


    vector<long long> answer;
    for(int j=0; j<q; j++)
    {
        int type=t[j], left=l[j], right=r[j];
        if(type==1)
        {
            for(int i=left; i<=right; i++)
            {
                open[i]=1;
            }
        }
        else
        {
            for(int i=left; i<=right; i++)
            {
                open[i]=0;
            }
        }

        answer.push_back(calc());
    }

    return answer;
}
