/** 308. Range Sum Query 2d-mutable * 2016-7-3 by Mingyang * Continue to be a lunatic segment Tree*/classNummatrix {int[] tree; int[] nums; intm; intN; PublicNummatrix (int[] matrix) { if(Matrix.length = = 0 | | matrix[0].length = = 0)return; M=matrix.length; N= Matrix[0].length; Tree=New int[M+1] [N+1]; Nums=New int[M][n]; for(inti = 0; I < m; i++) { for(intj = 0; J < N; J + +{Update (I, J, Matrix[i][j]); } } } Public voidUpdateintRowintColintval) { if(m = = 0 | | n = = 0)return; intDelta = Val-Nums[row][col]; Nums[row][col]=Val; for(inti = row + 1; I <= m; i + = i & (-i)) { for(intj = col + 1; J <= N; J + = J & (-j)) {Tree[i][j]+=Delta; } } } Public intSumregion (intRow1,intCol1,intRow2,intcol2) { if(m = = 0 | | n = = 0)return0; returnSum (row2+1, col2+1) + sum (row1, col1)-sum (Row1, col2+1)-SUM (row2+1, col1); } Public intSumintRowintCol) { intsum = 0; for(inti = row; i > 0; I-= i & (-i)) { for(intj = col; J > 0; J-= J & (-j)) {sum+=Tree[i][j]; } } returnsum; }}//Time should be O (log (m) * log (n))
308. Range Sum Query 2d-mutable