Problem p03968 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define LL long long
using namespace std;
#define G(x, y, z, t) ((x)*1LL * 1000000000 + (y)*1000000 + (z)*1000 + (t))
map<LL, int> number;
struct sqare {
  int x0, x1, x2, x3;
  LL key[5];
  void ini(int a, int b, int c, int d) {
    x0 = a, x1 = b, x2 = c, x3 = d;
    key[1] = G(a, b, c, d);
    key[2] = G(b, c, d, a);
    key[3] = G(c, d, a, b);
    key[4] = G(d, a, b, c);
  }
  void add() {
    for (int i = 1; i <= 4; i++)
      number[key[i]]++;
  }
  void dec() {
    for (int i = 1; i <= 4; i++)
      number[key[i]]--;
  }
} p[505];
LL calc(sqare fr, sqare bk) {
  fr.dec();
  bk.dec();
  LL ret = 0;
  for (int i = 1; i <= 4; i++) {
    int t = bk.x0;
    bk.x0 = bk.x1;
    bk.x1 = bk.x2;
    bk.x2 = bk.x3;
    bk.x3 = t;
    LL ans = 1;
    sqare key[5];
    key[1].ini(fr.x1, fr.x0, bk.x1, bk.x0);
    ans *= number[G(fr.x1, fr.x0, bk.x1, bk.x0)];
    key[1].dec();
    key[2].ini(fr.x2, fr.x1, bk.x0, bk.x3);
    ans *= number[G(fr.x2, fr.x1, bk.x0, bk.x3)];
    key[2].dec();
    key[3].ini(fr.x3, fr.x2, bk.x3, bk.x2);
    ans *= number[G(fr.x3, fr.x2, bk.x3, bk.x2)];
    key[3].dec();
    key[4].ini(fr.x0, fr.x3, bk.x2, bk.x1);
    ans *= number[G(fr.x0, fr.x3, bk.x2, bk.x1)];
    key[4].dec();
    ret += ans;
    for (int j = 1; j <= 4; j++)
      key[j].add();
  }
  fr.add();
  bk.add();
  return ret;
}
int n;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    int a, b, c, d;
    scanf("%d%d%d%d", &a, &b, &c, &d);
    p[i].ini(a, b, c, d);
    p[i].add();
  }
  LL ans = 0;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      if (i != j)
        ans += calc(p[i], p[j]);
  cout << ans / 6 << endl;
  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define LL long long
using namespace std;
#define G(x, y, z, t) ((x)*1LL * 1000000000 + (y)*1000000 + (z)*1000 + (t))
unordered_map<LL, int> number;
struct sqare {
  int x0, x1, x2, x3;
  LL key[5];
  void ini(int a, int b, int c, int d) {
    x0 = a, x1 = b, x2 = c, x3 = d;
    key[1] = G(a, b, c, d);
    key[2] = G(b, c, d, a);
    key[3] = G(c, d, a, b);
    key[4] = G(d, a, b, c);
  }
  void add() {
    for (int i = 1; i <= 4; i++)
      number[key[i]]++;
  }
  void dec() {
    for (int i = 1; i <= 4; i++)
      number[key[i]]--;
  }
} p[505];
LL calc(sqare fr, sqare bk) {
  fr.dec();
  bk.dec();
  LL ret = 0;
  for (int i = 1; i <= 4; i++) {
    int t = bk.x0;
    bk.x0 = bk.x1;
    bk.x1 = bk.x2;
    bk.x2 = bk.x3;
    bk.x3 = t;
    LL ans = 1;
    sqare key[5];
    key[1].ini(fr.x1, fr.x0, bk.x1, bk.x0);
    ans *= number[G(fr.x1, fr.x0, bk.x1, bk.x0)];
    key[1].dec();
    key[2].ini(fr.x2, fr.x1, bk.x0, bk.x3);
    ans *= number[G(fr.x2, fr.x1, bk.x0, bk.x3)];
    key[2].dec();
    key[3].ini(fr.x3, fr.x2, bk.x3, bk.x2);
    ans *= number[G(fr.x3, fr.x2, bk.x3, bk.x2)];
    key[3].dec();
    key[4].ini(fr.x0, fr.x3, bk.x2, bk.x1);
    ans *= number[G(fr.x0, fr.x3, bk.x2, bk.x1)];
    key[4].dec();
    ret += ans;
    for (int j = 1; j <= 4; j++)
      key[j].add();
  }
  fr.add();
  bk.add();
  return ret;
}
int n;
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    int a, b, c, d;
    scanf("%d%d%d%d", &a, &b, &c, &d);
    p[i].ini(a, b, c, d);
    p[i].add();
  }
  LL ans = 0;
  for (int i = 1; i <= n; i++)
    for (int j = 1; j <= n; j++)
      if (i != j)
        ans += calc(p[i], p[j]);
  cout << ans / 6 << endl;
  return 0;
}