Language: C++

#include <bits/stdc++.h>
using namespace std;
class disjoint_set {
  public:
  vector<long long> rank, prt, ptl;
  disjoint_set (long long size) {
    rank.resize(size);
    prt.resize(size);
    ptl.resize(size);
    for (long long i = 0; i < size; i++) {
      prt[i] = i;
      rank[i] = 0;
      ptl[i] = 0;
    }
  }
  long long find (long long x) {
    if (x == prt[x]){
      return x;
    } else {
      int r = find(prt[x]);
      ptl[x] += ptl[prt[x]];
      return prt[x] = r;
    }
  }
  bool is_equiv (int x, int y) {
    return find(x) == find(y);
  }
  bool unite (int x, int y, long long w) {
    if (find(x) == find(y)) {
      return false;
    }
    w += ptl[x];
    w -= ptl[y];
    x = find(x);
    y = find(y);
    if (rank[x] < rank[y]) {
      swap(x, y);
      w *= -1;
    }
    prt[y] = x;
    ptl[y] = w;
    if (rank[x] == rank[y]) {
      rank[x]++;
    }
    return true;
  }
  long long diff (int x, int y) {
    find(x);
    find(y);
    return ptl[y] - ptl[x];
  }
};
void err() {
  cout << "No" << '\n';
  exit(0);
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int h, w, n;
  cin >> h >> w >> n;
  vector<pair<pair<int, int>, int>> cnd(n);
  int d = 0;
  for (int t = 0; t < n; t++) {
    int i, j, a;
    cin >> i >> j >> a;
    cnd[t] = {{--i, --j}, a};
    if (i == 0 && j == 0) {
      d = a;
    }
  }
  for (auto& p : cnd) {
    int i = p.first.first;
    int j = p.first.second;
    long long& a = p.second;
    if (i > 0 && j > 0) {
      a += d;
    } else if (i == 0 && j == 0) {
      a = 0;
    }
  }
  disjoint_set djs(h + w - 1);
  int mina = 2e9;
  int maxb = -2e9;
  for (auto p : cnd) {
    int i = p.first.first;
    int j = p.first.second;
    int s = h - 1 + j;
    int t = h - 1 - i;
    long long w = p.second;
    if (djs.is_equiv(s, t)) {
      if (djs.diff(s, t) != w) {
        err();
      }
    } else {
      djs.unite(s, t, w);
      for (int r = 0; r < 2; r++) {
        if (djs.is_equiv(h - 1, s)) {
          if (s < h - 1) {
            mina = min(mina, djs.diff(h - 1, s));
          } else if (h - 1 < s) {
            maxb = max(maxb, djs.diff(s, h - 1));
          }
        }
        swap(s, t);
      }
    }
  }
  if (maxb > 0 && mina - maxb < d) {
    err();
  }
  cout << "Yes" << '\n';
  return 0;
}

Compiler output:
a.cc: In function 'int main()':
a.cc:75:22: error: cannot bind non-const lvalue reference of type 'long long int&' to a value of type 'int'
   75 |     long long& a = p.second;
      |                    ~~^~~~~~
a.cc:100:23: error: no matching function for call to 'min(int&, long long int)'
  100 |             mina = min(mina, djs.diff(h - 1, s));
      |                    ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
In file included from /usr/include/c++/14/algorithm:60,
                 from /usr/include/x86_64-linux-gnu/c++/14/bits/stdc++.h:51,
                 from a.cc:1:
/usr/include/c++/14/bits/stl_algobase.h:233:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::min(const _Tp&, const _Tp&)'
  233 |     min(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:233:5: note:   template argument deduction/substitution failed:
a.cc:100:23: note:   deduced conflicting types for parameter 'const _Tp' ('int' and 'long long int')
  100 |             mina = min(mina, djs.diff(h - 1, s));
      |                    ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::min(const _Tp&, const _Tp&, _Compare)'
  281 |     min(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:281:5: note:   candidate expects 3 arguments, 2 provided
In file included from /usr/include/c++/14/algorithm:61:
/usr/include/c++/14/bits/stl_algo.h:5686:5: note: candidate: 'template<class _Tp> constexpr _Tp std::min(initializer_list<_Tp>)'
 5686 |     min(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5686:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5696:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::min(initializer_list<_Tp>, _Compare)'
 5696 |     min(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5696:5: note:   template argument deduction/substitution failed:
a.cc:100:23: note:   mismatched types 'std::initializer_list<_Tp>' and 'int'
  100 |             mina = min(mina, djs.diff(h - 1, s));
      |                    ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:102:23: error: no matching function for call to 'max(int&, long long int)'
  102 |             maxb = max(maxb, djs.diff(s, h - 1));
      |                    ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note: candidate: 'template<class _Tp> constexpr const _Tp& std::max(const _Tp&, const _Tp&)'
  257 |     max(const _Tp& __a, const _Tp& __b)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:257:5: note:   template argument deduction/substitution failed:
a.cc:102:23: note:   deduced conflicting types for parameter 'const _Tp' ('int' and 'long long int')
  102 |             maxb = max(maxb, djs.diff(s, h - 1));
      |                    ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note: candidate: 'template<class _Tp, class _Compare> constexpr const _Tp& std::max(const _Tp&, const _Tp&, _Compare)'
  303 |     max(const _Tp& __a, const _Tp& __b, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algobase.h:303:5: note:   candidate expects 3 arguments, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5706:5: note: candidate: 'template<class _Tp> constexpr _Tp std::max(initializer_list<_Tp>)'
 5706 |     max(initializer_list<_Tp> __l)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5706:5: note:   candidate expects 1 argument, 2 provided
/usr/include/c++/14/bits/stl_algo.h:5716:5: note: candidate: 'template<class _Tp, class _Compare> constexpr _Tp std::max(initializer_list<_Tp>, _Compare)'
 5716 |     max(initializer_list<_Tp> __l, _Compare __comp)
      |     ^~~
/usr/include/c++/14/bits/stl_algo.h:5716:5: note:   template argument deduction/substitution failed:
a.cc:102:23: note:   mismatched types 'std::initializer_list<_Tp>' and 'int'
  102 |             maxb = max(maxb, djs.diff(s, h - 1));
      |                    ~~~^~~~~~~~~~~~~~~~~~~~~~~~~~