Language: C++

#include<iostream>
#include<iomanip>
#include<algorithm>
#include<array>
#include<bitset>
#include<cassert>
#include<cctype>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<functional>
#include<limits>
#include<list>
#include<map>
#include<numeric>
#include<set>
#include<stack>
#include<string>
#include<sstream>
#include<unordered_map>
#include<queue>
#include<vector>
using namespace std;

using ll = long long;
using ull = unsigned long long;
using pii = pair<int, int>;

#define rep(i,A,b) for(int i=(A);i<(b);i++)
#define rrep(i,A,b) for(int i=(b)-1;i>=(A);i--)
#define all(A) (A).begin(),(A).end()
#define dump(o) {cerr<<#o<<" "<<o<<endl;}
#define dumpc(o) {cerr<<#o; for(auto &e:(o))cerr<<" "<<e;cerr<<endl;}
#define INF 0x3f3f3f3f
#define INFL 0x3f3f3f3f3f3f3f3fLL
const int MOD = 1e9 + 7;

#define MAX 100010

signed main() {
	int R; cin >> R;
	int C; cin >> C;
	int N; cin >> N;
	static int a[3][MAX] = {};
	fill(a[0], a[3], -1);
	int prevr = 0;
	bool flag = true;
	queue<int> Q;
	rep(i, 0, N) {
		int r, c, A; cin >> r >> c >> A;
		a[r % 3][c] = A;
		if (r >= 2) {
			Q.push(c);
			Q.push(c + 1);
		}
		if (prevr < r&&r >= 3) {
			while (!Q.empty()) {
				int j = Q.front(); Q.pop();
				int cnt = 0;
				int x = a[(r - 2) % 3][j - 1],
					y = a[(r - 2) % 3][j],
					z = a[(r - 1) % 3][j - 1],
					w = a[(r - 1) % 3][j];

				if (x == -1)cnt++;
				if (y == -1)cnt++;
				if (z == -1)cnt++;
				if (w == -1)cnt++;
				if (cnt >= 3)continue;
				if (cnt == 2) {
					if (z != -1 && r >= 3) {
						if (a[(r - 3) % 3][j] != -1 || a[(r - 3) % 3][j - 1] != -1)continue;
						if (w - z != a[(r - 3) % 3][j] - a[(r - 3) % 3][j - 1]) { flag = false; i = N; break; }
					}
					if (y != -1 && j >= 3) {
						if (a[(r - 2) % 3][j - 2] != -1 || a[(r - 1) % 3][j - 2] != -1)continue;
						if (w - z != a[(r - 2) % 3][j - 2] - a[(r - 1) % 3][j - 2]) { flag = false; i = N; break; }
					}
					continue;
				}
				if (cnt == 1) {
					bool flag2 = false;
					if (x == -1 && z + y - w >= 0) { flag2 = true; a[(r - 2) % 3][j - 1] = z + y - w; }
					if (y == -1 && x + w - z >= 0) { flag2 = true; a[(r - 2) % 3][j] = x + w - z; }
					if (z == -1 && x + w - y >= 0) { flag2 = true; a[(r - 1) % 3][j - 1] = x + w - y; }
					if (w == -1 && z + y - x >= 0) { flag2 = true; a[(r - 1) % 3][j] = z + y - x; Q.push(j + 1); }
					if (!flag2) { flag = false; i = N; break; }
				}
				if (cnt == 0) {
					if (x + w != y + z) {
						flag = false;
						i = N;
						break;
					}
				}
			}
		}
	}
	if (flag) {
		while (!Q.empty()) {
			int j = Q.front(); Q.pop();
			int cnt = 0;
			int x = a[(R + 1 - 2) % 3][j - 1],
				y = a[(R + 1 - 2) % 3][j],
				z = a[(R + 1 - 1) % 3][j - 1],
				w = a[(R + 1 - 1) % 3][j];
			if (x == -1)cnt++;
			if (y == -1)cnt++;
			if (z == -1)cnt++;
			if (w == -1)cnt++;
			if (cnt >= 3)continue;
			if (cnt == 2) {
				if (z != -1 && r >= 3) {
					if (a[(r - 3) % 3][j] != -1 || a[(r - 3) % 3][j - 1] != -1)continue;
					if (w - z != a[(r - 3) % 3][j] - a[(r - 3) % 3][j - 1]) { flag = false; i = N; break; }
				}
				if (y != -1 && j >= 3) {
					if (a[(r - 2) % 3][j - 2] != -1 || a[(r - 1) % 3][j - 2] != -1)continue;
					if (w - z != a[(r - 2) % 3][j - 2] - a[(r - 1) % 3][j - 2]) { flag = false; i = N; break; }
				}
				continue;
			}
			if (cnt == 1) {
				bool flag2 = false;
				if (x == -1 && z + y - w >= 0) { flag2 = true; a[(R + 1 - 2) % 3][j - 1] = z + y - w; }
				if (y == -1 && x + w - z >= 0) { flag2 = true; a[(R + 1 - 2) % 3][j] = x + w - z; }
				if (z == -1 && x + w - y >= 0) { flag2 = true; a[(R + 1 - 1) % 3][j - 1] = x + w - y; }
				if (w == -1 && z + y - x >= 0) { flag2 = true; a[(R + 1 - 1) % 3][j] = z + y - x; Q.push(j + 1); }
				if (!flag2) {
					flag = false;
					break;
				}
			}
			if (cnt == 0) {
				if (x + w != y + z) {
					flag = false;
					break;
				}
			}
		}
	}
	if (flag)cout << "Yes" << endl;
	else cout << "No" << endl;
	return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:113:48: error: 'r' was not declared in this scope
  113 |                                 if (z != -1 && r >= 3) {
      |                                                ^
a.cc:115:113: error: 'i' was not declared in this scope
  115 |                                         if (w - z != a[(r - 3) % 3][j] - a[(r - 3) % 3][j - 1]) { flag = false; i = N; break; }
      |                                                                                                                 ^
a.cc:118:48: error: 'r' was not declared in this scope
  118 |                                         if (a[(r - 2) % 3][j - 2] != -1 || a[(r - 1) % 3][j - 2] != -1)continue;
      |                                                ^
a.cc:119:57: error: 'r' was not declared in this scope
  119 |                                         if (w - z != a[(r - 2) % 3][j - 2] - a[(r - 1) % 3][j - 2]) { flag = false; i = N; break; }
      |                                                         ^
a.cc:119:117: error: 'i' was not declared in this scope
  119 |                                         if (w - z != a[(r - 2) % 3][j - 2] - a[(r - 1) % 3][j - 2]) { flag = false; i = N; break; }
      |                                                                                                                     ^