SPOJ MSE06H

fenwick
spoj

#1

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