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 Scala (2.13.1) (judge verdict: Wrong Answer)

object Main extends App {
  val sc = new FastScanner(System.in)
  val h, w, m = sc.nextInt()
  val hw = Array.fill(m)((sc.nextInt()-1, sc.nextInt()-1))
  var bombs = hw.map{case (i, j) => i * w + j}.sorted
  val hb = Array.fill(h)(0)
  val wb = Array.fill(w)(0)
  for ((h, w) <- hw) {
    hb(h) += 1
    wb(w) += 1
  }

  def binSearch(begin: Int, end: Int, k: Int): Int = {
    if (end - begin <= 1) {
      begin
    } else {
      val m = begin + (end - begin) / 2
      if (bombs(m) >= k) binSearch(m, end, k) else binSearch(begin, m, k)
    }
  }

  val hMax = hb.max
  val wMax = wb.max
  val hMaxIndices = hb.zipWithIndex.collect{case (b, idx) if b == hMax => idx}
  val wMaxIndices = wb.zipWithIndex.collect{case (b, idx) if b == wMax => idx}
  if (hMaxIndices.length * wMaxIndices.length > m) {
  } else {
    var found = false
    for {
      hi <- hMaxIndices
      wi <- wMaxIndices
      if !found
    } {
      if (binSearch(0, m, hi * w + wi) == hi * w + wi) {
        found = true
      }
    }
    println(if (found) hb.max + wb.max else hb.max + wb.max - 1)
  }
}

class FastScanner(source: java.io.InputStream) {
  val buf = new Array[Byte](1024)
  var now, end = 0

  def isPrintable(b: Byte): Boolean = 33 <= b && b <= 126
  def isNum(b: Byte): Boolean = 48 <= b && b <= 57

  def hasNextByte: Boolean = {
    if (now < end) {
      true
    } else {
      now = 0
      end = source.read(buf)
      end > 0
    }
  }

  def nextByte(): Byte = {
    if (hasNextByte) {
      val b = buf(now)
      now += 1
      b
    } else {
      -1
    }
  }

  def hasNext: Boolean = {
    while (hasNextByte && !isPrintable(buf(now))) {
      now += 1
    }
    hasNextByte
  }

  def next(): String = {
    if (hasNext) {
      val sb = new StringBuilder()
      var b = nextByte()
      while (isPrintable(b)) {
        sb += b.toChar
        b = nextByte()
      }
      sb.toString
    } else {
      throw new NoSuchElementException()
    }
  }

  def nextInt(): Int = {
    nextLong().toInt
  }

  def nextLong(): Long = {
    if (hasNext) {
      var b = nextByte()
      var sign = 1
      var n = 0L
      if (b == '-') {
        sign = -1
        b = nextByte()
      }
      while (isNum(b)) {
        n = n * 10 + (b - 48)
        b = nextByte()
      }
      sign * n
    } else {
      throw new NoSuchElementException()
    }
  }

  def nextDouble(): Double = next().toDouble
}