#include <iostream>
#include <vector>
using namespace std;
#define ll long long

vector<vector<pair<int, ll>>> adj;
vector<int> ha;
vector<bool> open;

ll dfs(int st, int par, ll x){
    ll ans = 0;
    if(open[st]) ans = x;
    for(auto i : adj[st]){
        if(i.first == par) continue;
        ans += dfs(i.first, st, x^i.second);
    }
    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){

    adj.assign(N, {});

    for(int i = 0; i<N-1; i++){
        adj[u[i]].push_back({v[i], h[i]});
        adj[v[i]].push_back({u[i], h[i]});
    }



    vector<long long> ret;

    open.assign(N, 0);
    for(int i = 0; i<Q; i++){
        if(t[i] == 1){
            for(int j = l[i]; j<=r[i]; j++) open[j] = 1;
        }
        else{
            for(int j = l[i]; j<=r[i]; j++) open[j] = 0;
        }
        ll ans = 0;
        for(int j = 0; j<N; j++){
            if(open[j])ans += dfs(j, -1, 0);
        }
        ret.push_back(ans/2);
    }

    return ret;
}