// Input format:
// First line contains n, m and q, where n and m - the size of the matrix , q is tne number of query.
// n, m, q are positive integer and must not exceed 10^5.
// The matrix is initially 0.
// q following lines contains queries of 2 types:
// 1 r c val - change the value of the cell (r, c) to val.
// 2 r1 c1 r2 c2 - output the sum of the cell inside the submatrix (r1, c1, r2, c2)
//
// r, r1, r2 must be positive integer and must not exceed n
// c, c1, c2 must be positive integer and must not exceed m
// val must be an integer fit in 32 bit inger.
// #define testing // turn on self testing mode. i.e. check with the naive solution
#include <bits/stdc++.h>
using namespace std;
using llong = long long;
struct Upd {
int r, c, val, id;
Upd(int r_, int c_, int v, int i): r(r_), c(c_), val(v), id(i) {
--r;
--c;
}
};
struct Qr {
int r1, c1, r2, c2, id;
Qr(int r1_, int c1_, int r2_, int c2_, int i)
: r1(r1_ - 1), c1(c1_ - 1), r2(r2_ - 1), c2(c2_ - 1), id(i)
{
if (r1 > r2) swap(r1, r2);
if (c1 > c2) swap(c1, c2);
}
};
int n, m, q;
vector<Upd> updates;
vector<Qr> queries;
void read(){
cin >> n >> m >> q;
updates.clear();
queries.clear();
for (int i = 0; i < q; ++i) {
int type; cin >> type;
if (type == 1) {
int r, c, v; cin >> r >> c >> v;
updates.emplace_back(r, c, v, i);
} else {
int r1, c1, r2, c2;
cin >> r1 >> c1 >> r2 >> c2;
queries.emplace_back(r1, c1, r2, c2, i);
}
}
}
vector<llong> solve() {
// first we need to change the update from "change to" to "change by" to use with BIT
map<pair<int, int>, int> old_values;
for (auto& [r, c, v, i]: updates) {
pair<int, int> p {r, c};
v -= old_values[p];
old_values[p] += v;
}
struct BIT {
vector<llong> sum;
vector<bool> is_changed;
vector<int> changed_elm;
BIT(int sz): sum(sz + 10), is_changed(sz + 10) {}
void roll_back() {
for (auto c: changed_elm) {
is_changed[c] = 0;
sum[c] = 0;
}
changed_elm.clear();
}
void upd(int p, int val) {
for (++p; p < (int)sum.size(); p += p & -p) {
sum[p] += val;
if (!is_changed[p]) {
is_changed[p] = true;
changed_elm.push_back(p);
}
}
}
llong get(int p) {
llong ans = 0;
for (++p; p > 0; p -= p & -p) ans += sum[p];
return ans;
}
llong get(int l, int r) {
return get(r) - get(l - 1);
}
};
vector<vector<Upd>> upd_it(2 * n);
vector<vector<Qr>> qr_it(2 * n);
for (auto u: updates) {
for (int p = n + u.r; p > 0; p >>= 1)
upd_it[p].push_back(u);
}
for (auto qr: queries) {
for (int l = qr.r1 + n, r = qr.r2 + n + 1; l < r; l >>= 1, r >>= 1) {
if (l & 1) qr_it[l++].push_back(qr);
if (r & 1) qr_it[--r].push_back(qr);
}
}
BIT bit(m);
vector<llong> ans(q);
for (int root = 1; root < 2 * n; ++root) {
auto& ui = upd_it[root];
auto& qi = qr_it[root];
if (ui.empty() or qi.empty()) continue;
int ptr = 0;
for (auto qr: qi) {
while (ptr < (int)ui.size() and ui[ptr].id < qr.id) {
bit.upd(ui[ptr].c, ui[ptr].val);
++ptr;
}
ans[qr.id] += bit.get(qr.c1, qr.c2);
}
bit.roll_back();
}
int new_size = 0;
for (auto qr: queries) {
ans[new_size++] = ans[qr.id];
}
ans.resize(new_size);
return ans;
}
mt19937 rng;
#define rand() ((int)(rng() >> 1))
void gen_test();
void check_small_test();
vector<llong> brute();
int main() {
#ifdef testing
// rng.seed((llong)(main) ^ (llong)time(0));
while (true) {
gen_test();
auto exp = brute();
auto ans = solve();
if (exp == ans) {
cout << "Nice" << endl;
continue;
}
cout << "failed " << endl;
for (auto i: ans) cout << i << '\n';
cout << "---------------------\n";
for (auto i: exp) cout << i << '\n';
return 1;
}
#endif
#ifdef LOCAL
freopen("main.inp", "r", stdin);
freopen("main.out", "w", stdout);
#endif
read();
// auto ans = brute();
auto ans = solve();
for (auto i: ans) cout << i << '\n';
return 0;
}
///////////////////////////////////////////////////////////////////////////////////
void gen_test() {
ofstream inp("main.inp");
n = rand() % 10 + 1;
m = rand() % 10 + 1;
q = rand() % 10 + 1;
inp << n << ' ' << m << ' ' << q << '\n';
updates.clear();
queries.clear();
for (int i = 0; i < q; ++i) {
int type = rand() & 1;
if (type == 1) {
int r, c, v;
r = rand() % n + 1;
c = rand() % m + 1;
v = rand() % 100;
updates.emplace_back(r, c, v, i);
inp << 1 << ' ' << r << ' ' << c << ' ' << v << '\n';
} else {
int r1, c1, r2, c2;
r1 = rand() % n + 1;
r2 = rand() % n + 1;
c1 = rand() % m + 1;
c2 = rand() % m + 1;
queries.emplace_back(r1, c1, r2, c2, i);
inp << 2 << ' ' << r1 << ' ' << c1 << ' ' << r2 << ' ' << c2 << '\n';
}
}
}
vector<llong> brute() {
int ptr = 0;
vector<llong> ans;
vector<vector<llong>> matrix(n, vector<llong>(m));
for (auto [r1, c1, r2, c2, id]: queries) {
while (ptr < (int)updates.size() and updates[ptr].id < id) {
matrix[updates[ptr].r][updates[ptr].c] = updates[ptr].val;
++ptr;
}
llong cur_ans = 0;
for (int r = r1; r <= r2; ++r)
for (int c = c1; c <= c2; ++c) cur_ans += matrix[r][c];
ans.push_back(cur_ans);
}
return ans;
}
Ly8gSW5wdXQgZm9ybWF0OgovLyBGaXJzdCBsaW5lIGNvbnRhaW5zIG4sIG0gYW5kIHEsIHdoZXJlIG4gYW5kIG0gLSB0aGUgc2l6ZSBvZiB0aGUgbWF0cml4ICwgcSBpcyB0bmUgbnVtYmVyIG9mIHF1ZXJ5LgovLyBuLCBtLCBxIGFyZSBwb3NpdGl2ZSBpbnRlZ2VyIGFuZCBtdXN0IG5vdCBleGNlZWQgMTBeNS4KLy8gVGhlIG1hdHJpeCBpcyBpbml0aWFsbHkgMC4KLy8gcSBmb2xsb3dpbmcgbGluZXMgY29udGFpbnMgcXVlcmllcyBvZiAyIHR5cGVzOgovLyAxIHIgYyB2YWwgLSBjaGFuZ2UgdGhlIHZhbHVlIG9mIHRoZSBjZWxsIChyLCBjKSB0byB2YWwuCi8vIDIgcjEgYzEgcjIgYzIgLSBvdXRwdXQgdGhlIHN1bSBvZiB0aGUgY2VsbCBpbnNpZGUgdGhlIHN1Ym1hdHJpeCAocjEsIGMxLC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mdlZChzeiArIDEwKSB7fQogICAgICAgIHZvaWQgcm9sbF9iYWNrKCkgewogICAgICAgICAgICBmb3IgKGF1dG8gYzogY2hhbmdlZF9lbG0pIHsKICAgICAgICAgICAgICAgIGlzX2NoYW5nZWRbY10gPSAwOwogICAgICAgICAgICAgICAgc3VtW2NdID0gMDsKICAgICAgICAgICAgfQogICAgICAgICAgICBjaGFuZ2VkX2VsbS5jbGVhcigpOwogICAgICAgIH0KICAgICAgICB2b2lkIHVwZChpbnQgcCwgaW50IHZhbCkgewogICAgICAgICAgICBmb3IgKCsrcDsgcCA8IChpbnQpc3VtLnNpemUoKTsgcCArPSBwICYgLXApIHsKICAgICAgICAgICAgICAgIHN1bVtwXSArPSB2YWw7CiAgICAgICAgICAgICAgICBpZiAoIWlzX2NoYW5nZWRbcF0pIHsKICAgICAgICAgICAgICAgICAgICBpc19jaGFuZ2VkW3BdID0gdHJ1ZTsKICAgICAgICAgICAgICAgICAgICBjaGFuZ2VkX2VsbS5wdXNoX2JhY2socCk7CiAgICAgICAgICAgICAgICB9CiAgICAgICAgICAgIH0KICAgICAgICB9CiAgICAgICAgbGxvbmcgZ2V0KGludCBwKSB7CiAgICAgICAgICAgIGxsb25nIGFucyA9IDA7CiAgICAgICAgICAgIGZvciAoKytwOyBwID4gMDsgcCAtPSBwICYgLXApIGFucyArPSBzdW1bcF07CiAgICAgICAgICAgIHJldHVybiBhbnM7CiAgICAgICAgfQogICAgICAgIAogICAgICAgIGxsb25nIGdldChpbnQgbCwgaW50IHIpIHsKICAgICAgICAgICAgcmV0dXJuIGdldChyKSAtIGdldChsIC0gMSk7CiAgICAgICAgfQogICAgfTsKICAgIAogICAgdmVjdG9yPHZlY3RvcjxVcGQ+PiB1cGRfaXQoMiAqIG4pOwogICAgdmVjdG9yPHZlY3RvcjxRcj4+IHFyX2l0KDIgKiBuKTsKICAgIAogICAgZm9yIChhdXRvIHU6IHVwZGF0ZXMpIHsKICAgICAgICBmb3IgKGludCBwID0gbiArIHUucjsgcCA+IDA7IHAgPj49IDEpCiAgICAgICAgICAgIHVwZF9pdFtwXS5wdXNoX2JhY2sodSk7CiAgICB9CiAgICBmb3IgKGF1dG8gcXI6IHF1ZXJpZXMpIHsKICAgICAgICBmb3IgKGludCBsID0gcXIucjEgKyBuLCByID0gcXIucjIgKyBuICsgMTsgbCA8IHI7IGwgPj49IDEsIHIgPj49IDEpIHsKICAgICAgICAgICAgaWYgKGwgJiAxKSBxcl9pdFtsKytdLnB1c2hfYmFjayhxcik7CiAgICAgICAgICAgIGlmIChyICYgMSkgcXJfaXRbLS1yXS5wdXNoX2JhY2socXIpOwogICAgICAgIH0KICAgIH0KICAgIAogICAgQklUIGJpdChtKTsKICAgIHZlY3RvcjxsbG9uZz4gYW5zKHEpOwogICAgZm9yIChpbnQgcm9vdCA9IDE7IHJvb3QgPCAyICogbjsgKytyb290KSB7CiAgICAgICAgYXV0byYgdWkgPSB1cGRfaXRbcm9vdF07CiAgICAgICAgYXV0byYgcWkgPSBxcl9pdFtyb290XTsKICAgICAgICBpZiAodWkuZW1wdHkoKSBvciBxaS5lbXB0eSgpKSBjb250aW51ZTsKICAgICAgICAKICAgICAgICBpbnQgcHRyID0gMDsKICAgICAgICBmb3IgKGF1dG8gcXI6IHFpKSB7CiAgICAgICAgICAgIHdoaWxlIChwdHIgPCAoaW50KXVpLnNpemUoKSBhbmQgdWlbcHRyXS5pZCA8IHFyLmlkKSB7CiAgICAgICAgICAgICAgICBiaXQudXBkKHVpW3B0cl0uYywgdWlbcHRyXS52YWwpOwogICAgICAgICAgICAgICAgKytwdHI7CiAgICAgICAgICAgIH0KICAgICAgICAgICAgYW5zW3FyLmlkXSArPSBiaXQuZ2V0KHFyLmMxLCBxci5jMik7CiAgICAgICAgfQogICAgICAgIGJpdC5yb2xsX2JhY2soKTsKICAgIH0KICAgIGludCBuZXdfc2l6ZSA9IDA7CiAgICBmb3IgKGF1dG8gcXI6IHF1ZXJpZXMpIHsKICAgICAgICBhbnNbbmV3X3NpemUrK10gPSBhbnNbcXIuaWRdOwogICAgfQogICAgYW5zLnJlc2l6ZShuZXdfc2l6ZSk7CiAgICByZXR1cm4gYW5zOwp9CgptdDE5OTM3IHJuZzsKI2RlZmluZSByYW5kKCkgKChpbnQpKHJuZygpID4+IDEpKQp2b2lkIGdlbl90ZXN0KCk7CnZvaWQgY2hlY2tfc21hbGxfdGVzdCgpOwp2ZWN0b3I8bGxvbmc+IGJydXRlKCk7CgppbnQgbWFpbigpIHsKI2lmZGVmIHRlc3RpbmcKICAgIC8vIHJuZy5zZWVkKChsbG9uZykobWFpbikgXiAobGxvbmcpdGltZSgwKSk7IAogICAgd2hpbGUgKHRydWUpIHsKICAgICAgICBnZW5fdGVzdCgpOwogICAgICAgIGF1dG8gZXhwID0gYnJ1dGUoKTsKICAgICAgICBhdXRvIGFucyA9IHNvbHZlKCk7CiAgICAgICAgaWYgKGV4cCA9PSBhbnMpIHsKICAgICAgICAgICAgY291dCA8PCAiTmljZSIgPDwgZW5kbDsKICAgICAgICAgICAgY29udGludWU7CiAgICAgICAgfQogICAgICAgIGNvdXQgPDwgImZhaWxlZCAiIDw8IGVuZGw7CiAgICAgICAgZm9yIChhdXRvIGk6IGFucykgY291dCA8PCBpIDw8ICdcbic7CiAgICAgICAgY291dCA8PCAiLS0tLS0tLS0tLS0tLS0tLS0tLS0tXG4iOwogICAgICAgIGZvciAoYXV0byBpOiBleHApIGNvdXQgPDwgaSA8PCAnXG4nOwogICAgICAgIHJldHVybiAxOwogICAgfQojZW5kaWYKI2lmZGVmIExPQ0FMCiAgICBmcmVvcGVuKCJtYWluLmlucCIsICJyIiwgc3RkaW4pOwogICAgZnJlb3BlbigibWFpbi5vdXQiLCAidyIsIHN0ZG91dCk7CiNlbmRpZgogICAgcmVhZCgpOwogICAgLy8gYXV0byBhbnMgPSBicnV0ZSgpOyAgIAogICAgYXV0byBhbnMgPSBzb2x2ZSgpOyAgCiAgICBmb3IgKGF1dG8gaTogYW5zKSBjb3V0IDw8IGkgPDwgJ1xuJzsKICAgIHJldHVybiAwOwp9CgovLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLy8vLwp2b2lkIGdlbl90ZXN0KCkgewogICAgb2ZzdHJlYW0gaW5wKCJtYWluLmlucCIpOwogICAgbiA9IHJhbmQoKSAlIDEwICsgMTsKICAgIG0gPSByYW5kKCkgJSAxMCArIDE7CiAgICBxID0gcmFuZCgpICUgMTAgKyAxOwogICAgaW5wIDw8IG4gPDwgJyAnIDw8IG0gPDwgJyAnIDw8IHEgPDwgJ1xuJzsKICAgIAogICAgdXBkYXRlcy5jbGVhcigpOwogICAgcXVlcmllcy5jbGVhcigpOwogICAgZm9yIChpbnQgaSA9IDA7IGkgPCBxOyArK2kpIHsKICAgICAgICBpbnQgdHlwZSA9IHJhbmQoKSAmIDE7CiAgICAgICAgaWYgKHR5cGUgPT0gMSkgewogICAgICAgICAgICBpbnQgciwgYywgdjsKICAgICAgICAgICAgciA9IHJhbmQoKSAlIG4gKyAxOwogICAgICAgICAgICBjID0gcmFuZCgpICUgbSArIDE7CiAgICAgICAgICAgIHYgPSByYW5kKCkgJSAxMDA7CiAgICAgICAgICAgIHVwZGF0ZXMuZW1wbGFjZV9iYWNrKHIsIGMsIHYsIGkpOwogICAgICAgICAgICBpbnAgPDwgMSA8PCAnICcgPDwgciA8PCAnICcgPDwgYyA8PCAnICcgPDwgdiA8PCAnXG4nOwogICAgICAgIH0gZWxzZSB7CiAgICAgICAgICAgIGludCByMSwgYzEsIHIyLCBjMjsKICAgICAgICAgICAgcjEgPSByYW5kKCkgJSBuICsgMTsKICAgICAgICAgICAgcjIgPSByYW5kKCkgJSBuICsgMTsKICAgICAgICAgICAgYzEgPSByYW5kKCkgJSBtICsgMTsKICAgICAgICAgICAgYzIgPSByYW5kKCkgJSBtICsgMTsKICAgICAgICAgICAgcXVlcmllcy5l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