Hackerearth ques - help

Hey can anyone explain me following part of this question.

map<ll,ll>M[100011];
void join(int u,int v) {
    u = f(u);
    v = f(v);
    if(sz[u] < sz[v]) swap(u, v);
    sz[u] += sz[v];
    pa[v] = u;
    for(auto x:M[v]) {
        for(int j=-D;j<=D;j++) {
            if(M[u].find(x.f+j)!=M[u].end()) {
                ans+=x.s*M[u][x.f+j];
            }
        }
    }
    for(auto x:M[v]) {
        M[u][x.f]+=x.s;
    }
}

Other part is same as general implementation as well as constraints.