#include <bits/stdc++.h>
using namespace std;
#define ll long long int
#define endl "\n"
struct event
{
ll x, y1, y2, type;
bool operator<(const event &other) const
{
return x < other.x || (x == other.x && type < other.type);
}
};
struct dynamicSegmentTree
{
#define mid (left + ((right - left) >> 1))
private:
struct Node
{
ll Value;
ll count; // Number of positive values
Node *LeftChild, *RightChild;
Node()
{
Value = 0;
count = 0;
LeftChild = nullptr;
RightChild = nullptr;
}
Node(const ll &val)
{
Value = val;
count = (val > 0) ? 1 : 0;
LeftChild = nullptr;
RightChild = nullptr;
}
void createChildren(const ll val = 0)
{
if (LeftChild == nullptr)
LeftChild = new Node(val);
if (RightChild == nullptr)
RightChild = new Node(val);
}
~Node()
{
delete LeftChild;
delete RightChild;
LeftChild = nullptr;
RightChild = nullptr;
}
};
ll N;
ll merge(const ll &leftNode, const ll &rightNode)
{
return leftNode + rightNode;
}
ll mergeBinary(const Node *node)
{
ll leftCount = node->LeftChild ? node->LeftChild->count : 0;
ll rightCount = node->RightChild ? node->RightChild->count : 0;
return leftCount + rightCount;
}
void push(ll left, ll right, Node *segNode, Node *lazyNode)
{
if (segNode == nullptr || lazyNode == nullptr || lazyNode->Value == 0)
return;
segNode->Value += (right - left + 1) * lazyNode->Value;
segNode->count = (segNode->Value > 0) ? (right - left + 1) : 0;
if (left != right)
{
segNode->createChildren();
lazyNode->createChildren();
lazyNode->LeftChild->Value += lazyNode->Value;
lazyNode->RightChild->Value += lazyNode->Value;
}
lazyNode->Value = 0;
}
void update(ll left, ll right, Node *segNode, Node *lazyNode, ll leftQuery, ll rightQuery, const ll &val)
{
push(left, right, segNode, lazyNode);
if (left > rightQuery || right < leftQuery)
return;
if (left >= leftQuery && right <= rightQuery)
{
lazyNode->Value += val;
push(left, right, segNode, lazyNode);
return;
}
segNode->createChildren();
lazyNode->createChildren();
update(left, mid, segNode->LeftChild, lazyNode->LeftChild, leftQuery, rightQuery, val);
update(mid + 1, right, segNode->RightChild, lazyNode->RightChild, leftQuery, rightQuery, val);
segNode->Value = merge(segNode->LeftChild->Value, segNode->RightChild->Value);
segNode->count = mergeBinary(segNode);
}
ll query(ll left, ll right, Node *segNode, Node *lazyNode, ll leftQuery, ll rightQuery)
{
if (left > rightQuery || right < leftQuery)
return 0;
push(left, right, segNode, lazyNode);
if (leftQuery <= left && right <= rightQuery)
return segNode->count;
segNode->createChildren();
lazyNode->createChildren();
ll leftSegment = query(left, mid, segNode->LeftChild, lazyNode->LeftChild, leftQuery, rightQuery);
ll rightSegment = query(mid + 1, right, segNode->RightChild, lazyNode->RightChild, leftQuery, rightQuery);
return leftSegment + rightSegment;
}
public:
Node *segRoot;
Node *lazyRoot;
dynamicSegmentTree()
{
segRoot = new Node();
lazyRoot = new Node();
N = (1LL << 50);
}
// Frees all dynamically allocated nodes
void clear()
{
delete segRoot;
delete lazyRoot;
segRoot = new Node();
lazyRoot = new Node();
}
void update(ll left, ll right, const ll &val)
{
update(1, N, segRoot, lazyRoot, left, right, val);
}
ll query(ll M)
{
return query(1, N, segRoot, lazyRoot, 1, M);
}
#undef mid
};
ll calculateUnionArea(vector<event> &events, int M)
{
sort(events.begin(), events.end());
dynamicSegmentTree segTree;
ll prevX = 0, totalArea = 0, prevCoveredLength = 0;
for (const auto &e : events)
{
ll curX = e.x;
if (prevCoveredLength > 0)
totalArea += prevCoveredLength * (curX - prevX); // Add covered area
segTree.update(e.y1, e.y2, e.type); // Update segment tree
prevX = curX;
prevCoveredLength = segTree.query(M);
}
segTree.clear();
return totalArea;
}
int main()
{
ios_base::sync_with_stdio(false);
cin.tie(nullptr);
#ifndef ONLINE_JUDGE
freopen("Output.txt", "w", stdout
); #endif
int t = 1;
cin >> t;
while (t--)
{
ll N, M, Q;
cin >> N >> M >> Q;
vector<event> events;
while (Q--)
{
ll X, Y, S;
cin >> X >> Y >> S;
ll X1 = max(X - S, 1LL);
ll Y1 = max(Y - S, 1LL);
ll X2 = min(X + S, N);
ll Y2 = min(Y + S, M);
events.push_back({X1, Y1, Y2, 1});
events.push_back({X2 + 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