Problem:
Score : 500 points

Problem Statement

We have a two-dimensional grid with H \times W squares. There are M targets to destroy in this grid - the position of the i-th target is \left(h_i, w_i \right).

Takahashi will choose one square in this grid, place a bomb there, and ignite it. The bomb will destroy all targets that are in the row or the column where the bomb is placed. It is possible to place the bomb at a square with a target.

Takahashi is trying to maximize the number of targets to destroy. Find the maximum number of targets that can be destroyed.

Constraints

All values in input are integers.

1 \leq H, W \leq 3 \times 10^5

1 \leq M \leq \min\left(H\times W, 3 \times 10^5\right)

1 \leq h_i \leq H

1 \leq w_i \leq W

\left(h_i, w_i\right) \neq \left(h_j, w_j\right) \left(i \neq j\right)

Input

Input is given from Standard Input in the following format:

H W M
h_1 w_1
\vdots
h_M w_M

Output

Print the answer.

Sample Input 1

2 3 3
2 2
1 1
1 3

Sample Output 1

3

We can destroy all the targets by placing the bomb at \left(1, 2\right).

Sample Input 2

3 3 4
3 3
3 1
1 1
1 2

Sample Output 2

3

Sample Input 3

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

Sample Output 3

6

Solution in Haskell (GHC 8.8.3) (judge verdict: Accepted)

import Control.Monad
import Control.Applicative
import Data.Maybe
import Data.List
import qualified Data.ByteString.Char8 as BS
import qualified Data.Vector as V
import Debug.Trace as De

-- 入出力ライブラリ（コピペ） --
readInt = fst . fromJust . BS.readInt
readIntList = map readInt . BS.words
getInt = readInt <$> BS.getLine
getIntList = readIntList <$> BS.getLine
getIntNList n = map readIntList <$> replicateM (fromIntegral n) BS.getLine

-- 関数 --
func :: Int -> V.Vector Int -> [Int]
func nn av = solve 0 0 0
  where
    solve :: Int -> Int -> Int -> [Int]
    solve n px qx
      | n == 300100 = []
      | px == nn = ((px-qx):(solve (n+1) px px))
      | (av V.! px) /= n = ((px-qx):(solve (n+1) px px))
      | (av V.! qx) == n = solve n (px+1) qx

get1 :: [Int] -> Int
get1 x = (x!!0)

get2 :: [Int] -> Int
get2 x = (x!!1)

hantei :: (Int, Int) -> Bool
hantei x
  | fst(x) == 0 = True
  | otherwise = False

reflect :: [Int] -> Int -> [Int]
reflect x border = map hikizan x
  where
    hikizan :: Int -> Int
    hikizan t = t - border

tansaku :: Int -> V.Vector Int -> V.Vector Int -> V.Vector Int -> V.Vector Int -> Int
tansaku n xv yv ix iy = solve 0
  where
    solve :: Int -> Int
    solve pos
      | pos == n = 0
      | otherwise = newnum + (solve (pos+1))
        where newnum = if ((ix V.! (xv V.! pos)) == 1 && (iy V.! (yv V.! pos)) == 1) then 1 else 0

main = do
  -- 入力 --
  [h, w, n] <- (map read . words) <$> getLine
  a <- getIntNList n
  
  -- パース --
  let x = map get1 a
  let y = map get2 a
  let xw = (V.fromList x) :: V.Vector Int
  let yw = (V.fromList y) :: V.Vector Int
  let xs = sort x
  let ys = sort y
  let xv = (V.fromList xs) :: V.Vector Int
  let yv = (V.fromList ys) :: V.Vector Int
  
  -- 本質部分 --
  let numx = func n xv
  let numy = func n yv
  let maxx = maximum numx
  let maxy = maximum numy
  let evalx = reflect numx maxx
  let evaly = reflect numy maxy
  let ex = zip evalx [0..300100]
  let ey = zip evaly [0..300100]
  let fx = filter hantei ex
  let fy = filter hantei ey
  let gx = snd (unzip fx)
  let gy = snd (unzip fy)
  let gx2 = (V.fromList gx) :: V.Vector Int
  let gy2 = (V.fromList gy) :: V.Vector Int
  
  -- 探索パート --
  let szx = length gx
  let szy = length gy
  let hx = func szx gx2
  let hy = func szy gy2
  let ix = (V.fromList hx) :: V.Vector Int
  let iy = (V.fromList hy) :: V.Vector Int
  let ret = tansaku n xw yw ix iy
  
  -- 答えを出力 --
  let ans = if (ret == (szx * szy)) then (maxx + maxy - 1) else (maxx + maxy)
  putStrLn $ show ans