#include "happy.h"
#include<unordered_map>
using namespace std;
vector<pair<int,int>> muchii[300001];
vector<long long> ans;
bool activ[300001];
vector<int> opened;
int und=0;
long long sum=0;
void dfs(int parent,int node,int cat)
{
    for(int i=0; i<muchii[node].size(); i++)
    {
        int nxt=muchii[node][i].first;
        if(nxt==parent)
        {
            continue;
        }
        cat=cat^muchii[node][i].second;
        if(activ[nxt])
        {
            sum+=cat;
        }
        dfs(node,nxt,cat);
        cat=cat^muchii[node][i].second;
    }
}
void dfs2(int parent,int node,int cat)
{
    for(int i=0; i<muchii[node].size(); i++)
    {
        int nxt=muchii[node][i].first;
        if(nxt==parent)
        {
            continue;
        }
        cat=cat^muchii[node][i].second;
        if(activ[nxt])
        {
            sum-=cat;
        }
        dfs2(node,nxt,cat);
        cat=cat^muchii[node][i].second;
    }
}
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)
{
    for(int i=0; i<N-1; i++)
    {
        int a=u[i];
        int b=v[i];
        int c=h[i];
        muchii[a].push_back({b,c});
        muchii[b].push_back({a,c});
    }
    for(int q=0; q<Q; q++)
    {
        int type=t[q];
        int x=l[q];
        int y=r[q];
        if(type==1)
        {
            for(int j=x; j<=y; j++)
            {
                if(activ[j])
                {
                    continue;
                }
                activ[j]=1;
                und=j;
                dfs(-1,j,0);
                opened.push_back(j);
            }
        }
        else
        {
            for(int j=x; j<=y; j++)
            {
                if(!activ[j])
                {
                    continue;
                }
                activ[j]=0;
                dfs2(-1,j,0);
            }
        }
        ans.push_back(sum);
    }
    return ans;
}

