#P12634. [UOI 2020] Chessfield

[UOI 2020] Chessfield

题目描述

Pani Annochka recently got an interview with an IT company. In this interview, she was given a very interesting task:

The girl is given a field divided into squares. That is, each side is 11. And each square is painted in either white or black.

We say that a chessfield has sizes nn and mm, when it consists of rectangles having nn units in height and mm units in width. Also, all these rectangles should be painted either white or black. And each white rectangle borders only on black and vice versa --- each black borders only on white.

For example, the following chessboard has sizes 22 and 33 (blue is the coordinate axis):

But these are not:

As you may have noticed, there are often a lot of chessfiels with sizes nn and mm. The challenge is to say how many there are. No-no-no, it's too easy.

Annochka also knows kk squares on this field and their colors. Pani must find the number of chessfields with sizes nn and mm such that the squares with the corresponding coordinates have the specified colors. Help her do it!

More formally, you are given kk triplets of numbers: xi,yi,cix_i, y_i, c_i, where (xi,yix_i,y_i) --- the coordinates of the lower left edge of the square, and cic_i is 00 if this square is to be is painted black, and is 11 if this square is to be painted white. You need to print the number of different chessfields with sizes nn and mm that fit under these constraints. Two chessfields are considered different if there is at least one square painted in different colors on these planes.

输入格式

The first line contains four integers nn, mm, kk, gg (1k1051 \leq k \leq 10^5, 1n,m1091 \leq n, m \leq 10^9, 0g40 \leq g \leq 4) --- the height of each rectangular, the width of each rectangular, the number of known squares and the number of group.

Each of the next kk lines contains three integers xix_i, yiy_i, cic_i (1xi,yi109,0ci11 \leq x_i, y_i \leq 10^9, 0 \leq c_i \leq 1) --- the coordinates and color size. Note that coordinates can be repeated.

输出格式

Print one integer --- the number of different chess planes with sizes nn and mm that fit the given constraints.

2 2 4 0
1 4 0
2 3 0
3 3 1
4 4 1
1
2 6 2 0
1 2 0
3 2 0
8

提示

On the left is an image of a single plane that fits the 11 example limit.

On the right side there is one of the 88 possible chessfields that satisfy the conditions of the example 22.

(Blue is the coordinate axis, light gray --- squares, which should be white, dark gray --- squares, which should be black).

  • (1717 points): 1n,m,xi,yi,k1001 \leq n, m, x_i, y_i, k \leq 100;
  • (2424 points): 1n,m,k1001 \leq n, m, k \leq 100;
  • (3131 points): 1nm105,1k1041 \leq n \cdot m \leq 10^5, 1 \leq k \leq 10^4;
  • (2828 points): without additional restrictions.