Problem:
Score : 200 points

Problem Statement

There is a rectangle in the xy-plane, with its lower left corner at (0, 0) and its upper right corner at (W, H). Each of its sides is parallel to the x-axis or y-axis. Initially, the whole region within the rectangle is painted white.

Snuke plotted N points into the rectangle. The coordinate of the i-th (1 ≦ i ≦ N) point was (x_i, y_i).

Then, he created an integer sequence a of length N, and for each 1 ≦ i ≦ N, he painted some region within the rectangle black, as follows:

If a_i = 1, he painted the region satisfying x < x_i within the rectangle.

If a_i = 2, he painted the region satisfying x > x_i within the rectangle.

If a_i = 3, he painted the region satisfying y < y_i within the rectangle.

If a_i = 4, he painted the region satisfying y > y_i within the rectangle.

Find the area of the white region within the rectangle after he finished painting.

Constraints

1 ≦ W, H ≦ 100

1 ≦ N ≦ 100

0 ≦ x_i ≦ W (1 ≦ i ≦ N)

0 ≦ y_i ≦ H (1 ≦ i ≦ N)

W, H (21:32, added), x_i and y_i are integers.

a_i (1 ≦ i ≦ N) is 1, 2, 3 or 4.

Input

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

W H N
x_1 y_1 a_1
x_2 y_2 a_2
:
x_N y_N a_N

Output

Print the area of the white region within the rectangle after Snuke finished painting.

Sample Input 1

5 4 2
2 1 1
3 3 4

Sample Output 1

9

The figure below shows the rectangle before Snuke starts painting.

First, as (x_1, y_1) = (2, 1) and a_1 = 1, he paints the region satisfying x < 2 within the rectangle:

Then, as (x_2, y_2) = (3, 3) and a_2 = 4, he paints the region satisfying y > 3 within the rectangle:

Now, the area of the white region within the rectangle is 9.

Sample Input 2

5 4 3
2 1 1
3 3 4
1 4 2

Sample Output 2

0

It is possible that the whole region within the rectangle is painted black.

Sample Input 3

10 10 5
1 6 1
4 1 3
6 9 4
9 4 2
3 1 3

Sample Output 3

64

Solution in C# (Mono 4.6.2.0) (judge verdict: Accepted)

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text.RegularExpressions;

using static System.Console;
using static AtCoder.Util;
using static AtCoder.Cin;
using static System.Math;

namespace AtCoder {
    class Program {

        struct RangeEnd : IComparable<RangeEnd> {
            public int Place;
            public int T;

            public RangeEnd(int place, int t) {
                Place = place;
                T = t;
            }
            public int CompareTo(RangeEnd other) => 
                this.Place.CompareTo(other.Place);
        }

        static void Main() {
            var w = ReadInt();
            var h = ReadInt();
            var n = ReadLong().debug();
            var field = Range(0, h).Select(_=>Range(0, w).Select(s=> false).ToList()).ToList();
            n.Times(() => {
                var x = ReadInt();
                var y = ReadInt();
                var a = ReadInt();

                switch (a) {
                    case 1:
                        for (int i = 0; i < h; i++) for (int j = 0; j < x; j++) field[i][j] = true;
                    break;
                    case 2:
                        for (int i = 0; i < h; i++) for (int j = x; j < w; j++) field[i][j] = true;
                    break;
                    case 3:
                        for (int i = 0; i < y; i++) for (int j = 0; j < w; j++) field[i][j] = true;
                    break;
                    case 4:
                        for (int i = y; i < h; i++) for (int j = 0; j < w; j++) field[i][j] = true;
                    break;
                }
            });
            field.SelectMany(x=>x).Where(x=>x).Count().Call(i => w * h - i).WriteLine();
        }
    }
}
/* ***************** Following Contents are my common library ******** */

namespace AtCoder {
    static class Util {
        public const long MODCONST = 1000000007;

        public static T debug<T>(this T value) {
            Console.Error.WriteLine($"debug:{value}");
            return value;
        }

        public static long ToLong(this String s) => long.Parse(s);
        public static long ToLong(this char c) => c - '0';
        public static int ToInt(this String s) => int.Parse(s);
        public static int ToInt(this char c) => c - '0';
        public static void Times(this int n, Action action) {
            for (int i = 0; i < n; i++) action();
        }
        public static void Times(this long n, Action action) {
            for (long i = 0; i < n; i++) action();
        }
        public static void Call<T>(this T t, Action<T> action) => action(t);
        public static bool In<T>(this T t, IEnumerable<T> range) =>
            range.Contains(t);
        public static void WriteLine<T>(this T t) => Console.WriteLine(t);
        public static T Call<S, T>(this S s, Func<S, T> func) => func(s);
        public static void Each<T>(this IEnumerable<T> e, Action<T> action) {
            foreach (var v in e) action(v);
        }

        public static VectorInt2 ReadVectorInt2() => 
            new VectorInt2(ReadInt(), ReadInt());

        public static string ReplaceX(this string input, string pattern, string replace) =>
            Regex.Replace(input, pattern, replace);

        public static IEnumerable<int> Range(int i, int j) => Enumerable.Range(i, j);
        public static void Swap<T>(this IList<T> enumerable, int i, int j) {
            var buf = enumerable[i];
            enumerable[i] = enumerable[j];
            enumerable[j] = buf;

        }
        public static void ReverseRange<T>(this IList<T> enumerable, int i, int j) {
            int half = (j - i) / 2;
            for (int k = 0; k <= half; k++) enumerable.Swap(i + k, j - k);
        }
        
        public static bool isEmpty<T>(this IEnumerable<T> enumerable) => !enumerable.Any();
    }

    static class Cin {
        private static Queue<string> tokens;
        static Cin () {
            string line;
            tokens = new Queue<string> ();
            while ((line = Console.ReadLine ()) != null) {
                foreach (var token in line.Split (' ')) {
                    tokens.Enqueue (token);
                }
            }
        }

        static public int ReadInt() => int.Parse(tokens.Dequeue());
        static public IEnumerable<int> ReadInt(long n) {
            var list = new List<int>();
            for (int i = 0; i < n; i++) list.Add(ReadInt());
            return list;
        }
        static public long ReadLong() => long.Parse(tokens.Dequeue());
        static public IEnumerable<long> ReadLong(long n) {
            for (int i = 0; i < n; i++) yield return ReadLong();
        }
        static public string ReadString() => tokens.Dequeue();
        static public IEnumerable<string> ReadString(long n) {
            for (int i = 0; i < n; i++) yield return ReadString();
        }
    }

    struct VectorInt2 {
        public int X { get; set; }
        public int Y { get; set; }

        public VectorInt2(int x, int y) {
            X = x; Y = y;
        }

        static public VectorInt2 operator+ (VectorInt2 v1, VectorInt2 v2) =>
            new VectorInt2(v1.X + v2.X, v1.Y + v2.Y);
        static public VectorInt2 operator- (VectorInt2 v1, VectorInt2 v2) =>
            new VectorInt2(v1.X - v2.X, v1.Y - v2.Y);
        static public VectorInt2 operator* (VectorInt2 v1, VectorInt2 v2) =>
            new VectorInt2(v1.X * v2.X, v1.Y * v2.Y);
        static public VectorInt2 operator* (VectorInt2 v1, int i) =>
            new VectorInt2(v1.X * i, v1.Y * i);
        static public VectorInt2 operator* (int i, VectorInt2 v2) =>
            new VectorInt2(i * v2.X, i * v2.Y);
        static public VectorInt2 operator/ (VectorInt2 v1, int i) =>
            new VectorInt2(v1.X / i, v1.Y / i);
    }
}