#include "happy.h"
#include "bits/stdc++.h"
using namespace std;
const int N = 3e5 + 5;
int dp[N];
int op[N];
vector<int>g[N];
#define ll long long
#define pb push_back
void dfs(int nd, int p){
    if(~p) dp[nd] = dp[p] + 1;
    for(auto it:g[nd]){
        if(it != p){
            dfs(it, nd);
        }
    }
}
vector <long long int> happy (int N, int Q, vector <int> u, vector <int> v, vector <int> h, vector <int> t, vector <int> l, vector <int> r) {
    int n = N;
    int q = Q;
    for(int i = 0;i<n - 1;++i){
        g[u[i]].pb(v[i]);
        g[v[i]].pb(u[i]);
    }
    dfs(0, -1);
    int odd, even;
    odd = even = 0;
    vector<ll> answer;
    ll ans = 0;
    for(int i = 0;i<q;++i){
        int k = l[i];
        if(t[i] == 1){
            if(!op[k]){
                op[k] = 1;
                if(dp[k] & 1) ans += even, odd += 1;
                else ans += odd, even += 1;
            }
        }
        else{
            if(op[k]){
                op[k] = 0;
                if(dp[k] & 1) odd -= 1, ans -= even;
                else even -= 1, ans -= odd;
            }
        }
        answer.pb(ans);
    }
    return answer;
}
