#include "happy.h"
#include<unordered_map>
using namespace std;
unordered_map<int,int> drum[300001];
vector<pair<int,int>> muchii[300001];
vector<long long> ans;
bool activ[300001];
vector<int> opened;
int und=0;
void dfs(int parent,int node)
{
    for(int i=0; i<muchii[node].size(); i++)
    {
        int nxt=muchii[node][i].first;
        if(nxt==parent)
        {
            continue;
        }
        drum[nxt][und]=drum[node][und]^muchii[node][i].second;
        drum[und][nxt]=drum[nxt][und];
        dfs(node,nxt);
    }
}
void calc(int N)
{
    for(int i=0; i<N; i++)
    {
        und=i;
        dfs(-1,i);
    }
}
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});
    }
    calc(N);
    long long sum=0;
    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;
                for(int i=0; i<opened.size(); i++)
                {
                    sum+=drum[j][opened[i]];
                }
                opened.push_back(j);
            }
        }
        else
        {
            for(int j=x; j<=y; j++)
            {
                if(!activ[j])
                {
                    continue;
                }
                activ[j]=0;
                for(int i=0; i<opened.size(); i++)
                {
                    sum-=drum[j][opened[i]];
                    if(opened[i]==j)
                    {
                        opened.erase(opened.begin()+i);
                        i--;
                    }
                }
            }
        }
        ans.push_back(sum);
    }
    return ans;
}

