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

#include <bits/stdc++.h>
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = s; i < t; ++i)
#define rng(a) a.begin(), a.end()
#define rrng(a) a.rbegin(), a.rend()
#define isin(x, l, r) ((l) <= (x) && (x) < (r))
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
#define pcnt __builtin_popcountll
#define uni(x) x.erase(unique(rng(x)), x.end())
#define snuke srand((unsigned)clock() + (unsigned)time(NULL));
#define show(x) cerr << #x << " = " << x << endl;
#define PQ(T) priority_queue<T, v(T), greater<T>>
#define bn(x) ((1 << x) - 1)
#define dup(x, y) (((x) + (y)-1) / (y))
#define newline puts("")
#define v(T) vector<T>
#define vv(T) v(v(T))
using namespace std;
typedef long long int ll;
typedef unsigned uint;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef tuple<int, int, int> T;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
typedef vector<T> vt;
int getInt() {
  int x;
  scanf("%d", &x);
  return x;
}
template <typename T> istream &operator>>(istream &i, v(T) & v) {
  rep(j, sz(v)) i >> v[j];
  return i;
}
template <typename T> string join(const v(T) & v) {
  stringstream s;
  rep(i, sz(v)) s << ' ' << v[i];
  return s.str().substr(1);
}
template <typename T> ostream &operator<<(ostream &o, const v(T) & v) {
  if (sz(v))
    o << join(v);
  return o;
}
template <typename T1, typename T2>
istream &operator>>(istream &i, pair<T1, T2> &v) {
  return i >> v.fi >> v.se;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &o, const pair<T1, T2> &v) {
  return o << v.fi << "," << v.se;
}
template <typename T> bool mins(T &x, const T &y) {
  if (x > y) {
    x = y;
    return true;
  } else
    return false;
}
template <typename T> bool maxs(T &x, const T &y) {
  if (x < y) {
    x = y;
    return true;
  } else
    return false;
}
template <typename T> ll suma(const v(T) & a) {
  ll res(0);
  for (auto &&x : a)
    res += x;
  return res;
}
const double eps = 1e-10;
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
#define dame                                                                   \\
  {                                                                            \\
    puts("-1");                                                                \\
    return 0;                                                                  \\
  }
#define yn                                                                     \\
  { puts("Yes"); }                                                             \\
  else {                                                                       \\
    puts("No");                                                                \\
  }
const int MX = 200005;
typedef v(vp) vvp;
inline int bsf(uint x) { return __builtin_ctz(x); }
inline int bsr(uint x) { return 31 ^ __builtin_clz(x); }

// Mod int
const int mod = 1000000007;
// const int mod = 998244353;
struct mint {
  ll x;
  mint() : x(0) {}
  mint(ll x) : x((x % mod + mod) % mod) {}
  // mint(ll x):x(x){}
  mint &fix() {
    x = (x % mod + mod) % mod;
    return *this;
  }
  mint operator-() const { return mint(0) - *this; }
  mint operator~() const { return mint(1) / *this; }
  mint &operator+=(const mint &a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint &a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint &a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint &operator/=(const mint &a) {
    (x *= a.pow(mod - 2).x) %= mod;
    return *this;
  }
  mint operator+(const mint &a) const { return mint(*this) += a; }
  mint operator-(const mint &a) const { return mint(*this) -= a; }
  mint operator*(const mint &a) const { return mint(*this) *= a; }
  mint operator/(const mint &a) const { return mint(*this) /= a; }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint res = pow(t / 2);
    res *= res;
    return (t & 1) ? res * x : res;
  }
  bool operator<(const mint &a) const { return x < a.x; }
  bool operator==(const mint &a) const { return x == a.x; }
};
mint ex(mint x, ll t) { return x.pow(t); }
istream &operator>>(istream &i, mint &a) {
  i >> a.x;
  return i;
}
ostream &operator<<(ostream &o, const mint &a) {
  o << a.x;
  return o;
}
typedef vector<mint> vm;
typedef vector<vm> vvm;
struct comb {
  vm f, g;
  comb() {}
  comb(int mx) : f(mx + 1), g(mx + 1) {
    f[0] = 1;
    rrep(i, mx) f[i] = f[i - 1] * i;
    g[mx] = f[mx].pow(mod - 2);
    for (int i = mx; i > 0; i--)
      g[i - 1] = g[i] * i;
  }
  mint operator()(int a, int b) {
    if (a < b)
      return 0;
    return f[a] * g[b] * g[a - b];
  }
};
//

int main() {
  int n;
  scanf("%d", &n);
  int n2 = 1 << n;
  int n1 = n2 >> 1;
  int m = n / 2;
  int m2 = 1 << (m + 1);
  vi p(n1);
  cin >> p;
  rep(i, n1) p[i]--;
  vvm d(m2, vm(m2));
  rep(i, n1) {
    int a = n1 + i, b = n1 + p[i];
    mint xa = 1, xb = 1;
    rep(k, n - m - 1) xa *= a, a >>= 1;
    rep(k, n - m - 1) xb *= b, b >>= 1;
    assert(a < m2);
    rep(j, m) {
      int na = a;
      mint nxa = xa;
      xb *= b;
      rep(k, m) {
        nxa *= na;
        d[na][b] += nxa * xb;
        na >>= 1;
      }
      b >>= 1;
    }
  }
  mint ans;
  srep(i, 2, m2) {
    srep(j, 2, m2) { ans += d[i][j] * d[i ^ 1][j ^ 1] * (i >> 1) * (j >> 1); }
  }
  ans /= 2;

  vvm xs(n1, vm(n));
  rep(i, n1) {
    int a = n1 + i;
    mint x = 1;
    rep(j, n) {
      x *= a;
      xs[i][j] = x;
      a >>= 1;
    }
  }
  auto f = [&](int a, int b) {
    int e = a ^ b;
    e = bsr(e);
    return xs[a][e] * xs[b][e + 1];
  };

  int m1 = m2 >> 1;
  int w = n - m - 1;
  int w2 = 1 << w;
  srep(r, m1, m2) {
    vi is;
    rep(i, w2) is.pb(r << w | i);
    rep(i, w2) rep(j, i) {
      int a = is[i] - n1, b = is[j] - n1;
      int na = p[a], nb = p[b];
      ans += f(a, b) * f(na, nb);
    }
  }
  vi q(n1);
  rep(i, n1) q[p[i]] = i;
  srep(r, m1, m2) {
    vi is;
    rep(i, w2) is.pb(r << w | i);
    rep(i, w2) rep(j, i) {
      int a = is[i] - n1, b = is[j] - n1;
      int na = q[a], nb = q[b];
      if ((na >> w) == (nb >> w))
        continue;
      ans += f(a, b) * f(na, nb);
    }
  }
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define fi first
#define se second
#define rep(i, n) for (int i = 0; i < (n); ++i)
#define rrep(i, n) for (int i = 1; i <= (n); ++i)
#define drep(i, n) for (int i = (n)-1; i >= 0; --i)
#define srep(i, s, t) for (int i = s; i < t; ++i)
#define rng(a) a.begin(), a.end()
#define rrng(a) a.rbegin(), a.rend()
#define isin(x, l, r) ((l) <= (x) && (x) < (r))
#define pb push_back
#define eb emplace_back
#define sz(x) (int)(x).size()
#define pcnt __builtin_popcountll
#define uni(x) x.erase(unique(rng(x)), x.end())
#define snuke srand((unsigned)clock() + (unsigned)time(NULL));
#define show(x) cerr << #x << " = " << x << endl;
#define PQ(T) priority_queue<T, v(T), greater<T>>
#define bn(x) ((1 << x) - 1)
#define dup(x, y) (((x) + (y)-1) / (y))
#define newline puts("")
#define v(T) vector<T>
#define vv(T) v(v(T))
using namespace std;
typedef long long int ll;
typedef unsigned uint;
typedef unsigned long long ull;
typedef pair<int, int> P;
typedef tuple<int, int, int> T;
typedef vector<int> vi;
typedef vector<vi> vvi;
typedef vector<ll> vl;
typedef vector<P> vp;
typedef vector<T> vt;
int getInt() {
  int x;
  scanf("%d", &x);
  return x;
}
template <typename T> istream &operator>>(istream &i, v(T) & v) {
  rep(j, sz(v)) i >> v[j];
  return i;
}
template <typename T> string join(const v(T) & v) {
  stringstream s;
  rep(i, sz(v)) s << ' ' << v[i];
  return s.str().substr(1);
}
template <typename T> ostream &operator<<(ostream &o, const v(T) & v) {
  if (sz(v))
    o << join(v);
  return o;
}
template <typename T1, typename T2>
istream &operator>>(istream &i, pair<T1, T2> &v) {
  return i >> v.fi >> v.se;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &o, const pair<T1, T2> &v) {
  return o << v.fi << "," << v.se;
}
template <typename T> bool mins(T &x, const T &y) {
  if (x > y) {
    x = y;
    return true;
  } else
    return false;
}
template <typename T> bool maxs(T &x, const T &y) {
  if (x < y) {
    x = y;
    return true;
  } else
    return false;
}
template <typename T> ll suma(const v(T) & a) {
  ll res(0);
  for (auto &&x : a)
    res += x;
  return res;
}
const double eps = 1e-10;
const ll LINF = 1001002003004005006ll;
const int INF = 1001001001;
#define dame                                                                   \\
  {                                                                            \\
    puts("-1");                                                                \\
    return 0;                                                                  \\
  }
#define yn                                                                     \\
  { puts("Yes"); }                                                             \\
  else {                                                                       \\
    puts("No");                                                                \\
  }
const int MX = 200005;
typedef v(vp) vvp;

// Mod int
const int mod = 1000000007;
// const int mod = 998244353;
struct mint {
  ll x;
  mint() : x(0) {}
  mint(ll x) : x((x % mod + mod) % mod) {}
  // mint(ll x):x(x){}
  mint &fix() {
    x = (x % mod + mod) % mod;
    return *this;
  }
  mint operator-() const { return mint(0) - *this; }
  mint operator~() const { return mint(1) / *this; }
  mint &operator+=(const mint &a) {
    if ((x += a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator-=(const mint &a) {
    if ((x += mod - a.x) >= mod)
      x -= mod;
    return *this;
  }
  mint &operator*=(const mint &a) {
    (x *= a.x) %= mod;
    return *this;
  }
  mint &operator/=(const mint &a) {
    (x *= a.pow(mod - 2).x) %= mod;
    return *this;
  }
  mint operator+(const mint &a) const { return mint(*this) += a; }
  mint operator-(const mint &a) const { return mint(*this) -= a; }
  mint operator*(const mint &a) const { return mint(*this) *= a; }
  mint operator/(const mint &a) const { return mint(*this) /= a; }
  mint pow(ll t) const {
    if (!t)
      return 1;
    mint res = pow(t / 2);
    res *= res;
    return (t & 1) ? res * x : res;
  }
  bool operator<(const mint &a) const { return x < a.x; }
  bool operator==(const mint &a) const { return x == a.x; }
};
mint ex(mint x, ll t) { return x.pow(t); }
istream &operator>>(istream &i, mint &a) {
  i >> a.x;
  return i;
}
ostream &operator<<(ostream &o, const mint &a) {
  o << a.x;
  return o;
}
typedef vector<mint> vm;
struct comb {
  vm f, g;
  comb() {}
  comb(int mx) : f(mx + 1), g(mx + 1) {
    f[0] = 1;
    rrep(i, mx) f[i] = f[i - 1] * i;
    g[mx] = f[mx].pow(mod - 2);
    for (int i = mx; i > 0; i--)
      g[i - 1] = g[i] * i;
  }
  mint operator()(int a, int b) {
    if (a < b)
      return 0;
    return f[a] * g[b] * g[a - b];
  }
};
//

int main() {
  int n;
  scanf("%d", &n);
  int n2 = 1 << n;
  int n1 = n2 >> 1;
  vi p(n1);
  cin >> p;
  rep(i, n1) p[i]--;
  vector<unordered_map<int, mint>> d(n2);
  rep(i, n1) {
    int a = n1 + i, b = n1 + p[i];
    mint xa = 1, xb = 1;
    rep(j, n - 1) {
      xb *= b;
      a = n1 + i;
      xa = 1;
      rep(k, n - 1) {
        xa *= a;
        d[a][b] += (xa * xb);
        a >>= 1;
      }
      b >>= 1;
    }
  }
  mint ans;
  rrep(i, n1 - 1) {
    int l = i << 1, r = l + 1;
    for (auto &p : d[l]) {
      int j = p.fi;
      mint x = p.se;
      ans += x * d[r][j ^ 1] * i * (j >> 1);
    }
  }
  cout << ans << endl;
  return 0;
}