#include <bits/stdc++.h>
using namespace std;
#define ll long long
int n;
vector<int> a, flat;
vector<vector<int>> adj;
struct LCA
{
vector<vector<int>> par;
const int lg = 18;
vector<int> dist; // distance from the root
vector<int> in, out;
int t = 0;
LCA(int n)
{
in = out = dist = vector<int>(n + 4);
par = vector<vector<int>>(n + 4, vector<int>(lg + 5));
dist[1] = 0;
dfs(1, 1);
pre(n);
}
void dfs(int i, int p)
{
dist[i] = dist[p] + (i != 1);
flat.push_back(i);
in[i] = t++;
par[i][0] = p;
for (auto j : adj[i])
{
if (j != p)
dfs(j, i);
}
flat.push_back(i);
out[i] = t++;
}
bool isparent(int v, int p)
{ // is p parent of v
return in[p] <= in[v] and out[p] >= out[v];
}
void pre(int n)
{
for (int i = 1; i < lg; i++)
{
for (int f = 1; f <= n; f++)
{
par[f][i] = par[par[f][i - 1]][i - 1];
}
}
}
int lca(int a, int b)
{
if (isparent(a, b))
return b;
if (isparent(b, a))
return a;
int ret = a;
for (int i = lg - 1; ~i; i--)
{
if (!isparent(b, par[ret][i]))
ret = par[ret][i];
}
return par[ret][0];
}
int getdist(int u, int p)
{ // how many nodes are there int path from u to p
return dist[u] - dist[p] + 1;
}
};
vector<int> in, out;
struct Node
{
int frq[31]{};
};
struct Segtree
{
vector<Node> tree;
Node neutral = Node();
Segtree(int n, vector<int> &v)
{
int sz = 1;
while (sz <= n)
{
sz *= 2;
}
tree.resize(sz * 2);
build(1, 0, n - 1, v);
}
Node Single(int data)
{
Node ret;
int g = abs(data);
for (int i = 0; i < 31; i++)
{
ret.frq[i] = (data < 0 ? -1 : 1) * ((g & (1ll << i)) > 0);
}
return ret;
}
Node Merge(Node a, Node b)
{
Node ret;
for (int i = 0; i < 31; i++)
{
ret.frq[i] = a.frq[i] + b.frq[i];
}
return ret;
}
void build(int x, int lx, int rx, const vector<int> &v)
{
if (lx == rx)
return tree[x] = Single(v[lx]), void();
int m = (lx + rx) >> 1;
build(x * 2, lx, m, v);
build(x * 2 + 1, m + 1, rx, v);
tree[x] = Merge(tree[x * 2], tree[x * 2 + 1]);
}
void change(int x, int lx, int rx, const int &i, const int &val)
{
if (lx == rx)
return tree[x] = Single(val), void();
int m = (lx + rx) >> 1;
if (i <= m)
change(x * 2, lx, m, i, val);
else
change(x * 2 + 1, m + 1, rx, i, val);
tree[x] = Merge(tree[x * 2], tree[x * 2 + 1]);
}
Node query(int x, int lx, int rx, const int &l, const int &r)
{
if (lx > r or rx < l)
return neutral;
if (lx >= l and rx <= r)
return tree[x];
int m = (lx + rx) >> 1;
return Merge(query(x * 2, lx, m, l, r), query(x * 2 + 1, m + 1, rx, l, r));
}
};
int main()
{
ios_base::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n;
a = vector<int>(n + 4);
for (int i = 1; i <= n; i++)
{
cin >> a[i];
}
adj = vector<vector<int>>(n + 4);
for (int i = 0; i < n - 1; i++)
{
int u, v;
cin >> u >> v;
adj[u].push_back(v);
adj[v].push_back(u);
}
LCA lca(n);
in = lca.in;
out = lca.out;
vector<int> tmp(flat.size());
bool vis[n + 4]{};
for (int i = 0; i < flat.size(); i++)
{
tmp[i] = ((vis[flat[i]] ? -a[flat[i]] : a[flat[i]]));
vis[flat[i]] = true;
}
Segtree tree(flat.size(), tmp);
int q;
cin >> q;
while (q--)
{
int op;
cin >> op;
if (op == 1)
{
int u, v;
cin >> u >> v;
int lc = lca.lca(u, v);
auto resu = tree.query(1, 0, flat.size() - 1, in[lc], in[u]).frq;
auto resv = tree.query(1, 0, flat.size() - 1, in[lc], in[v]).frq;
int tot = lca.getdist(u, lc) + lca.getdist(v, lc) - 1;
ll ans = 0;
for (int i = 0; i < 31; i++)
{
if (resu[i] + resv[i] - ((a[lc] & (1ll << i)) > 0) == tot)
ans += (1ll << i);
}
cout << ans << '\n';
}
else
{
int u, x;
cin >> u >> x;
a[u] = x;
tree.change(1, 0, flat.size() - 1, in[u], x);
tree.change(1, 0, flat.size() - 1, out[u], -x);
}
}
}
I2luY2x1ZGUgPGJpdHMvc3RkYysrLmg+CnVzaW5nIG5hbWVzcGFjZSBzdGQ7CiNkZWZpbmUgbGwgbG9uZyBsb25nCmludCBuOwp2ZWN0b3I8aW50PiBhLCBmbGF0Owp2ZWN0b3I8dmVjdG9yPGludD4+IGFkajsKIApzdHJ1Y3QgTENBCnsKICAgIHZlY3Rvcjx2ZWN0b3I8aW50Pj4gcGFyOwogICAgY29uc3QgaW50IGxnID0gMTg7CiAgICB2ZWN0b3I8aW50PiBkaXN0OyAvLyBkaXN0YW5jZSBmcm9tIHRoZSByb290CiAgICB2ZWN0b3I8aW50PiBpbiwgb3V0OwogICAgaW50IHQgPSAwOwogICAgTENBKGludCBuKQogICAgewogICAgICAgIGluID0gb3V0ID0gZGlzdCA9IHZlY3RvcjxpbnQ+KG4gKyA0KTsKICAgICAgICBwYXIgPSB2ZWN0b3I8dmVjdG9yPGludD4+KG4gKyA0LCB2ZWN0b3I8aW50PihsZyArIDUpKTsKICAgICAgICBkaXN0WzFdID0gMDsKICAgICAgICBkZnMoMSwgMSk7CiAgICAgICAgcHJlKG4pOwogICAgfQogICAgdm9pZCBkZnMoaW50IGksIGludCBwKQogICAgewogICAgICAgIGRpc3RbaV0gPSBkaXN0W3BdICsgKGkgIT0gMSk7CiAgICAgICAgZmxhdC5wdXNoX2JhY2soaSk7CiAgICAgICAgaW5baV0gPSB0Kys7CiAgICAgICAgcGFyW2ldWzBdID0gcDsKICAgICAgICBmb3IgKGF1dG8gaiA6IGFkaltpXSkKICAgICAgICB7CiAgICAgICAgICAgIGlmIChqICE9IHApCiAgICAgICAgICAgICAgICBkZnMoaiwgaSk7CiAgICAgICAgfQogICAgICAgIGZsYXQucHVzaF9iYWNrKGkpOwogICAgICAgIG91dFtpXSA9IHQrKzsKICAgIH0KICAgIGJvb2wgaXNwYXJlbnQoaW50IHYsIGludCBwKQogICAgeyAvLyBpcyBwIHBhcmVudCBvZiB2CiAgICAgICAgcmV0dXJuIGluW3BdIDw9IGluW3ZdIGFuZCBvdXRbcF0gPj0gb3V0W3ZdOwogICAgfQogICAgdm9pZCBwcmUoaW50IG4pCiAgICB7CiAgICAgICAgZm9yIChpbnQgaSA9IDE7IGkgPCBsZzsgaSsrKQogICAgICAgIHsKICAgICAgICAgICAgZm9yIChpbnQgZiA9IDE7IGYgPD0gbjsgZisrKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBwYXJbZl1baV0gPSBwYXJbcGFyW2ZdW2kgLSAxXV1baSAtIDFdOwogICAgICAgICAgICB9CiAgICAgICAgfQogICAgfQogCiAgICBpbnQgbGNhKGludCBhLCBpbnQgYikKICAgIHsKICAgICAgICBpZiAoaXNwYXJlbnQoYSwgYikpCiAgICAgICAgICAgIHJldHVybiBiOwogICAgICAgIGlmIChpc3BhcmVudChiLCBhKSkKICAgICAgICAgICAgcmV0dXJuIGE7CiAgICAgICAgaW50IHJldCA9IGE7CiAgICAgICAgZm9yIChpbnQgaSA9IGxnIC0gMTsgfmk7IGktLSkKICAgICAgICB7CiAgICAgICAgICAgIGlmICghaXNwYXJlbnQoYiwgcGFyW3JldF1baV0pKQogICAgICAgICAgICAgICAgcmV0ID0gcGFyW3JldF1baV07CiAgICAgICAgfQogICAgICAgIHJldHVybiBwYXJbcmV0XVswXTsKICAgIH0KICAgIGludCBnZXRkaXN0KGludCB1LCBpbnQgcCkKICAgIHsgLy8gaG93IG1hbnkgbm9kZXMgYXJlIHRoZXJlIGludCBwYXRoIGZyb20gdSB0byBwCiAgICAgICAgcmV0dXJuIGRpc3RbdV0gLSBkaXN0W3BdICsgMTsKICAgIH0KfTsKdmVjdG9yPGludD4gaW4sIG91dDsKIApzdHJ1Y3QgTm9kZQp7CiAgICBpbnQgZnJxWzMxXXt9Owp9OwogCnN0cnVjdCBTZWd0cmVlCnsKICAgIHZlY3RvcjxOb2RlPiB0cmVlOwogICAgTm9kZSBuZXV0cmFsID0gTm9kZSgpOwogICAgU2VndHJlZShpbnQgbiwgdmVjdG9yPGludD4gJnYpCiAgICB7CiAgICAgICAgaW50IHN6ID0gMTsKICAgICAgICB3aGlsZSAoc3ogPD0gbikKICAgICAgICB7CiAgICAgICAgICAgIHN6ICo9IDI7CiAgICAgICAgfQogICAgICAgIHRyZWUucmVzaXplKHN6ICogMik7CiAgICAgICAgYnVpbGQoMSwgMCwgbiAtIDEsIHYpOwogICAgfQogICAgTm9kZSBTaW5nbGUoaW50IGRhdGEpCiAgICB7CiAgICAgICAgTm9kZSByZXQ7CiAgICAgICAgaW50IGcgPSBhYnMoZGF0YSk7CiAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgPCAzMTsgaSsrKQogICAgICAgIHsKICAgICAgICAgICAgcmV0LmZycVtpXSA9IChkYXRhIDwgMCA/IC0xIDogMSkgKiAoKGcgJiAoMWxsIDw8IGkpKSA+IDApOwogICAgICAgIH0KICAgICAgICByZXR1cm4gcmV0OwogICAgfQogICAgTm9kZSBNZXJnZShOb2RlIGEsIE5vZGUgYikKICAgIHsKICAgICAgICBOb2RlIHJldDsKICAgICAgICBmb3IgKGludCBpID0gMDsgaSA8IDMxOyBpKyspCiAgICAgICAgewogICAgICAgICAgICByZXQuZnJxW2ldID0gYS5mcnFbaV0gKyBiLmZycVtpXTsKICAgICAgICB9CiAgICAgICAgcmV0dXJuIHJldDsKICAgIH0KICAgIHZvaWQgYnVpbGQoaW50IHgsIGludCBseCwgaW50IHJ4LCBjb25zdCB2ZWN0b3I8aW50PiAmdikKICAgIHsKICAgICAgICBpZiAobHggPT0gcngpCiAgICAgICAgICAgIHJldHVybiB0cmVlW3hdID0gU2luZ2xlKHZbbHhdKSwgdm9pZCgpOwogICAgICAgIGludCBtID0gKGx4ICsgcngpID4+IDE7CiAgICAgICAgYnVpbGQoeCAqIDIsIGx4LCBtLCB2KTsKICAgICAgICBidWlsZCh4ICogMiArIDEsIG0gKyAxLCByeCwgdik7CiAgICAgICAgdHJlZVt4XSA9IE1lcmdlKHRyZWVbeCAqIDJdLCB0cmVlW3ggKiAyICsgMV0pOwogICAgfQogICAgdm9pZCBjaGFuZ2UoaW50IHgsIGludCBseCwgaW50IHJ4LCBjb25zdCBpbnQgJmksIGNvbnN0IGludCAmdmFsKQogICAgewogICAgICAgIGlmIChseCA9PSByeCkKICAgICAgICAgICAgcmV0dXJuIHRyZWVbeF0gPSBTaW5nbGUodmFsKSwgdm9pZCgpOwogICAgICAgIGludCBtID0gKGx4ICsgcngpID4+IDE7CiAgICAgICAgaWYgKGkgPD0gbSkKICAgICAgICAgICAgY2hhbmdlKHggKiAyLCBseCwgbSwgaSwgdmFsKTsKICAgICAgICBlbHNlCiAgICAgICAgICAgIGNoYW5nZSh4ICogMiArIDEsIG0gKyAxLCByeCwgaSwgdmFsKTsKICAgICAgICB0cmVlW3hdID0gTWVyZ2UodHJlZVt4ICogMl0sIHRyZWVbeCAqIDIgKyAxXSk7CiAgICB9CiAgICBOb2RlIHF1ZXJ5KGludCB4LCBpbnQgbHgsIGludCByeCwgY29uc3QgaW50ICZsLCBjb25zdCBpbnQgJnIpCiAgICB7CiAgICAgICAgaWYgKGx4ID4gciBvciByeCA8IGwpCiAgICAgICAgICAgIHJldHVybiBuZXV0cmFsOwogICAgICAgIGlmIChseCA+PSBsIGFuZCByeCA8PSByKQogICAgICAgICAgICByZXR1cm4gdHJlZVt4XTsKICAgICAgICBpbnQgbSA9IChseCArIHJ4KSA+PiAxOwogICAgICAgIHJldHVybiBNZXJnZShxdWVyeSh4ICogMiwgbHgsIG0sIGwsIHIpLCBxdWVyeSh4ICogMiArIDEsIG0gKyAxLCByeCwgbCwgcikpOwogICAgfQp9OwppbnQgbWFpbigpCnsKICAgIGlvc19iYXNlOjpzeW5jX3dpdGhfc3RkaW8oMCk7CiAgICBjaW4udGllKDApOwogICAgY291dC50aWUoMCk7CiAgICBjaW4gPj4gbjsKICAgIGEgPSB2ZWN0b3I8aW50PihuICsgNCk7CiAgICBmb3IgKGludCBpID0gMTsgaSA8PSBuOyBpKyspCiAgICB7CiAgICAgICAgY2luID4+IGFbaV07CiAgICB9CiAgICBhZ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ID0gbGNhLmdldGRpc3QodSwgbGMpICsgbGNhLmdldGRpc3QodiwgbGMpIC0gMTsKICAgICAgICAgICAgbGwgYW5zID0gMDsKICAgICAgICAgICAgZm9yIChpbnQgaSA9IDA7IGkgPCAzMTsgaSsrKQogICAgICAgICAgICB7CiAgICAgICAgICAgICAgICBpZiAocmVzdVtpXSArIHJlc3ZbaV0gLSAoKGFbbGNdICYgKDFsbCA8PCBpKSkgPiAwKSA9PSB0b3QpCiAgICAgICAgICAgICAgICAgICAgYW5zICs9ICgxbGwgPDwgaSk7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgY291dCA8PCBhbnMgPDwgJ1xuJzsKICAgICAgICB9CiAgICAgICAgZWxzZQogICAgICAgIHsKICAgICAgICAgICAgaW50IHUsIHg7CiAgICAgICAgICAgIGNpbiA+PiB1ID4+IHg7CiAgICAgICAgICAgIGFbdV0gPSB4OwogICAgICAgICAgICB0cmVlLmNoYW5nZSgxLCAwLCBmbGF0LnNpemUoKSAtIDEsIGluW3VdLCB4KTsKICAgICAgICAgICAgdHJlZS5jaGFuZ2UoMSwgMCwgZmxhdC5zaXplKCkgLSAxLCBvdXRbdV0sIC14KTsKICAgICAgICB9CiAgICB9Cn0K