#include #include #include #include using namespace std; class segmenttree { private: long long n, sz; unordered_map val; public: segmenttree() : n(0), sz(0) {}; segmenttree(int n_) : n(n_) { sz = 1; while (sz < n) sz *= 2; } void update(long long pos, int x) { pos += sz; val[pos] = x; while (pos > 1) { pos >>= 1; val[pos] = max(val[pos * 2], val[pos * 2 + 1]); } } long long reachl(int x) { if (val[1] < x) return n + 2; long long cur = 1; while (cur < sz) { cur *= 2; if (val[cur] < x) ++cur; } return cur - sz; } long long reachr(int x) { if (val[1] < x) return -1 - 2; long long cur = 1; while (cur < sz) { cur *= 2; if (val[cur + 1] >= x) ++cur; } return cur - sz; } }; int main() { cin.tie(0); ios_base::sync_with_stdio(false); int H, W, Q; cin >> H >> W >> Q; vector X(Q), Y(Q), L(Q); for (int i = 0; i < Q; ++i) { cin >> Y[i] >> X[i] >> L[i]; --X[i], --Y[i]; } vector sz(4); vector > dat(4); vector seg(4); for (int i = 0; i < 4; ++i) { sz[i] = (i % 2 == 0 ? H : W); seg[i] = segmenttree(sz[i]); } for (int i = 0; i < Q; ++i) { int dir = -1, pos = -1; if (Y[i] == -1) { dir = 0, pos = X[i]; } if (X[i] == H) { dir = 1, pos = Y[i]; } if (Y[i] == W) { dir = 2, pos = H - X[i] - 1; } if (X[i] == -1) { dir = 3, pos = W - Y[i] - 1; } int val1 = seg[(dir + 1) % 4].reachl(sz[dir] - pos) + 1; int val2 = sz[(dir + 1) % 4] - dat[(dir + 2) % 4][sz[dir] - pos - 1] + 1; int val3 = sz[(dir + 1) % 4] - seg[(dir + 3) % 4].reachr(pos + 1); int mn = min({ val1, val2, val3 }); int plen = dat[dir][pos] + L[i]; if (plen < mn) { dat[dir][pos] = plen; seg[dir].update(pos, plen); } else { dat[dir][pos] = 0; seg[dir].update(pos, 0); if (mn == val2) { dat[(dir + 2) % 4][sz[dir] - pos - 1] = 0; seg[(dir + 2) % 4].update(sz[dir] - pos - 1, 0); } else if (mn == val1) { dat[(dir + 1) % 4][val1 - 1] = 0; seg[(dir + 1) % 4].update(val1 - 1, 0); } else { dat[(dir + 3) % 4][sz[(dir + 1) % 4] - val3] = 0; seg[(dir + 3) % 4].update(sz[(dir + 1) % 4] - val3, 0); } } } long long ans = 0; for (int i = 0; i < 4; ++i) { for (pair j : dat[i]) { ans += j.second; } } cout << ans << endl; return 0; }