void update (int a, int x, int y){ int ix; x++; y++; for(; y < (size+1); y += LOW_BIT(y)) for(ix = x; ix < (size+1); ix += LOW_BIT(ix)) cum[ix-1][y-1] += a; }
int cumulative (int x, int y) { int res, ix; res = 0; x++; y++; for(; y > 0; y -= LOW_BIT(y)) for(ix = x; ix > 0; ix -= LOW_BIT(ix)) res += cum[ix-1][y-1]; return res; }
/* compute sum 0 < i < x, 0 < j < y */ int cumulative (int x, int y) { int res, ix; res = 0; x++; y++; for(; y > 0; y -= LOW_BIT(y)) for(ix = x; ix > 0; ix -= LOW_BIT(ix)) res += cum[ix-1][y-1]; //printf ("cumul (%d, %d) = %d \n", x, y, res); return res; }
int sum (int x1, int y1, int x2, int y2) { int res, ix1, ix2, iy1, iy2; res = 0; for(iy2 = y2+1; iy2 > y1; iy2 -= LOW_BIT(iy2)) { for (ix2 = x2+1; ix2 > x1; ix2 -= LOW_BIT(ix2)) res += cum[ix2-1][iy2-1]; for (ix1 = x1; ix1 > ix2; ix1 -= LOW_BIT(ix1)) res -= cum[ix1-1][iy2-1]; } for(iy1 = y1; iy1 > iy2; iy1 -= LOW_BIT(iy1)) { for (ix2 = x2+1; ix2 > x1; ix2 -= LOW_BIT(ix2)) res -= cum[ix2-1][iy1-1]; for (ix1 = x1; ix1 > ix2; ix1 -= LOW_BIT(ix1)) res += cum[ix1-1][iy1-1]; } return res; }