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 C++ (GCC 9.2.1) (judge verdict: Runtime Error)


//(・ω・)
#include <stdio.h>
#include <iostream> // cout, endl, cin
#include <string> // string, to_string, atoi
#include <vector> // vector
#include <algorithm> // min, max, swap, sort, reverse, lower_bound, upper_bound
#include <utility> // pair, make_pair
#include <tuple> // tuple, make_tuple
#include <cstdint> // int64_t, int*_t
#include <cstdio> // printf
#include <map> // map
#include <queue> // queue, priority_queue
#include <set> // set
#include <stack> // stack
#include <deque> // deque
#include <unordered_map> // unordered_map
#include <unordered_set> // unordered_set
#include <bitset> // bitset
#include <cctype> // isupper, islower, isdigit, toupper, tolower
#include <math.h>
#include <iomanip> //setprecision
#include <limits>
#include <sstream> //istringstream
#include <stdlib.h>
#include <list>
#include <iterator>//std::advance()
using namespace std;
int main(){
    int H,W,m;
    cin >> H >> W >> m;
    vector<int> h(m);
    vector<int> w(m);
    vector<vector<int>> b(H+1,vector<int>(W+1,0));
    for(int i=0; i<m; i++){
        cin >> h[i] >> w[i];
        b[h[i]][w[i]]++;
    }
    sort(h.begin(),h.end());
    sort(w.begin(),w.end());
    vector<pair<int,int>> hcount;
    vector<pair<int,int>> wcount;
    int hc=1,wc=1;
    for(int i=0; i<m-1; i++){
        if(w[i]==w[i+1]) wc++;
        else{
            wcount.push_back(make_pair(wc,w[i]));
            wc=1;
        }
        if(h[i]==h[i+1]) hc++;
        else{
            hcount.push_back(make_pair(hc,h[i]));
            hc=1;
        }
    }
    wcount.push_back(make_pair(wc,w[m-1]));
    hcount.push_back(make_pair(hc,h[m-1]));

    sort(hcount.begin(),hcount.end(),greater<pair<int,int>>());
    sort(wcount.begin(),wcount.end(),greater<pair<int,int>>());
    int max=0;
    for(int i=0; i<hcount.size(); i++){
        for(int j=0; j<wcount.size(); j++){
            int x;
            x=hcount[i].first+wcount[j].first-b[hcount[i].second][wcount[j].second];
            if(x>max) max=x;
        }
    }
    cout << max << endl;

}