#include <bits/stdc++.h>
using namespace std;
int n,m;
vector<int> fa,siz,ex;
vector<vector<int> > son;
void init (int n) {
fa.resize(n + 1);
iota(fa.begin(),fa.end(),0);
siz.resize(n + 1);
fill(siz.begin(),siz.end(),1);
son.resize(n + 1);
for (int i = 1; i <= n; i++)
son[i] = {i};
ex.resize(n + 1);
fill(ex.begin(),ex.end(),0);
}
int FIND(int x) {
if (fa[x] == x)
return x;
else
return fa[x] = FIND(fa[x]);
}
void merge(int u,int v) {
auto fu = FIND(u),fv = FIND(v);
if (fu == fv)
return;
if (siz[fu] < siz[fv])
swap(fu,fv);
for (auto i : son[fv]) {
if (i != fv)
ex[i] = ex[i] + ex[fv] - ex[fu];
else
ex[i] = ex[i] - ex[fu];
}
fa[fv] = fu;
siz[fu] += siz[fv];
son[fu].insert(son[fu].end(),son[fv].begin(),son[fv].end());
}
void add(int x,int v) {
auto fx = FIND(x);
ex[fx] += v;
}
int main() {
ios::sync_with_stdio(0);
cin.tie(0);
cout.tie(0);
cin >> n >> m;
init(n);
string s;
for (int i = 1; i <= m; i++) {
cin >> s;
if (s == "join") {
int x,y;
cin >> x >> y;
merge(x,y);
}
else {
if (s == "add") {
int x,v;
cin >> x >> v;
add(x,v);
}
else {
int x;
cin >> x;
auto fx = FIND(x);
if (x == fx)
cout << ex[x] << endl;
else
cout << ex[x] + ex[fx] << endl;
}
}
}
return 0;
}
并查集代码WA on test7