Problem:
Score : 800 points

Problem Statement

There is a grid with R rows and C columns.
We call the cell in the r-th row and c-th column (r，c).

Mr. Takahashi wrote non-negative integers into N of the cells, that is, he wrote a non-negative integer a_i into (r_i，c_i) for each i (1≤i≤N).
After that he fell asleep.

Mr. Aoki found the grid and tries to surprise Mr. Takahashi by writing integers into all remaining cells.
The grid must meet the following conditions to really surprise Mr. Takahashi.

Condition 1: Each cell contains a non-negative integer.

Condition 2: For any 2×2 square formed by cells on the grid, the sum of the top left and bottom right integers must always equal to the sum of the top right and bottom left integers.

Determine whether it is possible to meet those conditions by properly writing integers into all remaining cells.

Constraints

2≤R，C≤10^5

1≤N≤10^5

1≤r_i≤R

1≤c_i≤C

(r_i，c_i) ≠ (r_j，c_j) (i≠j)

a_i is an integer.

0≤a_i≤10^9

Input

The input is given from Standard Input in the following format:

R C
N
r_1 c_1 a_1
r_2 c_2 a_2
:
r_N c_N a_N

Output

Print Yes if it is possible to meet the conditions by properly writing integers into all remaining cells.
Otherwise, print No.

Sample Input 1

2 2
3
1 1 0
1 2 10
2 1 20

Sample Output 1

Yes

You can write integers as follows.

Sample Input 2

2 3
5
1 1 0
1 2 10
1 3 20
2 1 30
2 3 40

Sample Output 2

No

There are two 2×2 squares on the grid, formed by the following cells:

Cells (1，1), (1，2), (2，1) and (2，2)

Cells (1，2), (1，3), (2，2) and (2，3)

You have to write 40 into the empty cell to meet the condition on the left square, but then it does not satisfy the condition on the right square.

Sample Input 3

2 2
3
1 1 20
1 2 10
2 1 0

Sample Output 3

No

You have to write -10 into the empty cell to meet condition 2, but then it does not satisfy condition 1.

Sample Input 4

3 3
4
1 1 0
1 3 10
3 1 10
3 3 20

Sample Output 4

Yes

You can write integers as follows.

Sample Input 5

2 2
4
1 1 0
1 2 10
2 1 30
2 2 20

Sample Output 5

No

All cells already contain a integer and condition 2 is not satisfied.

Solution in Go (1.6) (judge verdict: Wrong Answer)

package main

import (
	"bufio"
	"container/heap"
	"fmt"
	"os"
	"strconv"
)

var sc = bufio.NewScanner(os.Stdin)

// An Item is something we manage in a priority queue.
type Item struct {
	//value    string // The value of the item; arbitrary.
	word     string
	weight   [2]int64
	priority int // The priority of the item in the queue.
	// The index is needed by update and is maintained by the heap.Interface methods.
	//index int // The index of the item in the heap.
}

// A PriorityQueue implements heap.Interface and holds Items.
type PriorityQueue []*Item

func (pq PriorityQueue) Len() int { return len(pq) }

func (pq PriorityQueue) Less(i, j int) bool {
	// We want Pop to give us the highest, not lowest, priority so we use greater than here.
	return pq[i].priority < pq[j].priority
}

func (pq PriorityQueue) Swap(i, j int) {
	pq[i], pq[j] = pq[j], pq[i]
	//pq[i].index = i
	//pq[j].index = j
}

func (pq *PriorityQueue) Push(x interface{}) {
	//n := len(*pq)
	item := x.(*Item)
	//item.index = n
	*pq = append(*pq, item)
}

func (pq *PriorityQueue) Pop() interface{} {
	old := *pq
	n := len(old)
	item := old[n-1]
	//item.index = -1 // for safety
	*pq = old[0 : n-1]
	return item
}

func nextInt() int64 {
	var i int64
	var e error
	for {
		sc.Scan()
		k := sc.Text()
		//re, _ := regexp.Compile(`\d{1,100}`)
		//t := re.FindAllStringSubmatch(k, -1)
		//k = t[0][0]
		//k = strings.TrimSpace(k)
		i, e = strconv.ParseInt(k, 10, 64)
		if e == nil {
			break
		}
	}
	return i
}
func bool2int(b bool) int {
	if b {
		return 1
	}
	return 0
}

func abs(a int64, b int64) int64 {
	if a < b {
		return b - a
	} else {
		return a - b
	}
}

func main() {
	sc.Split(bufio.ScanWords)
	r := nextInt()
	c := nextInt()
	n := nextInt()
	//dary2 := make([]bool, h)
	//fmt.Println(dary2[0])
	dary := make([][]bool, 3)
	for i := range dary {
		dary[i] = make([]bool, 3)
	}
	m := map[[2]int64]int64{}
	kk := map[[2]int64]int{}
	ans := true

	for i := int64(0); i < n; i++ {
		x := nextInt() - 1
		y := nextInt() - 1
		a := nextInt()
		//dary[a-1][b-1] = 1
		//m[b-1] = append(m[b-1], a-1)
		m[[2]int64{x, y}] = a
		for j := x - 1; j <= x; j++ {
			for k := y - 1; k <= y; k++ {
				if !(j < 0 || j > r-2 || k < 0 || k > c-2) {
					kk[[2]int64{j, k}] += 1
				}
			}
		}
	}
	pqs := len(kk)
	pq := make(PriorityQueue, pqs)
	pqi := 0
	for k, v := range kk {
		//priority := strings.Index(s, colors[i])
		pq[pqi] = &Item{
			weight:   k,
			priority: v,
		}
		pqi++
	}

	heap.Init(&pq)
	q := make([]int64, 4)
	ok := make([]bool, 4)
	pairtate := map[int64][2]int64{}
	pairyoko := map[int64][2]int64{}
	for {
		if pq.Len() == 0 {
			break
		}
		item := heap.Pop(&pq).(*Item)
		k := item.weight
		q[0], ok[0] = m[k]
		q[1], ok[1] = m[[2]int64{k[0], k[1] + 1}]
		q[2], ok[2] = m[[2]int64{k[0] + 1, k[1]}]
		q[3], ok[3] = m[[2]int64{k[0] + 1, k[1] + 1}]
		boolsum := 0
		intsum := int64(0)
		for i := 0; i < 4; i++ {
			boolsum += bool2int(ok[i])
			intsum += q[i]
		}
		if boolsum == 4 {
			if q[0]+q[3] != q[1]+q[2] {
				ans = false
			}
		} else if boolsum == 3 {
			x := k[0]
			y := k[1]
			ans2 := (q[0] + q[3]) - (q[1] + q[2])
			if !ok[2] || !ok[1] {
				ans2 = -ans2 // (q[0] + q[3]) - (q[1] + q[2])
			}
			if !ok[1] || !ok[3] {
				y += 1
			}
			if !ok[2] || !ok[3] {
				x += 1
			}
			if ans2 > 0 {
				ans = false
			}
			m[[2]int64{x, y}] = -ans2 //((q[0] + q[3]), (q[1] + q[2]))
			for j := x - 1; j <= x; j++ {
				for k := y - 1; k <= y; k++ {
					if !(j < 0 || j > r-2 || k < 0 || k > c-2) {
						if kk[[2]int64{j, k}] == 0 {
							item := Item{
								weight:   [2]int64{j, k},
								priority: kk[[2]int64{j, k}] + 1,
							}
							pt := &item
							heap.Push(&pq, pt)
						}
						//kk[[2]int64{j, k}] += 1
					}
				}
			}
		} else if boolsum == 2 {
			x := k[0]
			y := k[1]
			if !ok[0] && !ok[1] {
				p, ok := pairyoko[x]
				if ok {
					if p[0]+q[3] != p[1]+q[2] {
						ans = false
					}
				} else {
					pairyoko[x] = [2]int64{q[2], q[3]}
				}

				m[[2]int64{x, y}] = q[2]     //((q[0] + q[3]), (q[1] + q[2]))
				m[[2]int64{x, y + 1}] = q[3] //((q[0] + q[3]), (q[1] + q[2]))
			} else if !ok[0] && !ok[2] {
				p, ok := pairtate[x]
				if ok {
					if p[0]+q[3] != q[1]+p[1] {
						ans = false
					}
				} else {
					pairtate[y] = [2]int64{q[1], q[3]}
				}
				m[[2]int64{x, y}] = q[1]     //((q[0] + q[3]), (q[1] + q[2]))
				m[[2]int64{x + 1, y}] = q[3] //((q[0] + q[3]), (q[1] + q[2]))
			} else if !ok[2] && !ok[3] {
				p, ok := pairyoko[x]
				if ok {
					if p[1]+q[0] != q[1]+p[0] {
						ans = false
					}
				} else {
					pairyoko[x] = [2]int64{q[0], q[1]}
				}
				m[[2]int64{x + 1, y}] = q[0]     //((q[0] + q[3]), (q[1] + q[2]))
				m[[2]int64{x + 1, y + 1}] = q[1] //((q[0] + q[3]), (q[1] + q[2]))
			} else if !ok[1] && !ok[3] {
				p, ok := pairtate[x]
				if ok {
					if p[1]+q[0] != q[2]+p[0] {
						ans = false
					}
				} else {

					pairtate[y] = [2]int64{q[0], q[2]}
				}
				m[[2]int64{x, y + 1}] = q[0]     //((q[0] + q[3]), (q[1] + q[2]))
				m[[2]int64{x + 1, y + 1}] = q[2] //((q[0] + q[3]), (q[1] + q[2]))
			}
			/*
				if !(j < 0 || j > r-2 || k < 0 || k > c-2) {
					if kk[[2]int64{j, k}] == 0 {
						item := Item{
							weight:   [2]int64{j, k},
							priority: kk[[2]int64{j, k}] + 1,
						}
						pt := &item
						heap.Push(&pq, pt)
					}
				}
				if !(l < 0 || l > r-2 || v < 0 || v > c-2) {
					if kk[[2]int64{l, v}] == 0 {
						item := Item{
							weight:   [2]int64{l, v},
							priority: kk[[2]int64{l, v}] + 1,
						}
						pt := &item
						heap.Push(&pq, pt)
					}
				}*/
		}
		//fmt.Println(q, boolsum, m)
	}
	if ans {
		fmt.Println("Yes")
	} else {
		fmt.Println("No")
	}

}