SPOJ MSE06H

Can anyone explain the logic of
for(i = k-1; i >= 0; i = j) {
for(j = i; j >= 0 && roads[j].u == roads[i].u; j–) res += read(roads[j].v - 1);
for(j = i; j >= 0 && roads[j].u == roads[i].u; j–) add(roads[j].v, 1);
} Why are the innermost for loops used
The 2nd and the 3rd for loops??