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

using namespace std;
using ll = long long;

vector <vector <pair <int, int>>> g;
vector <vector <int>> res;
vector <bool> used;

void dfs(int s, int v, int x) {
  used[v] = 1;
  res[s][v] = x;
  for (auto [u, h] : g[v]) {
    if (!used[u]) {
      dfs(s, u, x ^ h);
    }
  }
}

std::vector <ll> 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) {
  g.resize(n);
  res.resize(n, vector <int> (n));
  used.resize(n);
  for (int i = 0; i < n-1; i++) {
    g[u[i]].emplace_back(v[i], h[i]);
    g[v[i]].emplace_back(u[i], h[i]);
  }
  for (int i = 0; i < n; i++) {
    fill(used.begin(), used.end(), 0);
    dfs(i, i, 0);
  }
  vector <ll> ans;
  vector <bool> open(n);
  for (int i = 0; i < q; i++) {
    if (t[i] == 2) {
      for (int j = l[i]; j <= r[i]; j++) {
        open[j] = 0;
      }
    } else {
      for (int j = l[i]; j <= r[i]; j++) {
        open[j] = 1;
      }
    }
    ll sum = 0;
    for (int u = 0; u < n; u++) {
      for (int v = u + 1; v < n; v++) {
        if (open[u] && open[v]) {
          sum += res[u][v];
        }
      }
    }
    ans.push_back(sum);
  }
  return ans;
}
