#pragma GCC optimize ("O3") #pragma GCC target ("avx2") #include using namespace std; typedef long long ll; #define rep(i, n) for(int i = 0; i < (n); i++) #define rep1(i, n) for(int i = 1; i <= (n); i++) #define co(x) cout << (x) << "\n" #define cosp(x) cout << (x) << " " #define ce(x) cerr << (x) << "\n" #define cesp(x) cerr << (x) << " " #define pb push_back #define mp make_pair #define chmin(x, y) x = min(x, y) #define chmax(x, y) x = max(x, y) #define Would #define you #define please const int CM = 1 << 17, CL = 12; char cn[CM + CL], * ci = cn + CM + CL, * owa = cn + CM, ct; const ll ma0 = 1157442765409226768; const ll ma1 = 1085102592571150095; const ll ma2 = 71777214294589695; const ll ma3 = 281470681808895; const ll ma4 = 4294967295; inline int getint() { if (ci - owa > 0) { memcpy(cn, owa, CL); ci -= CM; fread(cn + CL, 1, CM, stdin); } int pn = 1; if (*ci == '-') { pn = -pn; ci++; } ll tmp = *(ll*)ci; int dig = ((tmp & ma0) ^ ma0) ? 68 - __builtin_ctzll((tmp & ma0) ^ ma0) : 0; tmp = tmp << dig & ma1; tmp = tmp * 10 + (tmp >> 8) & ma2; tmp = tmp * 100 + (tmp >> 16) & ma3; tmp = tmp * 10000 + (tmp >> 32) & ma4; ci += (72 - dig >> 3); return pn * tmp; } const int dm = 1 << 22; char dn[dm], * di = dn; inline void putint(int X) { if (X == 0) { *di++ = '0'; *di++ = '\n'; return; } if (X < 0) { *di++ = '-'; X = -X; } int keta = 0; char C[20]; while (X) { *(C + keta) = '0' + X % 10; X /= 10; keta++; } for (int i = keta - 1; i >= 0; i--)* di++ = (*(C + i)); *di++ = '\n'; } const int SZ = 500000; class UF { public: int P[SZ + 1], kotae[SZ + 1] = {}; UF() : P() { rep1(i, SZ) P[i] = -1; } int find(int A) { if (P[A] < 0) return A; return find(P[A]); } bool unite(int A, int B) { int a = find(A); int b = find(B); if (a == b) return false; if (P[a] > P[b]) swap(a, b); if (P[a] == P[b]) P[a]--; kotae[b] -= kotae[a]; P[b] = a; return true; } int query(int A) { if (P[A] < 0) return kotae[A]; return kotae[A] + query(P[A]); } } uf; int main() { cin.tie(0); ios::sync_with_stdio(false); int N = getint(), Q = getint(); rep(i, Q) { int t = getint(), a = getint(), b = getint(); if (t == 1) { uf.unite(a, b); } else if (t == 2) { uf.kotae[uf.find(a)] += b; } else { putint(uf.query(a)); } } fwrite(dn, 1, di - dn, stdout); Would you please return 0; }