Problem p03968 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define fi first
#define se second
#define mp make_pair
#define pb push_back

typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef unsigned long long ull;
typedef long double ld;
typedef tree<ii, null_type, less<ii>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds;

map<vi, int> ma;
map<vi, int> rotar;
vi A[1111];

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    vi V;
    for (int j = 0; j < 4; j++) {
      int x;
      cin >> x;
      V.pb(x);
    }
    A[i] = V;
    for (int j = 0; j < 4; j++) {
      ma[V]++;
      V.pb(*V.begin());
      V.erase(V.begin());
    }
  }
  for (auto X : ma) {
    vi v = X.fi;
    vi ori = v;
    int cnt = 0;
    for (int i = 0; i < 4; i++) {
      v.pb(*v.begin());
      v.erase(v.begin());
      if (v == ori)
        cnt++;
    }
    rotar[v] = cnt;
  }
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      vi a = A[i];
      vi b = A[j];
      for (int k = 0; k < 4; k++) {
        ma[a]--;
        ma[b]--;
        a.pb(*a.begin());
        a.erase(a.begin());
        b.pb(*b.begin());
        b.erase(b.begin());
      }
      for (int k = 0; k < 4; k++) {
        vi F[4];
        F[0] = {a[0], b[1], b[0], a[1]};
        F[1] = {a[3], a[2], b[3], b[2]};
        F[2] = {b[1], a[0], a[3], b[2]};
        F[3] = {a[1], b[0], b[3], a[2]};
        ll res = 1;
        for (int z = 0; z < 4; z++) {
          res *= ma[F[z]];
          for (int dir = 0; dir < 4; dir++) {
            ma[F[z]]--;
            F[z].pb(*F[z].begin());
            F[z].erase(F[z].begin());
          }
        }
        for (int z = 0; z < 4; z++) {
          for (int dir = 0; dir < 4; dir++) {
            ma[F[z]]++;
            F[z].pb(*F[z].begin());
            F[z].erase(F[z].begin());
          }
        }
        ans += res;
        b.pb(*b.begin());
        b.erase(b.begin());
      }
      for (int k = 0; k < 4; k++) {
        ma[a]++;
        ma[b]++;
        a.pb(*a.begin());
        a.erase(a.begin());
        b.pb(*b.begin());
        b.erase(b.begin());
      }
    }
  }
  cout << ans / 3 << '\n';
}


Fixed submission:
#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>

using namespace std;
using namespace __gnu_pbds;

#define fi first
#define se second
#define mp make_pair
#define pb push_back

typedef long long ll;
typedef pair<int, int> ii;
typedef vector<int> vi;
typedef unsigned long long ull;
typedef long double ld;
typedef tree<ii, null_type, less<ii>, rb_tree_tag,
             tree_order_statistics_node_update>
    pbds;

map<vi, int> ma;
map<vi, int> rotar;
vi A[1111];

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    vi V;
    for (int j = 0; j < 4; j++) {
      int x;
      cin >> x;
      V.pb(x);
    }
    A[i] = V;
    for (int j = 0; j < 4; j++) {
      ma[V]++;
      V.pb(*V.begin());
      V.erase(V.begin());
    }
  }
  for (auto X : ma) {
    vi v = X.fi;
    vi ori = v;
    int cnt = 0;
    for (int i = 0; i < 4; i++) {
      v.pb(*v.begin());
      v.erase(v.begin());
      if (v == ori)
        cnt++;
    }
    rotar[v] = cnt;
  }
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      vi a = A[i];
      vi b = A[j];
      for (int k = 0; k < 4; k++) {
        ma[a]--;
        ma[b]--;
        a.pb(*a.begin());
        a.erase(a.begin());
        b.pb(*b.begin());
        b.erase(b.begin());
      }
      for (int k = 0; k < 4; k++) {
        vi F[4];
        F[0] = {a[0], b[1], b[0], a[1]};
        F[1] = {a[3], a[2], b[3], b[2]};
        F[2] = {b[1], a[0], a[3], b[2]};
        F[3] = {a[1], b[0], b[3], a[2]};
        if (!ma[F[0]] || !ma[F[1]] || !ma[F[2]] || !ma[F[3]]) {
          b.pb(*b.begin());
          b.erase(b.begin());
          continue;
        }
        ll res = 1;
        for (int z = 0; z < 4; z++) {
          res *= ma[F[z]];
          for (int dir = 0; dir < 4; dir++) {
            ma[F[z]]--;
            F[z].pb(*F[z].begin());
            F[z].erase(F[z].begin());
          }
        }
        for (int z = 0; z < 4; z++) {
          for (int dir = 0; dir < 4; dir++) {
            ma[F[z]]++;
            F[z].pb(*F[z].begin());
            F[z].erase(F[z].begin());
          }
        }
        ans += res;
        b.pb(*b.begin());
        b.erase(b.begin());
      }
      for (int k = 0; k < 4; k++) {
        ma[a]++;
        ma[b]++;
        a.pb(*a.begin());
        a.erase(a.begin());
        b.pb(*b.begin());
        b.erase(b.begin());
      }
    }
  }
  cout << ans / 3 << '\n';
}