import
java.util.ArrayList;
import
java.util.List;
public
class
FenwickTree2D {
static
void
update_y(
int
x,
int
y,
int
ele, List<List<Integer>> bit,
int
n) {
while
(y <= n) {
bit.get(x).set(y, bit.get(x).get(y) + ele);
y += (y & -y);
}
}
static
void
update(
int
x,
int
y,
int
ele, List<List<Integer>> bit,
int
n) {
while
(x <= n) {
update_y(x, y, ele, bit, n);
x += (x & -x);
}
}
static
int
getSum_y(
int
x,
int
y, List<List<Integer>> bit) {
int
sum =
0
;
while
(y >
0
) {
sum += bit.get(x).get(y);
y -= (y & -y);
}
return
sum;
}
static
int
getSum(
int
x,
int
y, List<List<Integer>> bit) {
int
sum =
0
;
while
(x >
0
) {
sum += getSum_y(x, y, bit);
x -= (x & -x);
}
return
sum;
}
public
static
void
main(String[] args) {
int
n =
3
;
List<List<Integer>> mat =
new
ArrayList<>(n +
1
);
List<List<Integer>> bit =
new
ArrayList<>(n +
1
);
for
(
int
i =
0
; i <= n; i++) {
mat.add(
new
ArrayList<>(List.of(
0
,
0
,
0
,
0
)));
bit.add(
new
ArrayList<>(List.of(
0
,
0
,
0
,
0
)));
}
List<List<Integer>> queries = List.of(
List.of(
1
,
2
,
0
,
2
),
List.of(
2
,
0
,
0
,
2
,
2
),
List.of(
1
,
0
,
0
,
1
),
List.of(
2
,
0
,
0
,
2
,
0
)
);
for
(List<Integer> query : queries) {
if
(query.get(
0
) ==
1
) {
int
x = query.get(
1
) +
1
;
int
y = query.get(
2
) +
1
;
int
ele = query.get(
3
);
update(x, y, ele - mat.get(x).get(y), bit, n);
mat.get(x).set(y, ele);
}
else
{
int
x1 = query.get(
1
) +
1
;
int
y1 = query.get(
2
) +
1
;
int
x2 = query.get(
3
) +
1
;
int
y2 = query.get(
4
) +
1
;
int
bottomRight = getSum(x2, y2, bit);
int
topRight = getSum(x1 -
1
, y2, bit);
int
bottomLeft = getSum(x2, y1 -
1
, bit);
int
topLeft = getSum(x1 -
1
, y1 -
1
, bit);
System.out.print(bottomRight - topRight - bottomLeft + topLeft +
" "
);
}
}
}
}