Codeforces Round 168 (Div. 1)
E
Mirror Room
Codeforces
3000
原题
← D→
翻译中…

Imagine an $*n* × *m*$ grid with some blocked cells. The top left cell in the grid has coordinates $(1, 1)$ and the bottom right cell has coordinates $(*n*, *m*)$. There are $*k*$ blocked cells in the grid and others are empty. You flash a laser beam from the center of an empty cell $(*x*_{*s*}, *y*_{*s*})$ in one of the diagonal directions (i.e. north-east, north-west, south-east or south-west). If the beam hits a blocked cell or the border of the grid it will reflect. The behavior of the beam reflection in different situations is depicted in the figure below.

翻译中…

image

翻译中…

After a while the beam enters an infinite cycle. Count the number of empty cells that the beam goes through at least once. We consider that the beam goes through cell if it goes through its center.

翻译中…

Input

翻译中…

The first line of the input contains three integers $*n*$, $*m*$ and $*k*$ $(1 ≤ *n*, *m* ≤ 10^{5}, 0 ≤ *k* ≤ 10^{5})$. Each of the next $*k*$ lines contains two integers $*x*_{*i*}$ and $*y*_{*i*}$ $(1 ≤ *x*_{*i*} ≤ *n*, 1 ≤ *y*_{*i*} ≤ *m*)$ indicating the position of the $*i*$-th blocked cell.

翻译中…

The last line contains $*x*_{*s*}$, $*y*_{*s*}$ $(1 ≤ *x*_{*s*} ≤ *n*, 1 ≤ *y*_{*s*} ≤ *m*)$ and the flash direction which is equal to "NE", "NW", "SE" or "SW". These strings denote directions $( - 1, 1)$, $( - 1,  - 1)$, $(1, 1)$, $(1,  - 1)$.

翻译中…

It's guaranteed that no two blocked cells have the same coordinates.

翻译中…

Output

翻译中…

In the only line of the output print the number of empty cells that the beam goes through at least once.

翻译中…

Please, do not write the %lld specifier to read or write 64-bit integers in С++. It is preferred to use the cin, cout streams or the %I64d specifier.

翻译中…

样例

Input #1
3 3 0
1 2 SW
Output #1
6
InputCopy OutputCopy
Input #2
7 5 3
3 3
4 3
5 3
2 1 SE
Output #2
14
InputCopy OutputCopy
题目信息
题号
274E
时间限制
2 s
内存限制
256 MB
标签
数据结构
模拟

题面与样例版权归 Codeforces 及出题人所有;中文翻译由 AI 生成(CI 管线),如有出入请以原文为准。