#include "happy.h"
#include <bits/stdc++.h>
using namespace std;
void dfs(int i,int node,vector<vector<pair<int,int>>> &graph,int sum,vector<bool> &aux,vector<vector<int>> &sume)
{
    aux[node]=1;
    sume[i][node]=sum;
    sume[node][i]=sum;
    for(auto it:graph[node])
    {
        if(aux[it.first]==0)
        {
            dfs(i,it.first,graph,(sum ^ it.second),aux,sume);
        }
    }
}
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)
{
    vector<bool> aux(N+5,0);
    vector<vector<int>> sume(N+5,vector<int>(N+5,0));
    vector<vector<pair<int,int>>> graph(N+5);
    vector<long long> ans1;
    for(int i=0;i<N;i++)
    {
        graph[u[i]].push_back({v[i],h[i]});
        graph[v[i]].push_back({u[i],h[i]});
    }
    for(int i=0;i<N;i++)
    {
        vector<bool> aux1(N+5,0);
        dfs(i,i,graph,0,aux1,sume);
    }
    long long ans=0;
    for(int i=0;i<Q;i++)
    {
        int tip=t[i];
        for(int j=l[i];j<=r[i];j++)
        {
            if(tip==1)
            {
                for(int u=0;u<N && aux[j]==0;u++)
                {
                    if(aux[u]==1) ans+=sume[j][u];
                }
                aux[j]=1;
            }
            else
            {
                for(int u=0;u<N && aux[j]==1;u++)
                {
                    if(aux[u]==1) ans-=sume[j][u];
                }
                aux[j]=0;
            }
        }
        ans1.push_back(ans);
    }
    return ans1;
}
