Problem p02580 (C++)

Buggy submission (status: Time Limit Exceeded):
/*  -*- coding: utf-8 -*-
 *
 * e.cc: E - Bomber
 */

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

/* constant */

const int MAX_H = 300000;
const int MAX_W = 300000;
const int MAX_M = 300000;

/* typedef */

typedef vector<int> vi;
typedef pair<int, int> pii;

/* global variables */

pii ps[MAX_M];
int cs[MAX_M], ycs[MAX_H], xcs[MAX_W];

/* subroutines */

/* main */

int main() {
  int h, w, m;
  scanf("%d%d%d", &h, &w, &m);

  for (int i = 0; i < m; i++) {
    int y, x;
    scanf("%d%d", &y, &x);
    y--, x--;
    ps[i] = pii(y, x);
  }
  sort(ps, ps + m);

  int n = 0;
  for (int i = 0; i < m;) {
    int j = i + 1;
    while (j < m && ps[i] == ps[j])
      j++;
    ps[n] = ps[i];
    cs[n] = j - i;
    n++;
    i = j;
  }

  for (int i = 0; i < n; i++) {
    ycs[ps[i].first] += cs[i];
    xcs[ps[i].second] += cs[i];
  }
  // for (int i = 0; i < h; i++) printf("%d ", ycs[i]); putchar('\n');
  // for (int i = 0; i < w; i++) printf("%d ", xcs[i]); putchar('\n');

  int maxyc = -1, maxxc = -1;
  vi maxys, maxxs;
  for (int y = 0; y < h; y++) {
    if (maxyc < ycs[y])
      maxyc = ycs[y], maxys.clear(), maxys.push_back(y);
    else if (maxyc == ycs[y])
      maxys.push_back(y);
  }
  for (int x = 0; x < w; x++) {
    if (maxxc < xcs[x])
      maxxc = xcs[x], maxxs.clear(), maxxs.push_back(x);
    else if (maxxc == xcs[x])
      maxxs.push_back(x);
  }

  int maxc = 0;
  for (vi::iterator yit = maxys.begin(); yit != maxys.end(); yit++)
    for (vi::iterator xit = maxxs.begin(); xit != maxxs.end(); xit++) {
      int c = maxyc + maxxc;
      pii p(*yit, *xit);
      int k = lower_bound(ps, ps + n, p) - ps;
      if (k < n && ps[k] == p)
        c -= cs[k];
      if (maxc < c)
        maxc = c;
    }

  printf("%d\n", maxc);
  return 0;
}


Fixed submission:
/*  -*- coding: utf-8 -*-
 *
 * e.cc: E - Bomber
 */

#include <algorithm>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <stack>
#include <string>
#include <utility>
#include <vector>

using namespace std;

/* constant */

const int MAX_H = 300000;
const int MAX_W = 300000;
const int MAX_M = 300000;

/* typedef */

typedef vector<int> vi;
typedef pair<int, int> pii;

/* global variables */

pii ps[MAX_M];
int cs[MAX_M], ycs[MAX_H], xcs[MAX_W];

/* subroutines */

/* main */

int main() {
  int h, w, m;
  scanf("%d%d%d", &h, &w, &m);

  for (int i = 0; i < m; i++) {
    int y, x;
    scanf("%d%d", &y, &x);
    y--, x--;
    ps[i] = pii(y, x);
  }
  sort(ps, ps + m);

  int n = 0;
  for (int i = 0; i < m;) {
    int j = i + 1;
    while (j < m && ps[i] == ps[j])
      j++;
    ps[n] = ps[i];
    cs[n] = j - i;
    n++;
    i = j;
  }

  for (int i = 0; i < n; i++) {
    ycs[ps[i].first] += cs[i];
    xcs[ps[i].second] += cs[i];
  }
  // for (int i = 0; i < h; i++) printf("%d ", ycs[i]); putchar('\n');
  // for (int i = 0; i < w; i++) printf("%d ", xcs[i]); putchar('\n');

  int maxyc = -1, maxxc = -1;
  vi maxys, maxxs;
  for (int y = 0; y < h; y++) {
    if (maxyc < ycs[y])
      maxyc = ycs[y], maxys.clear(), maxys.push_back(y);
    else if (maxyc == ycs[y])
      maxys.push_back(y);
  }
  for (int x = 0; x < w; x++) {
    if (maxxc < xcs[x])
      maxxc = xcs[x], maxxs.clear(), maxxs.push_back(x);
    else if (maxxc == xcs[x])
      maxxs.push_back(x);
  }

  int maxc = 0;
  for (vi::iterator yit = maxys.begin(); yit != maxys.end(); yit++)
    for (vi::iterator xit = maxxs.begin(); xit != maxxs.end(); xit++) {
      int c = maxyc + maxxc;
      pii p(*yit, *xit);
      int k = lower_bound(ps, ps + n, p) - ps;
      if (k >= n || ps[k] != p) {
        maxc = c;
        break;
      }
      c -= cs[k];
      if (maxc < c)
        maxc = c;
    }

  printf("%d\n", maxc);
  return 0;
}