/*
  _      __        __         __        ____                    ___    _                   __
 | | /| / / ___ _ / /_ ____  / /       / __ \  ___  ___        / _ \  (_) ___  ____ ___   / /
 | |/ |/ / / _ `// __// __/ / _ \     / /_/ / / _ \/ -_)      / ___/ / / / -_)/ __// -_) /_/ 
 |__/|__/  \_,_/ \__/ \__/ /_//_/     \____/ /_//_/\__/      /_/    /_/  \__/ \__/ \__/ (_)  
                                                                                             
*/
 
#pragma GCC optimize("03")
#pragma GCC optimize("unroll-loops")
#include <bits/stdc++.h>
#define ll long long
#define ld long double
#define all(v) v.begin(), v.end()
#define allr(v, l, r) v.begin() + l, v.begin() + r;
#define endl "\n"
#define newline cout << endl;
using namespace std;

// constants
const int INF = 1e9;

// functions

// structs
struct graph
{
    vector <vector <pair <int, int> > g;
    vector <ll> dist;
    
    void cleanse()
    {
        fill(all(dist), 0LL);
    }
    
    graph(int n)
    {
        g.resize(n);
        dist.resize(n);
    }
    
    void adde(int u, int v, int w)
    {
        g[u].push_back({v, w});
        g[v].push_back({u, w});
    }
    
    void DFS(int v, int P)
    {
        for (auto [u, d] : g[v])
        {
            if (u == P)
            {
                continue;
            }
            
            dist[u] = (dist[v] ^ (1LL * d)); 
            DFS(u, v);
        }
    }
};

// globals

// notes
/*
 -stuff you should look for-
* int overflow, array bounds
* special cases (n=1?)
* do something instead of nothing and stay organized
* WRITE STUFF DOWN
* DON'T GET STUCK ON ONE APPROACH

continue - skip the rest in the loop
*/

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)
{
    graph G(N);
    for (int i = 0; i < N - 1; i++)
    {
        G.adde(u[i], v[i], h[i]);    
    }
    
    vector <vector <ll> > dist(N, vector <ll>(N, 0LL));
    for (int i = 0; i < n; i++)
    {
        G.cleanse();
        G.DFS(i, -1);
        dist = G.dist;
    }
    vector <bool> active(N, false);
    vector <ll> res(Q + 1);
    for (int i = 0; i < Q; i++)
    {
        int type = t[i], u = l[i], v = r[i];
        
        if (type == 1)
        {
            for (int j = u; j <= v; j++)
            {
                active[j] = true;
            }
        }
        else 
        {
            for (int j = u; j <= v; j++)
            {
                active[j] = false;
            }
        }
        
        ll cur = 0LL;
        for (int x = 0; x < N; x++)
        {
            if (!active[x])
            {
                continue;
            }
            
            for (int y = x + 1; y < N; y++)
            {
                if (!active[y])
                {
                    continue;
                }
                
                cur += 1LL * dist[x][y];
            }
        }
    }
    
    return res;
}

/*
$$$$$$$$\ $$$$$$$$\ 
$$  _____|\____$$  |
$$ |          $$  / 
$$$$$\       $$  /  
$$  __|     $$  /   
$$ |       $$  /    
$$$$$$$$\ $$$$$$$$\ 
\________|\________|
*/