Language: C++

#include <cstdio>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
#include <string>
#include <iostream>
#include <cmath>
#include <string>
#include <map>
#include <unordered_map>
#include <queue>

using namespace std;

typedef long long LL;

const int dx[] = {0, 1, 1, 0};
const int dy[] = {0, 1, 0, 1};

int data[100005][100005];
int val[100005][100005];
int r, c;
int n;

queue<pair<int, int> > Q;

void check(int x, int y) {
  val[x][y] -= 1;
  if (x > 0) {
    val[x - 1][y] -= 1;
    if (val[x - 1][y] == 1) {
      Q.push(make_pair(x - 1, y));
    }
  }
  if (y > 0) {
    val[x][y - 1] -= 1;
    if (val[x][y - 1] == 1) {
      Q.push(make_pair(x, y - 1));
    }
  }
  if (x > 0 && y > 0) {
    val[x - 1][y - 1] -= 1;
    if (val[x - 1][y - 1] == 1) {
      Q.push(make_pair(x - 1, y - 1));
    }
  }
}

int main() {
  scanf("%d %d", &r, &c);
  scanf("%d", &n);
  for (int i = 0; i < r; i += 1) {
    for (int j = 0; j < c; j += 1) {
      data[i][j] = -1;
      val[i][j] = 4;
    }
  }

  for (int i = 0; i < n; i += 1) {
    int x, y, a;
    scanf("%d %d %d", &x, &y, &a);
    x -= 1; y -= 1;
    data[x][y] = a;
    check(x, y);
  }
  while (!Q.empty()) {
    int x = Q.front().first, y = Q.front().second;
    Q.pop();
    int pos = -1, flag = 0;
    for (int i = 0; i < 4; i += 1) {
      if (data[x + dx[i]][y + dy[i]] == -1) {
        pos = i;
        flag += 1;
      }
    }
    if (flag != 1) {
      if (data[x][y] + data[x + 1][y + 1] != data[x][y + 1] + data[x + 1][y]) {
        printf("NO\n");
        return 0;
      }
    }
    if (pos == 0) {
      data[x][y] = data[x + 1][y] + data[x][y + 1] - data[x + 1][y + 1];
      if (data[x][y] < 0) {
        printf("NO\n");
        return 0;
      }
      check(x, y);
    } else if (pos == 1) {
      data[x + 1][y + 1] = data[x + 1][y] + data[x][y + 1] - data[x][y];
      if (data[x + 1][y + 1] < 0) {
        printf("NO\n");
        return 0;
      }
      check(x + 1, y + 1);
    } else if (pos == 2) {
      data[x + 1][y] = data[x][y] + data[x + 1][y + 1] - data[x][y + 1];
      if (data[x + 1][y] < 0) {
        printf("NO\n");
        return 0;
      }
      check(x + 1, y);
    } else {
      data[x][y + 1] = data[x][y] + data[x + 1][y + 1] - data[x + 1][y];
      if (data[x][y + 1] < 0) {
        printf("NO\n");
        return 0;
      }
      check(x, y + 1);
    }
  }
  for (int i = 0; i < r; i += 1) {
    for (int j = 0; j < c; j += 1) {
      if (val[i][j] == 0) {
        if (data[i][j] + data[i + 1][j + 1] != data[i][j + 1] + data[i + 1][j]) {
          printf("NO\n");
          return 0;
        }
      }
    }
  }
  printf("YES\n");
  return 0;
}


Compiler output:
a.cc: In function 'int main()':
a.cc:55:7: error: reference to 'data' is ambiguous
   55 |       data[i][j] = -1;
      |       ^~~~
In file included from /usr/include/c++/14/vector:69,
                 from a.cc:5:
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:21:5: note:                 'int data [100005][100005]'
   21 | int data[100005][100005];
      |     ^~~~
a.cc:64:5: error: reference to 'data' is ambiguous
   64 |     data[x][y] = a;
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:21:5: note:                 'int data [100005][100005]'
   21 | int data[100005][100005];
      |     ^~~~
a.cc:72:11: error: reference to 'data' is ambiguous
   72 |       if (data[x + dx[i]][y + dy[i]] == -1) {
      |           ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:21:5: note:                 'int data [100005][100005]'
   21 | int data[100005][100005];
      |     ^~~~
a.cc:78:11: error: reference to 'data' is ambiguous
   78 |       if (data[x][y] + data[x + 1][y + 1] != data[x][y + 1] + data[x + 1][y]) {
      |           ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:21:5: note:                 'int data [100005][100005]'
   21 | int data[100005][100005];
      |     ^~~~
a.cc:78:24: error: reference to 'data' is ambiguous
   78 |       if (data[x][y] + data[x + 1][y + 1] != data[x][y + 1] + data[x + 1][y]) {
      |                        ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:21:5: note:                 'int data [100005][100005]'
   21 | int data[100005][100005];
      |     ^~~~
a.cc:78:46: error: reference to 'data' is ambiguous
   78 |       if (data[x][y] + data[x + 1][y + 1] != data[x][y + 1] + data[x + 1][y]) {
      |                                              ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:21:5: note:                 'int data [100005][100005]'
   21 | int data[100005][100005];
      |     ^~~~
a.cc:78:63: error: reference to 'data' is ambiguous
   78 |       if (data[x][y] + data[x + 1][y + 1] != data[x][y + 1] + data[x + 1][y]) {
      |                                                               ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:21:5: note:                 'int data [100005][100005]'
   21 | int data[100005][100005];
      |     ^~~~
a.cc:84:7: error: reference to 'data' is ambiguous
   84 |       data[x][y] = data[x + 1][y] + data[x][y + 1] - data[x + 1][y + 1];
      |       ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  334 |     data(_Tp (&__array)[_Nm]) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:323:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(const _Container&)'
  323 |     data(const _Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:312:5: note:                 'template<class _Container> constexpr decltype (__cont.data()) std::data(_Container&)'
  312 |     data(_Container& __cont) noexcept(noexcept(__cont.data()))
      |     ^~~~
a.cc:21:5: note:                 'int data [100005][100005]'
   21 | int data[100005][100005];
      |     ^~~~
a.cc:84:20: error: reference to 'data' is ambiguous
   84 |       data[x][y] = data[x + 1][y] + data[x][y + 1] - data[x + 1][y + 1];
      |                    ^~~~
/usr/include/c++/14/bits/range_access.h:344:5: note: candidates are: 'template<class _Tp> constexpr const _Tp* std::data(initializer_list<_Tp>)'
  344 |     data(initializer_list<_Tp> __il) noexcept
      |     ^~~~
/usr/include/c++/14/bits/range_access.h:334:5: note:                 'template<class _Tp, long unsigned int _Nm> constexpr _Tp* std::data(_Tp (&)[_Nm])'
  3