#include "happy.h"
#include "bits/stdc++.h"
using namespace std;
#define pb push_back
#define ll long long
#define ls(v) (ll)v.size()

const ll M = 35;
const ll N = 3e5 + 5;
ll pref[N][M];
ll suf[N][M];
ll op[N];

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) {
    ll n = N;
    ll q = Q;
    for(auto &it:u) it += 1;
    for(auto &it:v) it += 1;
    for(auto &it:L) it += 1;
    for(auto &it:R) it += 1;
    ll val = 0;
    for(ll i = 2;i<=n;++i){
        val ^= h[i - 2];
        for(ll j = 0;j<30;++j){
            pref[i][j] = pref[i - 1][j] + (val >> j & 1);
        }
    }
    val = 0;
    for(int i = n - 1;i;--i){
        val ^= h[i - 1];
        for(int j = 0;j<30;++j){
            suf[i][j] = suf[i + 1][j] + (val >> j & 1);
        }
    }
    ll ans = 0;
    vector<ll> answer;
    for(ll ii = 0;ii<q;++ii){
        ll k = L[ii];
        // cout << k << endl;
        if(t[ii] == 1){
            if(op[k] == 0){
                for(ll i = 1;i<k;++i){
                    for(ll j = 0;j<30;++j){
                        if(op[i] and pref[k][j] % 2 != pref[i][j] % 2) ans += (1 << j);
                    }
                }
                for(ll i = k + 1;i<=n;++i){
                    for(ll j = 0;j<30;++j){
                        if(op[i] and suf[k][j] % 2 != suf[i][j] % 2) ans += (1 << j);
                    }
                }
            }
            op[k] = 1;
        }
        else{
            if(op[k] == 1){
                for(ll i = 1;i<k;++i){
                    for(ll j = 0;j<30;++j){
                        if(op[i] and pref[k][j] % 2 != pref[i][j] % 2) ans -= (1 << j);
                    }
                }
                for(ll i = k + 1;i<=n;++i){
                    for(ll j = 0;j<30;++j){
                        if(op[i] and suf[k][j] % 2 != suf[i][j] % 2) ans -= (1 << j);
                    }
                }
            }
            op[k] = 0;
        }
        answer.pb(ans);
    }
    return answer;
}
/*
6 5
0 1 1
1 2 2
2 3 3
2 4 4
4 5 2
1 1 2
1 2 3
2 2 2
2 4 5
1 0 5
*/