Problem p02580 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#define xx first
#define yy second

#define MAX_N 300000

using namespace std;

const int sqrInf = (int)(1e9) - 1;

int n, m; /*
 int a[1009][1009];

 string s;

 const int dl[] = {-1, 1, 0, 0};
 const int dc[] = {0, 0, 1, -1};

 int sl, sc;
 int xl, xc;

 priority_queue <pair<pair<int, int>, int>, vector<pair<pair<int, int>, int>>,
 greater<pair<pair<int, int>, int>> > q;

 int dp[1009][1009];
 int viz[1009][1009];

 void solve()
 {
     dp[sl][sc] = 0;
     q.push({{0, sl}, sc});

     while(q.size() > 0)
     {
         auto cr = q.top();
         q.pop();

         int l = cr.xx.yy;
         int c = cr.yy;

         if(viz[l][c] == 0)
         {
             viz[l][c] = 1;

             for(int d = 0; d < 4; d ++)
             {
                 int nl = l + dl[d];
                 int nc = c + dc[d];

                 if(nl >= 1 && nl <= n && nc >= 1 && nc <= m)
                 {
                     if(a[nl][nc] == 0)
                     if(dp[l][c] < dp[nl][nc])
                     {
                         dp[nl][nc] = dp[l][c];
                         q.push({{dp[nl][nc], nl},nc });
                     }
                 }
             }

             for(int nl = l - 2; nl <= l + 2; nl ++)
             {
                 for(int nc = c - 2; nc <= c + 2; nc ++)
                 {
                     if(nl >= 1 && nl <= n && nc >= 1 && nc <= m)
                     {
                         if(a[nl][nc] == 0)
                         if(dp[l][c] + 1 < dp[nl][nc])
                         {
                             dp[nl][nc] = dp[l][c] + 1;
                             q.push({{dp[nl][nc], nl},nc });
                         }
                     }
                 }
             }
         }
     }
 }
 */
int lin[MAX_N + 1];
int col[MAX_N + 1];

unordered_map<int, int> ap[MAX_N + 1];

int q;
pair<int, int> b[MAX_N + 1];

int dl;
int xl[MAX_N + 1];

int dc;
int xc[MAX_N + 1];

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

  cin >> n >> m >> q;
  for (int i = 1; i <= q; i++) {
    int l, c;
    cin >> l >> c;
    lin[l]++;
    col[c]++;
    ap[l][c] = 1;

    b[i] = {l, c};
  }

  int rez = 0;
  int ml = 0, mc = 0;
  for (int i = 1; i <= q; i++) {
    ml = max(ml, lin[b[i].xx]);
    mc = max(mc, col[b[i].yy]);
  }

  int cnt = 0;
  for (int i = 1; i <= q; i++) {
    rez = max(rez, lin[b[i].xx] + col[b[i].yy] - 1);

    if (lin[b[i].xx] + col[b[i].yy] == ml + mc)
      cnt++;
  }

  for (int i = 1; i <= n; i++) {
    if (lin[i] == ml)
      xl[++dl] = i;
  }

  for (int j = 1; j <= m; j++) {
    if (col[j] == mc)
      xc[++dc] = j;
  }

  rez = ml + mc;

  if (cnt == dl * dc)
    rez--;

  cout << rez << "\\n";

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define xx first
#define yy second

#define MAX_N 300000

using namespace std;

const int sqrInf = (int)(1e9) - 1;

int n, m; /*
 int a[1009][1009];

 string s;

 const int dl[] = {-1, 1, 0, 0};
 const int dc[] = {0, 0, 1, -1};

 int sl, sc;
 int xl, xc;

 priority_queue <pair<pair<int, int>, int>, vector<pair<pair<int, int>, int>>,
 greater<pair<pair<int, int>, int>> > q;

 int dp[1009][1009];
 int viz[1009][1009];

 void solve()
 {
     dp[sl][sc] = 0;
     q.push({{0, sl}, sc});

     while(q.size() > 0)
     {
         auto cr = q.top();
         q.pop();

         int l = cr.xx.yy;
         int c = cr.yy;

         if(viz[l][c] == 0)
         {
             viz[l][c] = 1;

             for(int d = 0; d < 4; d ++)
             {
                 int nl = l + dl[d];
                 int nc = c + dc[d];

                 if(nl >= 1 && nl <= n && nc >= 1 && nc <= m)
                 {
                     if(a[nl][nc] == 0)
                     if(dp[l][c] < dp[nl][nc])
                     {
                         dp[nl][nc] = dp[l][c];
                         q.push({{dp[nl][nc], nl},nc });
                     }
                 }
             }

             for(int nl = l - 2; nl <= l + 2; nl ++)
             {
                 for(int nc = c - 2; nc <= c + 2; nc ++)
                 {
                     if(nl >= 1 && nl <= n && nc >= 1 && nc <= m)
                     {
                         if(a[nl][nc] == 0)
                         if(dp[l][c] + 1 < dp[nl][nc])
                         {
                             dp[nl][nc] = dp[l][c] + 1;
                             q.push({{dp[nl][nc], nl},nc });
                         }
                     }
                 }
             }
         }
     }
 }
 */
int lin[MAX_N + 1];
int col[MAX_N + 1];

unordered_map<int, int> ap[MAX_N + 1];

int q;
pair<int, int> b[MAX_N + 1];

int dl;
int xl[MAX_N + 1];

int dc;
int xc[MAX_N + 1];

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

  cin >> n >> m >> q;
  for (int i = 1; i <= q; i++) {
    int l, c;
    cin >> l >> c;
    lin[l]++;
    col[c]++;
    ap[l][c] = 1;

    b[i] = {l, c};
  }

  int rez = 0;
  int ml = 0, mc = 0;
  for (int i = 1; i <= q; i++) {
    rez = max(rez, lin[b[i].xx] + col[b[i].yy] - 1);
    ml = max(ml, lin[b[i].xx]);
    mc = max(mc, col[b[i].yy]);
  }

  for (int i = 1; i <= n; i++) {
    if (lin[i] == ml)
      xl[++dl] = i;
  }

  for (int j = 1; j <= m; j++) {
    if (col[j] == mc)
      xc[++dc] = j;
  }

  for (int i = 1; i <= dl; i++) {
    for (int j = 1; j <= dc; j++) {
      int l = xl[i];
      int c = xc[j];
      if (ap[l][c] == 0)
        rez = max(rez, lin[l] + col[c]);
    }
  }

  cout << rez << "\\n";

  return 0;
}