并查集维护区间加
#include <bits/stdc++.h>
using namespace std;
const int N = 10005;
int n, m, d[N], p[N];
int find(int x){
if(p[x] == x || p[p[x]] == p[x]) return p[x];
int r = find(p[x]);
d[x] += d[p[x]];
p[x] = r;
return r;
}
int main(){
cin >> n >> m;
for(int i = 1; i <= n; i ++ ) p[i] = i;
while(m -- ){
int t, a, b;
cin >> t >> a >> b;
if(t == 1){
a = find(a), b = find(b);
if(a == b) continue;
d[a] -= d[b];
p[a] = b;
}
else {
a = find(a);
d[a] += b;
}
}
for(int i = 1; i <= n; i ++ ) {
if(i == find(i)) cout << d[i];
else cout << d[i] + d[p[i]];
cout << " ";
}
return 0;
}