Three Body
题号:NC239619
时间限制:C/C++/Rust/Pascal 3秒,其他语言6秒
空间限制:C/C++/Rust/Pascal 256 M,其他语言512 M
Special Judge, 64bit IO Format: %lld

题目描述

See Problem N for PDF statements.
"The fish who dried the sea went onto land before they did this. They move from one dark forest to another dark forest."

You are now in a K-dimensional universe, which could be seen as an hyperrectangle. To be specific, let's divide the universe into hypercubes sized to describe different regions. The position of each region can be denoted by a K-dimensional integer coordinate (a_1, a_2, ..., a_K) where . Each region owns a and the initial value is K. However, due to the space war and the abuse of dimensionality weapons, the of some regions in the universe has decayed to less than K.

Your spaceship is an hyperrectangle which can also be viewed as parts of hypercubes sized . The position of each part can also be denoted by a K-dimensional integer coordinate (b_1, b_2, ..., b_K) where . Each part of your spaceship also owns a which is initially K. Thanks to advanced technology, some parts of your spaceship are reformed and their become less than K, which can make them enter the regions with lower .

We define the spaceship to be in a valid position if the following conditions are met:
  • Each part of the spaceship is located exactly in one region.
  • Each part of the spaceship cannot have a strictly higher than the of the region.
Please calculate the number of different valid positions for your spaceship. Please note that you can't rotate the spaceship.

输入描述:

The first line contains an integer K ().

The second line contains K integers n_1,n_2,...,n_K (), indicating the size of the universe.

The third line is an integer c_u (), indicating the number of regions whose has decayed.

Each of the following c_u lines contains integers a_1,a_2,...,a_K and d (, ), indicating that the of region (a_1,a_2,...,a_K) has decayed to d. It is guaranteed that the c_u regions are pairwise different.

The next line contains K integers m_1,m_2,...,m_K (), indicating the size of the spaceship.

The next line contains an integer c_s (), indicating the number of parts of the spaceship are reformed.

Each of the following c_s lines contains integers b_1,b_2,...,b_K and d (, ), indicating that the of part (b_1,b_2,...,b_K) becomes d. It is guaranteed that the c_s parts are pairwise different.

输出描述:

Output one integer indicating the number of different valid positions for your spaceship.
示例1

输入

复制
2
4 4
7
0 0 1
1 0 1
1 2 1
2 0 1
2 1 1
2 3 1
3 3 1
2 2
1
1 1 1

输出

复制
1

说明

For the first sample, the universe is two-dimensional. The \textit{Dimensional Values} of the universe and spaceship are as follows.

示例2

输入

复制
3
4 4 4
4
0 1 0 1
1 2 2 2
3 2 3 2
3 3 0 1
2 2 2
1
1 0 0 2

输出

复制
15