Problem p02580 (C++) — buggy submission, verdict: Runtime Error

#pragma GCC optimize(3, "Ofast", "inline")
#include <bits/stdc++.h>
#define ios std::ios::sync_with_stdio(false)
#define rep(i, a, n) for (int i = a; i <= n; i++)
#define per(i, n, a) for (int i = n; i >= a; i--)
#define repp(i, a, b) for (int i = a; i < b; i++)
#define mm(a, n) memset(a, n, sizeof(a))
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define il inline
#define int long long
#define ll long long
#define ull unsigned long long
#define MOD 1000000007
#define pi 3.14159265358979323
#define SZ(x) ((ll)(x).size())
#define debug(x) cout << #x << ": " << x << endl;
#define debug2(x, y)                                                           \\
  cout << #x << ": " << x << " | " << #y << ": " << y << endl;
#define debug3(x, y, z)                                                        \\
  cout << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": "   \\
       << z << endl;
#define debug4(a, b, c, d)                                                     \\
  cout << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \\
       << c << " | " << #d << ": " << d << endl;
using namespace std;
const ll INF(0x3f3f3f3f3f3f3f3fll);
const int inf(0x3f3f3f3f);
template <typename T> void read(T &res) {
  bool flag = false;
  char ch;
  while (!isdigit(ch = getchar()))
    (ch == '-') && (flag = true);
  for (res = ch - 48; isdigit(ch = getchar());
       res = (res << 1) + (res << 3) + ch - 48)
    ;
  flag && (res = -res);
}
template <typename T> void Out(T x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x > 9)
    Out(x / 10);
  putchar(x % 10 + '0');
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
ll pow_mod(ll x, ll n, ll mod) {
  ll res = 1;
  while (n) {
    if (n & 1)
      res = res * x % mod;
    x = x * x % mod;
    n >>= 1;
  }
  return res;
}
ll fact_pow(ll n, ll p) {
  ll res = 0;
  while (n) {
    n /= p;
    res += n;
  }
  return res;
}
ll mult(ll a, ll b, ll p) {
  a %= p;
  b %= p;
  ll r = 0, v = a;
  while (b) {
    if (b & 1) {
      r += v;
      if (r > p)
        r -= p;
    }
    v <<= 1;
    if (v > p)
      v -= p;
    b >>= 1;
  }
  return r;
}
ll quick_pow(ll a, ll b, ll p) {
  ll r = 1, v = a % p;
  while (b) {
    if (b & 1)
      r = mult(r, v, p);
    v = mult(v, v, p);
    b >>= 1;
  }
  return r;
}
bool CH(ll a, ll n, ll x, ll t) {
  ll r = quick_pow(a, x, n);
  ll z = r;
  for (ll i = 1; i <= t; i++) {
    r = mult(r, r, n);
    if (r == 1 && z != 1 && z != n - 1)
      return true;
    z = r;
  }
  return r != 1;
}
bool Miller_Rabin(ll n) {
  if (n < 2)
    return false;
  if (n == 2)
    return true;
  if (!(n & 1))
    return false;
  ll x = n - 1, t = 0;
  while (!(x & 1)) {
    x >>= 1;
    t++;
  }
  srand(time(NULL));
  ll o = 8;
  for (ll i = 0; i < o; i++) {
    ll a = rand() % (n - 1) + 1;
    if (CH(a, n, x, t))
      return false;
  }
  return true;
}
ll exgcd1(ll a, ll b, ll &x, ll &y) {
  if (!b) {
    x = 1, y = 0;
    return a;
  }
  ll t = exgcd1(b, a % b, y, x);
  y -= a / b * x;
  return t;
}
ll get_inv(ll a, ll mod) {
  ll x, y;
  ll d = exgcd1(a, mod, x, y);
  return d == 1 ? (x % mod + mod) % mod : -1;
}
void exgcd(ll a, ll b, ll &x, ll &y) {
  if (!b) {
    x = 1, y = 0;
    return;
  }
  exgcd(b, a % b, x, y);
  ll t = x;
  x = y, y = t - (a / b) * y;
}
ll INV(ll a, ll b) {
  ll x, y;
  return exgcd(a, b, x, y), (x % b + b) % b;
}
ll crt(ll x, ll p, ll mod) { return INV(p / mod, mod) * (p / mod) * x; }
ll FAC(ll x, ll a, ll b) {
  if (!x)
    return 1;
  ll ans = 1;
  for (ll i = 1; i <= b; i++)
    if (i % a)
      ans *= i, ans %= b;
  ans = pow_mod(ans, x / b, b);
  for (ll i = 1; i <= x % b; i++)
    if (i % a)
      ans *= i, ans %= b;
  return ans * FAC(x / a, a, b) % b;
}
ll C(ll n, ll m, ll a, ll b) {
  ll N = FAC(n, a, b), M = FAC(m, a, b), Z = FAC(n - m, a, b), sum = 0, i;
  for (i = n; i; i = i / a)
    sum += i / a;
  for (i = m; i; i = i / a)
    sum -= i / a;
  for (i = n - m; i; i = i / a)
    sum -= i / a;
  return N * pow_mod(a, sum, b) % b * INV(M, b) % b * INV(Z, b) % b;
}
ll exlucas(ll n, ll m, ll p) {
  ll t = p, ans = 0, i;
  for (i = 2; i * i <= p; i++) {
    ll k = 1;
    while (t % i == 0) {
      k *= i, t /= i;
    }
    ans += crt(C(n, m, i, k), p, k), ans %= p;
  }
  if (t > 1)
    ans += crt(C(n, m, t, t), p, t), ans %= p;
  return ans % p;
}
const int N = 3e5 + 10;
const int Max = 1e6 + 10;
struct Dinic {
  struct Edge {
    int to, nex, flow; // flow?????a¡§???¨¤?|¨¬?¨¤???|¨¬??¨¤?2D¡§¡é?
  } edge[Max << 1];
  int n, m, s, t;
  int head[Max], TOT;
  int dis[Max], cur[Max];
  void init(int nn, int ss, int tt) {
    this->n = nn, this->s = ss, this->t = tt;
    TOT = 1;
    for (int i = 0; i <= nn + 1; i++)
      head[i] = 0;
  }
  void add_edge(int u, int v, int flow) {
    edge[++TOT].nex = head[u];
    edge[TOT].to = v;
    edge[TOT].flow = flow;
    head[u] = TOT;
    edge[++TOT].nex = head[v];
    edge[TOT].to = u;
    edge[TOT].flow = 0;
    head[v] = TOT;
  }
  bool bfs() //?D??¡§a?¡§o??¡è?¡§¡é?¡§a?¡ì
  {
    queue<int> que;
    memset(dis, -1, (n + 1) * sizeof(int));
    dis[s] = 0;
    que.push(s);
    while (!que.empty()) {
      int u = que.front();
      que.pop();
      for (int i = head[u]; i; i = edge[i].nex) {
        int v = edge[i].to;
        if (dis[v] == -1 &&
            edge[i].flow > 0) //?¡§|¡§¡ã??¡§¡§?¡§2?¨¤?i|¨¬????D?|¨¬??¡§¡é|¨¬?
        {
          dis[v] = dis[u] + 1; //?¡§??¡ê¡è|¨¬?|¨¬????|¨¬?|¨¬??¡§¡è¡§¡è??¨¤¡§¡èo?
          que.push(v);
        }
      }
    }
    return dis[t] != -1; // ¡§¡§?¡è¡§¡§?¡§o??¡è?¡§¡é?¡§a?¡ì
  }
  int dfs(int u, int flow_in) {
    if (u == t)
      return flow_in;
    int flow_out = 0; //?????a¡§¡ã?|¨¬?¡§o|¨¬?¡§o¡§¡é??3?|¨¬?¡§¡é??¡§¡é?
    for (int i = cur[u]; i; i = edge[i].nex) {
      cur[u] =
          i; // ¡§?¡§|¡§?¡§2?¡§¡ã????1?|¨¬?¡§o?¨¤o¡§¡ã??¡§o???¡§¡é?¡§?????a¡§???¨¤?|¨¬?¡§¡§Y¡§¡é??a???¡§?¡§¡é|¨¬??¨º?
      // ??????¡§2?¡§2¡§????¡§¡é??¡§¡§?¡§¡é??¡§¡é???????¨º??¡§¡ã??¡§o1¡§??|¨¬??¨¤??¨º???¡§¡ã??-?¡§2¡§¡é??¡§¡é?
      //??¡§?D?¡§?D???1?|¨¬??¡§|?¡§1¡§¡é??¨º??¡§¡ä¡§¡ã???¨¢?¡§2¡§¡é??aD??????|¨¬??¨¤??¨º???¡§?????¡§2¡§¡é??|¨¬??¨¤??a¡§o??¨¤¡§|¡§¡è¡§2
      int v = edge[i].to;
      if (dis[v] == dis[u] + 1 && edge[i].flow > 0) {
        int flow_part = dfs(v, min(flow_in, edge[i].flow));
        if (!flow_part)
          continue; //?T?¡è?¡ìD?3¡§|??1???¡è
        flow_in -=
            flow_part; // ¡§¡é??3?¡§¡é?¡§¡ã?2??¡è??¨º?¡§o?¨º¡§?¡§¡è?¡§|?¡è???¡§¡é??¡§¡§??¡§a??¡§|¡§¡ä¡§¡é?
        flow_out += flow_part; //?????a¡§¡ã?|¨¬??¨¢???¡§?|¨¬?¡§¡é??3?
        edge[i].flow -= flow_part;
        edge[i ^ 1].flow +=
            flow_part; //??¡§|¡§¡ä??1???¡è¡§|??¨¤?|¨¬?¡§¡§Y¡§¡é??¨º????¡§????¡è???¡§¡ã?¨¤?|¨¬?¡§¡§Y¡§¡é?
        if (!flow_in)
          break;
      }
    }
    return flow_out;
  }
  int dinic() {
    int maxflow = 0;
    while (bfs()) {
      for (int i = 0; i <= n; i++)
        cur[i] = head[i];
      maxflow += dfs(s, INF);
    }
    return maxflow;
  }
} DI;
map<int, int> cntx, cnty;
map<pair<int, int>, int> mp;
signed main() {
  int n, m, t;
  cin >> n >> m >> t;
  rep(i, 1, t) {
    int x, y;
    cin >> x >> y;
    mp[make_pair(x, y)]++;
    cntx[x]++, cnty[y]++;
  }
  vector<int> vec, vec1;
  int ans1 = 0, ans2 = 0, ma1 = 0, ma2 = 0;
  for (auto i : cntx) {
    if (ans1 < i.se) {
      ans1 = i.se;
      ma1 = i.fi;
      vec.clear();
      vec.pb(ma1);
    } else if (ans1 == i.se)
      vec.pb(i.fi);
  }
  for (auto i : cnty) {
    if (ans2 < i.se) {
      ans2 = i.se;
      ma2 = i.fi;
      vec1.clear();
      vec1.pb(ma2);
    } else if (ans2 == i.se)
      vec1.pb(i.fi);
  }
  int ans = 0;
  for (auto j : vec)
    for (auto i : vec1) {
      int add = 0;
      pair<int, int> now = make_pair(j, i);
      if (!mp.count(now)) {
        ans = ans1 + ans2;
        break;
      } else
        add = 1;
      ans = max(ans1 + ans2 - add, ans);
    }
  cout << ans << '\\n';
  return 0;
}


Error: -11

Fixed submission:

#pragma GCC optimize(3, "Ofast", "inline")
#include <bits/stdc++.h>
#define ios std::ios::sync_with_stdio(false)
#define rep(i, a, n) for (int i = a; i <= n; i++)
#define per(i, n, a) for (int i = n; i >= a; i--)
#define repp(i, a, b) for (int i = a; i < b; i++)
#define mm(a, n) memset(a, n, sizeof(a))
#define pb push_back
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define il inline
#define int long long
#define ll long long
#define ull unsigned long long
#define MOD 1000000007
#define pi 3.14159265358979323
#define SZ(x) ((ll)(x).size())
#define debug(x) cout << #x << ": " << x << endl;
#define debug2(x, y)                                                           \\
  cout << #x << ": " << x << " | " << #y << ": " << y << endl;
#define debug3(x, y, z)                                                        \\
  cout << #x << ": " << x << " | " << #y << ": " << y << " | " << #z << ": "   \\
       << z << endl;
#define debug4(a, b, c, d)                                                     \\
  cout << #a << ": " << a << " | " << #b << ": " << b << " | " << #c << ": "   \\
       << c << " | " << #d << ": " << d << endl;
using namespace std;
const ll INF(0x3f3f3f3f3f3f3f3fll);
const int inf(0x3f3f3f3f);
template <typename T> void read(T &res) {
  bool flag = false;
  char ch;
  while (!isdigit(ch = getchar()))
    (ch == '-') && (flag = true);
  for (res = ch - 48; isdigit(ch = getchar());
       res = (res << 1) + (res << 3) + ch - 48)
    ;
  flag && (res = -res);
}
template <typename T> void Out(T x) {
  if (x < 0)
    putchar('-'), x = -x;
  if (x > 9)
    Out(x / 10);
  putchar(x % 10 + '0');
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
ll lcm(ll a, ll b) { return a * b / gcd(a, b); }
ll pow_mod(ll x, ll n, ll mod) {
  ll res = 1;
  while (n) {
    if (n & 1)
      res = res * x % mod;
    x = x * x % mod;
    n >>= 1;
  }
  return res;
}
ll fact_pow(ll n, ll p) {
  ll res = 0;
  while (n) {
    n /= p;
    res += n;
  }
  return res;
}
ll mult(ll a, ll b, ll p) {
  a %= p;
  b %= p;
  ll r = 0, v = a;
  while (b) {
    if (b & 1) {
      r += v;
      if (r > p)
        r -= p;
    }
    v <<= 1;
    if (v > p)
      v -= p;
    b >>= 1;
  }
  return r;
}
ll quick_pow(ll a, ll b, ll p) {
  ll r = 1, v = a % p;
  while (b) {
    if (b & 1)
      r = mult(r, v, p);
    v = mult(v, v, p);
    b >>= 1;
  }
  return r;
}
bool CH(ll a, ll n, ll x, ll t) {
  ll r = quick_pow(a, x, n);
  ll z = r;
  for (ll i = 1; i <= t; i++) {
    r = mult(r, r, n);
    if (r == 1 && z != 1 && z != n - 1)
      return true;
    z = r;
  }
  return r != 1;
}
bool Miller_Rabin(ll n) {
  if (n < 2)
    return false;
  if (n == 2)
    return true;
  if (!(n & 1))
    return false;
  ll x = n - 1, t = 0;
  while (!(x & 1)) {
    x >>= 1;
    t++;
  }
  srand(time(NULL));
  ll o = 8;
  for (ll i = 0; i < o; i++) {
    ll a = rand() % (n - 1) + 1;
    if (CH(a, n, x, t))
      return false;
  }
  return true;
}
ll exgcd1(ll a, ll b, ll &x, ll &y) {
  if (!b) {
    x = 1, y = 0;
    return a;
  }
  ll t = exgcd1(b, a % b, y, x);
  y -= a / b * x;
  return t;
}
ll get_inv(ll a, ll mod) {
  ll x, y;
  ll d = exgcd1(a, mod, x, y);
  return d == 1 ? (x % mod + mod) % mod : -1;
}
void exgcd(ll a, ll b, ll &x, ll &y) {
  if (!b) {
    x = 1, y = 0;
    return;
  }
  exgcd(b, a % b, x, y);
  ll t = x;
  x = y, y = t - (a / b) * y;
}
ll INV(ll a, ll b) {
  ll x, y;
  return exgcd(a, b, x, y), (x % b + b) % b;
}
ll crt(ll x, ll p, ll mod) { return INV(p / mod, mod) * (p / mod) * x; }
ll FAC(ll x, ll a, ll b) {
  if (!x)
    return 1;
  ll ans = 1;
  for (ll i = 1; i <= b; i++)
    if (i % a)
      ans *= i, ans %= b;
  ans = pow_mod(ans, x / b, b);
  for (ll i = 1; i <= x % b; i++)
    if (i % a)
      ans *= i, ans %= b;
  return ans * FAC(x / a, a, b) % b;
}
ll C(ll n, ll m, ll a, ll b) {
  ll N = FAC(n, a, b), M = FAC(m, a, b), Z = FAC(n - m, a, b), sum = 0, i;
  for (i = n; i; i = i / a)
    sum += i / a;
  for (i = m; i; i = i / a)
    sum -= i / a;
  for (i = n - m; i; i = i / a)
    sum -= i / a;
  return N * pow_mod(a, sum, b) % b * INV(M, b) % b * INV(Z, b) % b;
}
ll exlucas(ll n, ll m, ll p) {
  ll t = p, ans = 0, i;
  for (i = 2; i * i <= p; i++) {
    ll k = 1;
    while (t % i == 0) {
      k *= i, t /= i;
    }
    ans += crt(C(n, m, i, k), p, k), ans %= p;
  }
  if (t > 1)
    ans += crt(C(n, m, t, t), p, t), ans %= p;
  return ans % p;
}
const int N = 3e5 + 10;
const int Max = 1e6 + 10;
struct Dinic {
  struct Edge {
    int to, nex, flow; // flow?????a¨¬?¡À?¦Ì¡À?¡ã¦Ì?¡À?2D¨¢?
  } edge[Max << 1];
  int n, m, s, t;
  int head[Max], TOT;
  int dis[Max], cur[Max];
  void init(int nn, int ss, int tt) {
    this->n = nn, this->s = ss, this->t = tt;
    TOT = 1;
    for (int i = 0; i <= nn + 1; i++)
      head[i] = 0;
  }
  void add_edge(int u, int v, int flow) {
    edge[++TOT].nex = head[u];
    edge[TOT].to = v;
    edge[TOT].flow = flow;
    head[u] = TOT;
    edge[++TOT].nex = head[v];
    edge[TOT].to = u;
    edge[TOT].flow = 0;
    head[v] = TOT;
  }
  bool bfs() //?D??¨ª?¨º?¡¤?¨¢?¨ª¡§
  {
    queue<int> que;
    memset(dis, -1, (n + 1) * sizeof(int));
    dis[s] = 0;
    que.push(s);
    while (!que.empty()) {
      int u = que.front();
      que.pop();
      for (int i = head[u]; i; i = edge[i].nex) {
        int v = edge[i].to;
        if (dis[v] == -1 &&
            edge[i].flow > 0) //?¨¦¨°??¨¨?¨²¡À?i¦Ì?¡ä?D?¦Ì??¨¢¦Ì?
        {
          dis[v] = dis[u] + 1; //?¨®?£¤¦Ì?¦Ì??¡ä¦Ì?¦Ì??¨¤¨¤?¡À¨¤o?
          que.push(v);
        }
      }
    }
    return dis[t] != -1; // ¨¨¡¤¨¨?¨º?¡¤?¨¢?¨ª¡§
  }
  int dfs(int u, int flow_in) {
    if (u == t)
      return flow_in;
    int flow_out = 0; //?????a¨°?¦Ì?¨º¦Ì?¨º¨¢¡Â3?¦Ì?¨¢¡Â¨¢?
    for (int i = cur[u]; i; i = edge[i].nex) {
      cur[u] =
          i; // ¨®¨¦¨®¨²?¨°????1?¦Ì?¨º¡Ào¨°??¨º???¨¢?¨®????a¨¬?¡À?¦Ì?¨¨Y¨¢??a?¡ã¨¬¨¢¦Ì?¡ê?
      // ???¡ä?¨²?¨²¨®???¨¢¡Â¨¨?¨¢¡Â¨¢????¡ã¡ê??¨°??¨º1¨®?¦Ì?¡À?¡ê???¨°??-?¨²¨¢¡Â¨¢?
      //??¨®D?¨¬D???1?¦Ì??¨¦?¨¹¨¢?¡ê??¨´¨°??¡Á?¨²¨¢??aD????¡ä¦Ì?¡À?¡ê?¡ä¨®?¡ä?¨²¨¢¡Â¦Ì?¡À??a¨º?¡À¨¦¨¤¨²
      int v = edge[i].to;
      if (dis[v] == dis[u] + 1 && edge[i].flow > 0) {
        int flow_part = dfs(v, min(flow_in, edge[i].flow));
        if (!flow_part)
          continue; //?T¡¤¡§D?3¨¦??1??¡¤
        flow_in -=
            flow_part; // ¨¢¡Â3?¨¢?¨°?2?¡¤?¡ê?¨º¡ê¨®¨¤?¨¦¡¤???¨¢¡Â¨¨??¨ª??¨¦¨´¨¢?
        flow_out += flow_part; //?????a¨°?¦Ì?¡Á?¡ä¨®¦Ì?¨¢¡Â3?
        edge[i].flow -= flow_part;
        edge[i ^ 1].flow +=
            flow_part; //??¨¦¨´??1??¡¤¨¦?¡À?¦Ì?¨¨Y¨¢?¡ê????¨®??¡¤¡ä?¨°¡À?¦Ì?¨¨Y¨¢?
        if (!flow_in)
          break;
      }
    }
    return flow_out;
  }
  int dinic() {
    int maxflow = 0;
    while (bfs()) {
      for (int i = 0; i <= n; i++)
        cur[i] = head[i];
      maxflow += dfs(s, INF);
    }
    return maxflow;
  }
} DI;
map<int, int> cntx, cnty;
map<pair<int, int>, int> mp;
signed main() {
  int n, m, t;
  cin >> n >> m >> t;
  rep(i, 1, t) {
    int x, y;
    cin >> x >> y;
    mp[make_pair(x, y)]++;
    cntx[x]++, cnty[y]++;
  }
  vector<int> vec, vec1;
  int ans1 = 0, ans2 = 0, ma1 = 0, ma2 = 0;
  for (auto i : cntx) {
    if (ans1 < i.se) {
      ans1 = i.se;
      ma1 = i.fi;
      vec.clear();
      vec.pb(ma1);
    } else if (ans1 == i.se)
      vec.pb(i.fi);
  }
  for (auto i : cnty) {
    if (ans2 < i.se) {
      ans2 = i.se;
      ma2 = i.fi;
      vec1.clear();
      vec1.pb(ma2);
    } else if (ans2 == i.se)
      vec1.pb(i.fi);
  }
  int ans = 0;
  vector<int> vec2;
  for (int i = 0; i < vec.size() && i <= 100; i++)
    vec2.pb(vec[i]);
  int len = vec.size();
  for (int i = len - 1; i >= 0 && i >= len - 100; i++)
    vec2.pb(vec[i]);
  for (auto j : vec2)
    for (auto i : vec1) {
      int add = 0;
      pair<int, int> now = make_pair(j, i);
      if (mp.count(now))
        add = 1;
      ans = max(ans1 + ans2 - add, ans);
    }
  cout << ans << '\\n';
  return 0;
}