Problem p03968 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(x) (x).begin(), (x).end()
#define LL long long
#define pii pair<int, int>
#define pll pair<LL, LL>

using namespace std;

map<LL, LL> colorCount;

LL tolong(LL c0, LL c1, LL c2, LL c3) {
  return (c0 * 1000000000LL + c1 * 1000000LL + c2 * 1000LL + c3);
}

void toc(LL num, LL &c0, LL &c1, LL &c2, LL &c3) {
  c0 = num / 1000000000LL;
  num %= 1000000000LL;
  c1 = num / 1000000LL;
  num %= 1000000LL;
  c2 = num / 1000LL;
  num %= 1000LL;
  c3 = num;
}

void add(LL c0, LL c1, LL c2, LL c3) {
  colorCount[tolong(c0, c1, c2, c3)] += 1;
  colorCount[tolong(c3, c0, c1, c2)] += 1;
  colorCount[tolong(c2, c3, c0, c1)] += 1;
  colorCount[tolong(c1, c2, c3, c0)] += 1;
}

void sub(LL c0, LL c1, LL c2, LL c3) {
  colorCount[tolong(c0, c1, c2, c3)] -= 1;
  colorCount[tolong(c3, c0, c1, c2)] -= 1;
  colorCount[tolong(c2, c3, c0, c1)] -= 1;
  colorCount[tolong(c1, c2, c3, c0)] -= 1;
}

int main() {

  int N;
  cin >> N;
  LL color[N];
  REP(i, N) {
    LL c0, c1, c2, c3;
    cin >> c0 >> c1 >> c2 >> c3;
    add(c0, c1, c2, c3);
    color[i] = tolong(c0, c1, c2, c3);
  }

  LL col[4];
  LL a, b, c, d, e, f, g, h;
  LL ans = 0;
  REP(i, N) {
    toc(color[i], col[0], col[1], col[2], col[3]);
    sub(col[0], col[1], col[2], col[3]);
    a = col[0], b = col[1], c = col[2], d = col[3];
    REP(j, N) {
      if (i == j)
        continue;
      toc(color[j], col[0], col[1], col[2], col[3]);
      sub(col[0], col[1], col[2], col[3]);
      REP(k, 4) {
        LL T = 1;
        e = col[(k) % 4], h = col[(k + 1) % 4], g = col[(k + 2) % 4],
        f = col[(k + 3) % 4];
        T *= colorCount[tolong(e, f, b, a)];
        sub(e, f, b, a);
        T *= colorCount[tolong(f, g, c, b)];
        sub(f, g, c, b);
        T *= colorCount[tolong(g, h, d, c)];
        sub(g, h, d, c);
        T *= colorCount[tolong(h, e, a, d)];
        sub(h, e, a, d);

        ans += T;
        add(e, f, b, a);
        add(f, g, c, b);
        add(g, h, d, c);
        add(h, e, a, d);
      }
      add(col[0], col[1], col[2], col[3]);
    }
    toc(color[i], col[0], col[1], col[2], col[3]);
    add(col[0], col[1], col[2], col[3]);
  }

  cout << ans / 6 << endl;

  return 0;
}

Fixed submission:
#include <bits/stdc++.h>
#define REP(i, n) for (int i = 0; i < (int)(n); i++)
#define ALL(x) (x).begin(), (x).end()
#define LL long long
#define pii pair<int, int>
#define pll pair<LL, LL>

using namespace std;

unordered_map<LL, LL> colorCount;

LL tolong(LL c0, LL c1, LL c2, LL c3) {
  return (c0 * 1000000000LL + c1 * 1000000LL + c2 * 1000LL + c3);
}

void toc(LL num, LL &c0, LL &c1, LL &c2, LL &c3) {
  c0 = num / 1000000000LL;
  num %= 1000000000LL;
  c1 = num / 1000000LL;
  num %= 1000000LL;
  c2 = num / 1000LL;
  num %= 1000LL;
  c3 = num;
}

void add(LL c0, LL c1, LL c2, LL c3) {
  colorCount[tolong(c0, c1, c2, c3)] += 1;
  colorCount[tolong(c3, c0, c1, c2)] += 1;
  colorCount[tolong(c2, c3, c0, c1)] += 1;
  colorCount[tolong(c1, c2, c3, c0)] += 1;
}

void sub(LL c0, LL c1, LL c2, LL c3) {
  colorCount[tolong(c0, c1, c2, c3)] -= 1;
  colorCount[tolong(c3, c0, c1, c2)] -= 1;
  colorCount[tolong(c2, c3, c0, c1)] -= 1;
  colorCount[tolong(c1, c2, c3, c0)] -= 1;
}

int main() {

  int N;
  cin >> N;
  LL color[N];
  REP(i, N) {
    LL c0, c1, c2, c3;
    cin >> c0 >> c1 >> c2 >> c3;
    add(c0, c1, c2, c3);
    color[i] = tolong(c0, c1, c2, c3);
  }

  LL col[4];
  LL a, b, c, d, e, f, g, h;
  LL ans = 0;
  REP(i, N) {
    toc(color[i], col[0], col[1], col[2], col[3]);
    sub(col[0], col[1], col[2], col[3]);
    a = col[0], b = col[1], c = col[2], d = col[3];
    REP(j, N) {
      if (i == j)
        continue;
      toc(color[j], col[0], col[1], col[2], col[3]);
      sub(col[0], col[1], col[2], col[3]);
      REP(k, 4) {
        LL T = 1;
        e = col[(k) % 4], h = col[(k + 1) % 4], g = col[(k + 2) % 4],
        f = col[(k + 3) % 4];
        T *= colorCount[tolong(e, f, b, a)];
        sub(e, f, b, a);
        T *= colorCount[tolong(f, g, c, b)];
        sub(f, g, c, b);
        T *= colorCount[tolong(g, h, d, c)];
        sub(g, h, d, c);
        T *= colorCount[tolong(h, e, a, d)];
        sub(h, e, a, d);

        ans += T;
        add(e, f, b, a);
        add(f, g, c, b);
        add(g, h, d, c);
        add(h, e, a, d);
      }
      add(col[0], col[1], col[2], col[3]);
    }
    toc(color[i], col[0], col[1], col[2], col[3]);
    add(col[0], col[1], col[2], col[3]);
  }

  cout << ans / 6 << endl;

  return 0;
}