PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#FT0000062. Đếm giao cắt giữa các đường cao tốc (Japan)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Đếm giao cắt giữa các đường cao tốc (Japan)

Japan

Source: SPOJ

Version: Phuoc Hung OJ Extended

Problem Statement

Japan plans to build KK straight superhighways between cities on the eastern and western coasts of a long island. There are NN cities on the east coast and MM on the west coast. Cities on either coast are numbered 1,2,…1,2,\ldots from north to south.

Each highway connects a city on the east coast to a city on the west coast. A crossing occurs when two highways intersect in the interiors of their segments. Sharing a city endpoint is not counted as a crossing. No three highway interiors meet at the same point.

Count the pairs of highways that cross, counting each pair once.

Input

The first line contains N,M,KN,M,K, the numbers of eastern cities, western cities, and highways.

Each of the following KK lines contains integers u,vu,v, denoting the highway from eastern city uu to western city vv.

Output

Print one integer: the number of crossing highway pairs.

Subtasks

  • Subtask 1 (20 points): 1≤N,M≤121\le N,M\le 12; 0≤K≤min⁡(50,N⋅M)0\le K\le \min(50,N\cdot M); 1≤u≤N1\le u\le N, 1≤v≤M1\le v\le M for every highway; the endpoint pairs are distinct.

  • Subtask 2 (30 points): 1≤N,M≤2001\le N,M\le 200; 0≤K≤min⁡(4000,N⋅M)0\le K\le \min(4000,N\cdot M); 1≤u≤N1\le u\le N, 1≤v≤M1\le v\le M for every highway; the endpoint pairs are distinct.

  • Subtask 3 (50 points): 1≤N,M≤10001\le N,M\le 1000; 0≤K≤min⁡(1000000,N⋅M)0\le K\le \min(1000000,N\cdot M); 1≤u≤N1\le u\le N, 1≤v≤M1\le v\le M for every highway; the endpoint pairs are distinct.

Examples

Input

3 4 4
1 4
2 3
3 2
3 1

Output

5

Explanation

The highways connect (1,4)(1,4), (2,3)(2,3), (3,2)(3,2), and (3,1)(3,1), with eastern coordinates first. Highway (1,4)(1,4) crosses each of the other three highways, contributing 33. Highway (2,3)(2,3) crosses both highways starting at eastern city 33, contributing 22 more.

The highways (3,2)(3,2) and (3,1)(3,1) share an endpoint and are not counted as crossing. Hence 3+2=53+2=5.