alexjercan/bugnet huggingface dataset:
Problem p02579 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
const ll INF = 2305843009213693951;
ll dx[4] = {1, 0, -1, 0};
ll dy[4] = {0, 1, 0, -1};

int main() {
  ll H, W, ch, cw, dh, dw;
  cin >> H >> W >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  string field[1000];
  for (ll i = 0; i < H; i++) {
    cin >> field[i];
  }

  deque<pair<ll, ll>> Q;
  ll cost[1020][1020];
  for (ll i = 0; i < 1020; i++) {
    for (ll j = 0; j < 1020; j++) {
      cost[i][j] = INF;
    }
  }
  cost[ch][cw] = 0;
  Q.push_front({ch, cw});
  while (!Q.empty()) {
    pair<ll, ll> t = Q.front();
    Q.pop_front();
    ll i = t.first, j = t.second;
    for (ll k = 0; k < 4; k++) {
      ll x = i + dx[k], y = j + dy[k];
      if (x < 0 || x >= H)
        continue;
      if (y < 0 || y >= W)
        continue;
      if (field[x][y] != '.')
        continue;
      if (cost[x][y] <= cost[i][j])
        continue;
      Q.push_front({x, y});
      cost[x][y] = cost[i][j];
    }
    for (ll k = -2; k <= 2; k++) {
      for (ll l = -2; l <= 2; l++) {
        ll x = i + k, y = j + l;
        if (x < 0 || x >= H)
          continue;
        if (y < 0 || y >= W)
          continue;
        if (field[x][y] != '.')
          continue;
        if (cost[x][y] <= cost[i][j] + 1)
          continue;
        Q.push_back({x, y});
        cost[x][y] = cost[i][j] + 1;
      }
    }
  }
  if (cost[dh][dw] == INF) {
    cout << -1 << endl;
  } else {
    cout << cost[dh][dw] << endl;
  }
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
const ll MOD = 1e9 + 7;
const ll INF = 2305843009213693951;
ll dx[4] = {1, 0, -1, 0};
ll dy[4] = {0, 1, 0, -1};

int main() {
  ll H, W, ch, cw, dh, dw;
  cin >> H >> W >> ch >> cw >> dh >> dw;
  ch--;
  cw--;
  dh--;
  dw--;
  string field[1000];
  for (ll i = 0; i < H; i++) {
    cin >> field[i];
  }

  deque<pair<ll, ll>> Q;
  ll cost[1020][1020];
  for (ll i = 0; i < 1020; i++) {
    for (ll j = 0; j < 1020; j++) {
      cost[i][j] = INF;
    }
  }
  cost[ch][cw] = 0;
  Q.push_front({ch, cw});
  while (!Q.empty()) {
    pair<ll, ll> t = Q.front();
    Q.pop_front();
    ll i = t.first, j = t.second;
    for (ll k = 0; k < 4; k++) {
      ll x = i + dx[k], y = j + dy[k];
      if (x < 0 || x >= H)
        continue;
      if (y < 0 || y >= W)
        continue;
      if (field[x][y] != '.')
        continue;
      if (cost[x][y] <= cost[i][j])
        continue;
      Q.push_front({x, y});
      cost[x][y] = cost[i][j];
    }
    for (ll k = -2; k <= 2; k++) {
      for (ll l = -2; l <= 2; l++) {
        ll x = i + k, y = j + l;
        if (x < 0 || x >= H)
          continue;
        if (y < 0 || y >= W)
          continue;
        if (field[x][y] != '.')
          continue;
        if (cost[x][y] <= cost[i][j] + 1)
          continue;
        Q.push_front({x, y});
        cost[x][y] = cost[i][j] + 1;
      }
    }
  }
  if (cost[dh][dw] == INF) {
    cout << -1 << endl;
  } else {
    cout << cost[dh][dw] << endl;
  }
}
Problem p02579 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <iostream>
#include <queue>

using namespace std;

#define int long long

const int N = 1e3 + 10;

int dp[N][N];
char s[N][N];
bool vis[N][N];
int h, w, ch, cw;
int r[] = {0, 0, 1, -1};
int c[] = {1, -1, 0, 0};

void dijkstra() {
  priority_queue<pair<int, pair<int, int>>> q;
  q.push({0, {ch, cw}});
  while (!q.empty()) {
    auto z = q.top();
    int x = z.second.first;
    int y = z.second.second;
    q.pop();
    if (!vis[x][y]) {
      vis[x][y] = 1;
    } else if (dp[x][y] <= -z.first) {
      continue;
    }
    dp[x][y] = -z.first;
    for (int i = 0; i < 5; ++i) {
      for (int j = 0; j < 5; ++j) {
        int u = x - 2 + i;
        int v = y - 2 + j;
        if (u < 1 || u > h || v < 1 || v > w || s[u][v] == '#') {
          continue;
        }
        if (dp[u][v] > dp[x][y] + (abs(u - x) + abs(v - y) > 1)) {
          q.push({-(dp[x][y] + (abs(u - x) + abs(v - y) > 1)), {u, v}});
        }
      }
    }
  }
}

int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> h >> w >> ch >> cw;
  int dh, dw;
  cin >> dh >> dw;
  for (int i = 1; i <= h; ++i) {
    cin >> s[i] + 1;
  }
  for (int i = 1; i <= h; ++i) {
    for (int j = 1; j <= w; ++j) {
      dp[i][j] = 1e18;
    }
  }
  dijkstra();
  if (dp[dh][dw] == 1e18) {
    cout << -1;
  } else {
    cout << dp[dh][dw];
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <iostream>
#include <queue>

using namespace std;

#define int long long

const int N = 1e3 + 10;

int dp[N][N];
char s[N][N];
bool vis[N][N];
int h, w, ch, cw;
int r[] = {0, 0, 1, -1};
int c[] = {1, -1, 0, 0};

void dijkstra() {
  priority_queue<pair<int, pair<int, int>>> q;
  q.push({0, {ch, cw}});
  while (!q.empty()) {
    auto z = q.top();
    int x = z.second.first;
    int y = z.second.second;
    q.pop();
    if (!vis[x][y]) {
      vis[x][y] = 1;
    } else if (dp[x][y] < -z.first) {
      continue;
    }
    dp[x][y] = -z.first;
    for (int i = 0; i < 5; ++i) {
      for (int j = 0; j < 5; ++j) {
        int u = x - 2 + i;
        int v = y - 2 + j;
        if (u < 1 || u > h || v < 1 || v > w || s[u][v] == '#') {
          continue;
        }
        if (dp[u][v] > dp[x][y] + (abs(u - x) + abs(v - y) > 1)) {
          q.push({-(dp[x][y] + (abs(u - x) + abs(v - y) > 1)), {u, v}});
        }
      }
    }
  }
}

int32_t main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  cout.tie(0);
  cin >> h >> w >> ch >> cw;
  int dh, dw;
  cin >> dh >> dw;
  for (int i = 1; i <= h; ++i) {
    cin >> s[i] + 1;
  }
  for (int i = 1; i <= h; ++i) {
    for (int j = 1; j <= w; ++j) {
      dp[i][j] = 1e18;
    }
  }
  dijkstra();
  if (dp[dh][dw] == 1e18) {
    cout << -1;
  } else {
    cout << dp[dh][dw];
  }
  return 0;
}
Problem p02579 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define loop(i, a, n) for (int i = (a); i < (n); i++)
#ifdef _DEBUG
#define dd(x) cout << #x << " : " << x << endl
#define sorn(i, n) char((i == n - 1) ? '\\n' : ' ')
#else
#define dd(x)
#endif
using namespace std;
using ll = int64_t;
using P = pair<int, int>;

int main() {
  int h, w;
  cin >> h >> w;
  int s_i, s_j;
  cin >> s_i >> s_j;
  s_i--;
  s_j--;
  int g_i, g_j;
  cin >> g_i >> g_j;
  g_i--;
  g_j--;
  char m[h][w];
  bool vis[h][w];
  rep(i, h) rep(j, w) {
    cin >> m[i][j];
    vis[i][j] = false;
  }

  deque<pair<P, int>> q;
  q.push_back({{s_i, s_j}, 0});
  while (q.size()) {
    int now_i = q.front().first.first, now_j = q.front().first.second;
    int bnum = q.front().second;
    q.pop_front();
    if (vis[now_i][now_j])
      continue;
    vis[now_i][now_j] = true;
    if (now_i == g_i && now_j == g_j) {
      cout << bnum << endl;
      return 0;
    }
    for (int di = -2; di <= 2; di++)
      for (int dj = -2; dj <= 2; dj++) {
        int next_i = now_i + di, next_j = now_j + dj;
        if (0 <= next_i && next_i < h && 0 <= next_j && next_j < w &&
            !vis[next_i][next_j] && m[next_i][next_j] == '.') {
          if (abs(di) + abs(dj) <= 1)
            q.push_front({{next_i, next_j}, bnum});
          else
            q.push_back({{next_i, next_j}, bnum + 1});
        }
      }
  }
  cout << -1 << endl;

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < (n); i++)
#define loop(i, a, n) for (int i = (a); i < (n); i++)
#ifdef _DEBUG
#define dd(x) cout << #x << " : " << x << endl
#else
#define dd(x)
#endif
using namespace std;
using ll = int64_t;
using P = pair<int, int>;

constexpr int di2[] = {-2, -1, 0,  1,  2,  2, 2, 2, 2, 1, 0,  -1,
                       -2, -2, -2, -2, -1, 0, 1, 1, 1, 0, -1, -1};
constexpr int dj2[] = {-2, -2, -2, -2, -2, -1, 0,  1, 2, 2, 2, 2,
                       2,  1,  0,  -1, -1, -1, -1, 0, 1, 1, 1, 0};

int main() {
  int h, w;
  cin >> h >> w;
  int s_i, s_j;
  cin >> s_i >> s_j;
  s_i--;
  s_j--;
  int g_i, g_j;
  cin >> g_i >> g_j;
  g_i--;
  g_j--;
  char m[h][w];
  int warp[h][w];
  rep(i, h) rep(j, w) {
    cin >> m[i][j];
    warp[i][j] = 1e6 + 5;
  }

  queue<P> q;
  warp[s_i][s_j] = 0;
  q.push({s_i, s_j});
  while (q.size()) {
    int now_i = q.front().first, now_j = q.front().second;
    q.pop();
    rep(k, 24) {
      int next_i = now_i + di2[k], next_j = now_j + dj2[k];
      if (0 <= next_i && next_i < h && 0 <= next_j && next_j < w) {
        int next_warp = warp[now_i][now_j];
        if (k != 17 && k != 19 && k != 21 && k != 23)
          next_warp += 1;
        if (m[next_i][next_j] == '.' && next_warp < warp[next_i][next_j]) {
          warp[next_i][next_j] = next_warp;
          q.push({next_i, next_j});
        }
      }
    }
  }
  cout << ((warp[g_i][g_j] != 1e6 + 5) ? warp[g_i][g_j] : -1) << endl;

  return 0;
}
Problem p02579 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
#define maxn 1010
using namespace std;
char G[maxn][maxn];
int D[maxn][maxn];
const int Dx[4] = {-1, 0, 1, 0};
const int Dy[4] = {0, -1, 0, 1};
int main() {
  int n, m, sx, sy, dx, dy;
  scanf("%d%d", &n, &m);
  scanf("%d%d", &sx, &sy);
  scanf("%d%d", &dx, &dy);
  for (int i = 1; i <= n; ++i)
    scanf("%s", G[i] + 1);
  memset(D, 0x3f, sizeof D);
  auto f = [&](int a, int b) { return 1 <= a && a <= n && 1 <= b && b <= m; };
  priority_queue<tuple<int, int, int>> Q;
  Q.emplace(0, sx, sy);
  D[sx][sy] = 0;
  while (!Q.empty()) {
    int x, y;
    tie(ignore, x, y) = Q.top();
    Q.pop();
    for (int k = 0; k < 4; ++k) {
      int nx = x + Dx[k], ny = y + Dy[k];
      if (f(nx, ny) && G[nx][ny] == '.' && D[nx][ny] > D[x][y]) {
        D[nx][ny] = D[x][y];
        Q.emplace(-D[nx][ny], nx, ny);
      }
    }
    for (int i = -2; i <= 2; ++i) {
      for (int j = -2; j <= 2; ++j) {
        int nx = x + i, ny = y + j;
        if (f(nx, ny) && G[nx][ny] == '.' && D[nx][ny] > D[x][y] + 1) {
          D[nx][ny] = D[x][y] + 1;
          Q.emplace(-D[nx][ny], nx, ny);
        }
      }
    }
  }
  printf("%d", D[dx][dy] == 0x3f3f3f3f ? -1 : D[dx][dy]);
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define maxn 1010
using namespace std;
char G[maxn][maxn];
int D[maxn][maxn];
int qx[maxn * maxn * 10], qxh, qxt;
int qy[maxn * maxn * 10], qyh, qyt;
const int Dx[4] = {-1, 0, 1, 0};
const int Dy[4] = {0, -1, 0, 1};
int main() {
  int n, m, sx, sy, dx, dy;
  scanf("%d%d", &n, &m);
  scanf("%d%d", &sx, &sy);
  scanf("%d%d", &dx, &dy);
  for (int i = 1; i <= n; ++i)
    scanf("%s", G[i] + 1);
  memset(D, 0x3f, sizeof D);
  auto f = [&](int a, int b) { return 1 <= a && a <= n && 1 <= b && b <= m; };
  qx[qxt++] = sx;
  qy[qyt++] = sy;
  D[sx][sy] = 0;
  while (qxh < qxt) {
    int x = qx[qxh++], y = qy[qyh++];
    for (int k = 0; k < 4; ++k) {
      int nx = x + Dx[k], ny = y + Dy[k];
      if (f(nx, ny) && G[nx][ny] == '.' && D[nx][ny] > D[x][y]) {
        D[nx][ny] = D[x][y];
        qx[qxt++] = nx;
        qy[qyt++] = ny;
      }
    }
    for (int i = -2; i <= 2; ++i) {
      for (int j = -2; j <= 2; ++j) {
        int nx = x + i, ny = y + j;
        if (f(nx, ny) && G[nx][ny] == '.' && D[nx][ny] > D[x][y] + 1) {
          D[nx][ny] = D[x][y] + 1;
          qx[qxt++] = nx;
          qy[qyt++] = ny;
        }
      }
    }
  }
  printf("%d", D[dx][dy] == 0x3f3f3f3f ? -1 : D[dx][dy]);
  return 0;
}
Problem p02579 (C++) — buggy submission, verdict: Runtime Error

#include <iostream>
#include <queue>
using namespace std;

int h, w, g[1005][1005], ans[1005][1005], vis[1005][1005], ch, cw, dh, dw;
string s;

int main() {
  cin >> h >> w;
  cin >> ch >> cw >> dh >> dw;
  for (int i = 1; i <= h; i++) {
    cin >> s;
    for (int j = 1; j <= w; j++) {
      if (s[j - 1] == '.')
        g[i][j] = 1;
    }
  }
  priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>,
                 greater<pair<int, pair<int, int>>>>
      pq;
  pq.push({0, {ch, cw}});
  while (!pq.empty()) {
    int d = pq.top().first, x = pq.top().second.first,
        y = pq.top().second.second;
    pq.pop();
    if (vis[x][y])
      continue;
    ans[x][y] = d;
    vis[x][y] = 1;
    for (int i = max(1, x - 2); i <= min(h, x + 2); i++) {
      for (int j = max(1, y - 2); j <= min(w, y + 2); j++) {
        if (!g[i][j] || vis[i][j])
          continue;
        if (abs(i - x) + abs(j - y) == 1) {
          ans[i][j] = ans[x][y];
          pq.push({ans[i][j], {i, j}});
        } else {
          ans[i][j] = ans[x][y] + 1;
          pq.push({ans[i][j], {i, j}});
        }
      }
    }
  }
  if (!vis[dh][dw])
    cout << -1 << "\\n";
  else
    cout << ans[dh][dw] << "\\n";
}


Error: 0

Fixed submission:

#include <iostream>
#include <queue>
using namespace std;

int h, w, g[15][15], ans[15][15], vis[15][15], ch, cw, dh, dw;
string s;

int main() {
  cin >> h >> w;
  cin >> ch >> cw >> dh >> dw;
  for (int i = 1; i <= h; i++) {
    cin >> s;
    for (int j = 1; j <= w; j++) {
      if (s[j - 1] == '.')
        g[i][j] = 1;
    }
  }
  priority_queue<pair<int, pair<int, int>>, vector<pair<int, pair<int, int>>>,
                 greater<pair<int, pair<int, int>>>>
      pq;
  pq.push({0, {ch, cw}});
  while (!pq.empty()) {
    int d = pq.top().first, x = pq.top().second.first,
        y = pq.top().second.second;
    pq.pop();
    if (vis[x][y])
      continue;
    ans[x][y] = d;
    vis[x][y] = 1;
    for (int i = max(1, x - 2); i <= min(h, x + 2); i++) {
      for (int j = max(1, y - 2); j <= min(w, y + 2); j++) {
        if (!g[i][j] || vis[i][j])
          continue;
        if (abs(i - x) + abs(j - y) == 1) {
          ans[i][j] = ans[x][y];
          pq.push({ans[i][j], {i, j}});
        } else {
          ans[i][j] = ans[x][y] + 1;
          pq.push({ans[i][j], {i, j}});
        }
      }
    }
  }
  if (!vis[dh][dw])
    cout << -1 << "\\n";
  else
    cout << ans[dh][dw] << "\\n";
}
Problem p02580 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int h, w, m;
  cin >> h >> w >> m;
  // h = w = m = 3e5;
  vector<pair<int, int>> x(m);
  map<pair<int, int>, int> mp;
  vector<int> hor(h), ver(w);
  for (int i = 0; i < m; i++) {
    cin >> x[i].first >> x[i].second;
    x[i].first--;
    x[i].second--;
    mp[make_pair(x[i].first, x[i].second)]++;
    hor[x[i].first]++;
    ver[x[i].second]++;
  }

  int hormax = 0, vermax = 0;
  for (int i = 0; i < h; i++)
    hormax = max(hormax, hor[i]);
  for (int i = 0; i < w; i++)
    vermax = max(vermax, ver[i]);

  vector<int> schor, scver;
  for (int i = 0; i < h; i++) {
    if (hor[i] == hormax)
      schor.emplace_back(i);
  }
  for (int i = 0; i < w; i++) {
    if (ver[i] == vermax)
      scver.emplace_back(i);
  }

  bool ok = false;
  int ans = hormax + vermax;
  for (auto i : schor) {
    if (hor[i] != hormax)
      continue;
    for (auto j : scver) {
      if (ver[j] != vermax)
        continue;
      if (mp.find(make_pair(i, j)) == mp.end()) {
        ok = true;
        break;
      }
    }
    if (ok)
      break;
  }

  if (ok)
    cout << ans << '\\n';
  else
    cout << ans - 1 << '\\n';
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);

  int h, w, m;
  cin >> h >> w >> m;
  // h = w = m = 3e5;
  vector<pair<int, int>> x(m);
  map<pair<int, int>, int> mp;
  vector<int> hor(h), ver(w);
  for (int i = 0; i < m; i++) {
    cin >> x[i].first >> x[i].second;
    x[i].first--;
    x[i].second--;
    mp[make_pair(x[i].first, x[i].second)]++;
    hor[x[i].first]++;
    ver[x[i].second]++;
  }

  int hormax = 0, vermax = 0;
  for (int i = 0; i < h; i++)
    hormax = max(hormax, hor[i]);
  for (int i = 0; i < w; i++)
    vermax = max(vermax, ver[i]);

  bool ok = false;
  int ans = hormax + vermax;
  for (int i = 0; i < h; i++) {
    if (hor[i] != hormax)
      continue;
    for (int j = 0; j < w; j++) {
      if (ver[j] != vermax)
        continue;
      if (mp.find(make_pair(i, j)) == mp.end()) {
        ok = true;
        break;
      }
    }
    if (ok)
      break;
  }

  if (ok)
    cout << ans << '\\n';
  else
    cout << ans - 1 << '\\n';
}
Problem p02580 (C++) — buggy submission, verdict: Runtime Error

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

int main(void) {
  int H, W, M;
  cin >> H >> W >> M;
  // map<int,int> X, Y;
  // map< pair<int,int> ,int> mp;
  vector<vector<int>> X(H);
  vector<int> Y(W);
  for (int i = 0; i < M; i++) {
    int x, y;
    cin >> x >> y;
    --x;
    --y;
    X[x].emplace_back(y);
    Y[y]++;
    // X[x]++;
    // Y[y]++;
    // mp[make_pair(x, y)]++;
  }

  multiset<int> mst;
  for (auto e : Y) {
    mst.insert(e);
  }

  int ans = 0;
  for (int i = 0; i < H; i++) {
    for (auto y : X[i]) {
      ans = max(ans, (int)X[i].size() + Y[y] - 1);
    }
    for (auto y : X[i]) {
      mst.erase(mst.find(Y[y]));
    }
    if (mst.size())
      ans = max(ans, (int)X[i].size() + *mst.rbegin());
    for (auto y : X[i]) {
      mst.insert(Y[y]);
    }
  }

  cout << ans << endl;
  return 0;
}


Error: 0

Fixed submission:

#include <algorithm>
#include <cmath>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <set>
#include <string>
#include <tuple>
#include <vector>

using namespace std;

int main(void) {
  int H, W, M;
  cin >> H >> W >> M;
  // map<int,int> X, Y;
  // map< pair<int,int> ,int> mp;
  vector<vector<int>> X(H);
  vector<int> Y(W);
  for (int i = 0; i < M; i++) {
    int x, y;
    cin >> x >> y;
    --x;
    --y;
    X[x].emplace_back(y);
    Y[y]++;
    // X[x]++;
    // Y[y]++;
    // mp[make_pair(x, y)]++;
  }

  multiset<int> mst;
  for (auto e : Y) {
    mst.insert(e);
  }

  int ans = 0;
  for (int i = 0; i < H; i++) {
    for (auto y : X[i]) {
      ans = max(ans, (int)X[i].size() + Y[y] - 1);
    }
    for (auto y : X[i]) {
      mst.erase(mst.find(Y[y]));
    }
    ans = max(ans, (int)X[i].size() + *mst.rbegin());
    for (auto y : X[i]) {
      mst.insert(Y[y]);
    }
  }

  cout << ans << endl;
  return 0;
}
Problem p02580 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
  ll w, h, i, j, k, m;
  cin >> h >> w >> m;
  vector<ll> hh(h + 1), ww(w + 1);
  vector<pair<ll, ll>> q(m + 1);
  map<pair<ll, ll>, ll> p;
  for (i = 1; i <= m; i++) {
    ll x, y;
    cin >> x >> y;
    q[i].first = x;
    q[i].second = y;
    p[{x, y}] = 1;
    hh[x]++;
    ww[y]++;
  }
  ll ans = 0, kk = 0;
  ll m1 = 0, m2 = 0;
  for (i = 1; i <= h; i++) {
    if (hh[i] >= m1)
      m1 = hh[i];
  }
  for (i = 1; i <= w; i++) {
    if (ww[i] >= m2)
      m2 = ww[i];
  }
  ll xx = 0, yy = 0;
  vector<ll> u, v;
  for (i = 1; i <= w; i++) {
    if (ww[i] == m2)
      v.push_back(i);
  }
  for (i = 1; i <= h; i++) {
    if (hh[i] == m1)
      u.push_back(i);
  }
  for (i = 0; i < u.size(); i++) {
    for (j = 0; j < v.size(); j++) {
      if (p[{u[i], v[j]}] != 1) {
        cout << m1 + m2 << endl;
        return 0;
      }
    }
  }
  cout << m1 + m2 - 1 << endl;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int main() {
  ll w, h, i, j, k, m;
  cin >> h >> w >> m;
  vector<ll> hh(h + 1), ww(w + 1);
  map<pair<ll, ll>, ll> p;
  for (i = 0; i < m; i++) {
    ll x, y;
    cin >> x >> y;
    p[{x, y}] = 1;
    hh[x]++;
    ww[y]++;
  }
  ll ans = 0;
  for (i = 1; i <= h; i++) {
    for (j = 1; j <= w; j++) {
      ll x = i, y = j, a = 0;
      a = hh[i] + ww[j];
      if (p[{x, y}] == 1)
        a--;
      ans = max(ans, a);
    }
  }
  cout << ans << endl;
}
Problem p02580 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
#include <map>
#include <stdio.h>
#include <vector>
using namespace std;
const int N = 3e5 + 10;
vector<int> row[N], col[N];
bool cmp1(int &l, int &r) { return row[l].size() > row[r].size(); }
bool cmp2(int &l, int &r) { return col[l].size() > col[r].size(); }
typedef struct boom {
  int x, y;
} boom;
boom b[N];
map<pair<int, int>, int> mp;
int rs[N], cs[N];
int main() {
  int n, m, k;
  scanf("%d%d%d", &n, &m, &k);
  int maxrow, maxcol;
  maxrow = maxcol = 0;
  for (int i = 1; i <= k; i++) {
    scanf("%d%d", &b[i].x, &b[i].y);
    mp.insert(make_pair(make_pair(b[i].x, b[i].y), 1));
    row[b[i].x].push_back(i);
    col[b[i].y].push_back(i);
    rs[b[i].x] = b[i].x;
    cs[b[i].y] = b[i].y;
  }
  sort(rs + 1, rs + n + 1, cmp1);
  sort(cs + 1, cs + m + 1, cmp2);
  int ans = row[rs[1]].size() + col[cs[1]].size();
  int flag = 0;
  for (int i = 1; i <= n;) {
    for (int j = 1; j <= m;) {
      pair<int, int> tmp = make_pair(rs[i], cs[j]);
      if (!mp.count(tmp)) {
        flag = 1;
        break;
      }
      j++;
      if (col[cs[j]].size() < col[cs[j - 1]].size())
        break;
    }
    if (flag)
      break;
    i++;
    if (row[rs[i]].size() < row[rs[i - 1]].size())
      break;
  }

  printf("%d\\n", flag == 1 ? ans : ans - 1);
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <stdio.h>
#include <vector>
using namespace std;
const int N = 3e5 + 10;
vector<int> row[N], col[N];
bool cmp1(int &l, int &r) { return row[l].size() > row[r].size(); }
bool cmp2(int &l, int &r) { return col[l].size() > col[r].size(); }
typedef struct boom {
  int x, y;
} boom;
boom b[N];
int rs[N], cs[N];
int main() {
  int n, m, k;
  scanf("%d%d%d", &n, &m, &k);
  int maxrow, maxcol;
  maxrow = maxcol = 0;
  for (int i = 1; i <= k; i++) {
    scanf("%d%d", &b[i].x, &b[i].y);
    row[b[i].x].push_back(i);
    col[b[i].y].push_back(i);
    rs[b[i].x] = b[i].x;
    cs[b[i].y] = b[i].y;
  }
  sort(rs + 1, rs + n + 1, cmp1);
  sort(cs + 1, cs + m + 1, cmp2);
  int ans = 0, flag;
  for (int i = 1; i <= n;) {
    for (int j = 1; j <= m;) {
      flag = 0;
      int tmp = row[rs[i]].size() + col[cs[j]].size();
      for (auto &val : col[cs[j]]) {
        int idx = lower_bound(row[rs[i]].begin(), row[rs[i]].end(), val) -
                  row[rs[i]].begin();
        if (idx < row[rs[i]].size() && row[rs[i]][idx] == val) {
          tmp--;
          flag = 1;
          break;
        }
      }
      ans = max(ans, tmp);
      if (!flag)
        break;
      j++;
      if (col[cs[j]].size() < col[cs[j - 1]].size())
        break;
    }
    if (!flag)
      break;
    i++;
    if (row[rs[i]].size() < row[rs[i - 1]].size())
      break;
  }
  printf("%d\\n", ans);
  return 0;
}