#include "happy.h"
#include <bits/stdc++.h>
using namespace std;

#define pb push_back
#define sz(x) (x).size()
#define all(x) (x).begin(), (x).end()
#define rall(x) (x).rbegin(), (x).rend()
#define fi first
#define se second

using ll = long long;

const int N = 3e5 + 67;
vector<pair<int, int>> g[N];
int d[N];
bool is_act[N];
int cnt[2];

void dfs(int u, int p = -1) {
    for (pair<int, int> nxt: g[u]) {
        int v = nxt.fi;
        if (v != p) {
            d[v] = d[u] ^ nxt.se;
            dfs(v, u);
        }
    }
}

vector <ll> happy (int n, int q, vector <int> u, vector <int> v, vector <int> h, vector <int> tp, vector <int> l, vector <int> r) {
    for (int i = 0; i < n - 1; i++) {
        g[u[i]].pb({v[i], h[i]});
        g[v[i]].pb({u[i], h[i]});
    }
    dfs(0);
    vector<ll> ans;
    if (n <= 2000) {
        for (int i = 0; i < q; i++) {
            if (tp[i] == 1) {
                for (int j = l[i]; j <= r[i]; j++) {
                    is_act[j] = 1;
                }
            } else {
                for (int j = l[i]; j <= r[i]; j++) {
                    is_act[j] = 0;
                }
            }
            ll sm = 0;
            for (int u = 0; u < n; u++) {
                if (!is_act[u]) continue;
                for (int v = u + 1; v < n; v++) {
                    if (is_act[v]) sm += 1LL * (d[u] ^ d[v]);
                }
            }
            ans.pb(sm);
        }
    } else {
        int curans = 0;
        for (int i = 0; i < q; i++) {
            int u = l[i];
            if (tp[i] == 1) {
                if (!is_act[u]) {
                    curans += cnt[d[u] ^ 1];
                    cnt[d[u]]++;
                    is_act[u] = 1;
                }
            } else {
                if (is_act[u]) {
                    curans -= cnt[d[u] ^ 1];
                    cnt[d[u]]--;
                    is_act[u] = 0;
                }
            }
        }
        ans.pb(curans);
    }
    return ans;
}