#include #ifdef WIN32 #define getchar_fast _getchar_nolock #else #define getchar_fast getchar_unlocked #endif int ri() { int r = 0, c, s = 0; for (;;) { c = getchar(); if (c == '-') { s = 1; break; } if (c >= '0' && c <= '9') { r = c - '0'; break; } } for (;;) { c = getchar(); if (c < '0' || c > '9') break; r = r * 10 + c - '0'; } return s ? -r : r; } struct UnionFind { std::vector parent; // -size if it's root std::vector plus; UnionFind (int n) : parent(n, -1), plus(n) {} std::pair root(int i) { if (parent[i] < 0) return {i, plus[i]}; auto tmp = root(parent[i]); plus[i] += tmp.second - plus[tmp.first]; return {parent[i] = tmp.first, plus[tmp.first] + plus[i]}; } void add_union(int i, int val) { plus[root(i).first] += val; } int get(int i) { return root(i).second; } void unite(int i, int j) { i = root(i).first; j = root(j).first; if (i == j) return; if (parent[i] > parent[j]) std::swap(i, j); parent[i] += parent[j]; parent[j] = i; plus[j] -= plus[i]; } }; int main() { int n = ri(); int q = ri(); UnionFind uni(n); for (int i = 0; i < q; i++) { int t = ri(), a = ri() - 1, b = ri(); if (t == 1) uni.unite(a, b - 1); else if (t == 2) uni.add_union(a, b); else printf("%d\n", uni.get(a)); } return 0; }