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

using namespace std;

const int MAX_N = 3e5;

vector<pair<int, long long>> nbrs[MAX_N];

void dfs(int u, vector<long long>& dist)
{
    for(auto [v, w] : nbrs[u])
    {
        if(dist[v] != -1) continue;
        dist[v] = dist[u] ^ w;
        dfs(v, dist);
    }
}

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) {
    int n = N, q = Q;
    vector<long long> ansv;
    for(int i = 0; i < n - 1; ++i) {
        nbrs[u[i]].push_back({v[i], h[i]});
        nbrs[v[i]].push_back({u[i], h[i]});
    }
    unordered_set<int> s;
    for(int i = 0; i < q; ++i) {
        if(t[i] == 1)
        {
            for(int j = l[i]; j <= r[i]; ++j)
            {
                s.insert(j);
            }
        }
        else
        {
            for(int j = l[i]; j <= r[i]; ++j)
            {
                s.erase(j);
            }
        }
        long long ans = 0;
        for(auto x : s) {
            vector<long long> dist(n, -1);
            dist[x] = 0;
            dfs(x, dist);
            for(auto y : s) {
                ans += dist[y];
                //cout << "OT " << x << " do " << y << " " << dist[y] << endl;
            }

        }
        //cout << endl;
        ansv.push_back(ans / 2);
    }
    return ansv;
}
