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

using namespace std;
using ll = long long;
vector<vector<pair<ll, ll>>> g;
vector<bool> col; // 0 -0, 1 - 1

void dfs1(ll v, bool c, ll p) {
  col[v] = c;
  for (auto [u, h] : g[v]) {
    if (u == p) continue;
    if (h == 0) {
      dfs1(u, c, v);
    } else {
      dfs1(u, !c, v);
    }
  }
}

vector <long long int> happy (ll n, ll q, vector <ll> u,
  vector <ll> v, vector <ll> h, vector <ll> t, vector <ll> l, vector <ll> r) {
  g.resize(n);
  col.resize(n);
  for (ll i = 0; i < n - 1; i++) {
    g[v[i]].emplace_back(u[i], h[i]);
    g[u[i]].emplace_back(v[i], h[i]);
  }
  dfs1(0, 1, -1);
  ll cnt = 0; // col[j] = 1
  ll ans = 0;
  ll cntOpen = 0;
  vector<ll> ret(q);
  vector<bool> open(n, 0);
  for (ll i = 0; i < q; i++) {
    ll j = l[i];

    if (t[i] == 1) {
      if (open[j]) continue;
      open[j] = 1;
      cntOpen++;
      if (col[j]) {
        cnt++;
        ans += cntOpen - cnt;
      } else {
        ans += cnt;
      }
    } else {
      if (!open[j]) continue;
      open[j] = 0;
      cntOpen--;
      if (col[j]) {
        cnt--;
        ans -= cntOpen - cnt;
      } else {
        ans -= cnt;
      }
    }
    ret[i] = ans;
  }
  return ret;
}