#include <bits/stdc++.h>
#include "happy.h"

#define pb push_back
#define sz(x) (ll)x.size()
#define pw(x) ((1LL)<<(x))
#define all(x) x.begin(), x.end()

using ll = long long int;
using ld = long double;

using namespace 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){
    ll n, m;
    ll i, j;
    string s;
    n = N;
    ll q = Q;
    ll sum=0;
    vector<vector<pair<ll,ll>>> g(n+2);
    for (i=0;i<n-1;i++){
        g[u[i]].pb({v[i],h[i]});
        g[v[i]].pb({u[i],h[i]});
    }
    vector<ll> used(n+2,false);
    ll root = -1;
    vector<ll> answer(n+2,0);
    function<void(ll,ll,ll)> dfs = [&](ll u, ll pr, ll ans){
//        answer[root]+=ans;
//        answer[u]+=ans;
        if (used[u]) sum+=ans;
//        cout << u << " " << ans << endl;
        for (auto [v,w] : g[u]){
            if (v==pr) continue;
            dfs(v,u,ans^w);
        }
    };

    vector<ll> res;
//    dfs(1,-1,0);
    for (i=0;i<q;i++){
        if (t[i]==1){
            for (j=l[i];j<=r[i];j++){
                used[j] = true;
            }
        }else{
            for (j=l[i];j<=r[i];j++){
                used[j] = false;
            }
        }
        sum=0;
//        mp.clear();
        for (j=0;j<n;j++){
            if (!used[j]) continue;
            root = j;
            dfs(j,-1,0);
//        cout << sum << endl;
        }
        res.pb(sum/2);
//        cout << "---------------------------------------\n";
    }
    return res;
}