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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define X first
#define Y second
#define pb push_back
#define rep(X, Y) for (int(X) = 0; (X) < (int)(Y); ++(X))
#define reps(X, S, Y) for (int(X) = (int)(S); (X) < (int)(Y); ++(X))
#define rrep(X, Y) for (int(X) = (int)(Y)-1; (X) >= 0; --(X))
#define rreps(X, S, Y) for (int(X) = (int)(Y)-1; (X) >= (int)(S); --(X))
#define repe(X, Y) for ((X) = 0; (X) < (Y); ++(X))
#define peat(X, Y) for (; (X) < (Y); ++(X))
#define all(X) (X).begin(), (X).end()
#define rall(X) (X).rbegin(), (X).rend()
#define eb emplace_back
#define UNIQUE(X) (X).erase(unique(all(X)), (X).end())
#define Endl endl
#define NL << "\\n"
#define cauto const auto

using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
template <class T> using vv = vector<vector<T>>;
template <class T> inline bool MX(T &l, const T &r) {
  return l < r ? l = r, 1 : 0;
}
template <class T> inline bool MN(T &l, const T &r) {
  return l > r ? l = r, 1 : 0;
}
// #undef NUIP
#ifdef NUIP
#include "benri.h"
#else
#define out(args...)
#endif
#ifdef __cpp_init_captures
template <typename T> vector<T> table(int n, T v) { return vector<T>(n, v); }
template <class... Args> auto table(int n, Args... args) {
  auto val = table(args...);
  return vector<decltype(val)>(n, move(val));
}
#endif
template <class A, class B>
pair<A, B> operator+(const pair<A, B> &p, const pair<A, B> &q) {
  return {p.X + q.X, p.Y + q.Y};
}
template <class A, class B, class C, class D>
pair<A, B> &operator+=(pair<A, B> &p, const pair<C, D> &q) {
  p.X += q.X;
  p.Y += q.Y;
  return p;
}
template <class A, class B>
pair<A, B> operator-(const pair<A, B> &p, const pair<A, B> &q) {
  return {p.X - q.X, p.Y - q.Y};
}
template <class A, class B, class C, class D>
pair<A, B> &operator-=(pair<A, B> &p, const pair<C, D> &q) {
  p.X -= q.X;
  p.Y -= q.Y;
  return p;
}
template <class A, class B> istream &operator>>(istream &is, pair<A, B> &p) {
  is >> p.X >> p.Y;
  return is;
}
template <class T = ll> T read() {
  T re;
  cin >> re;
  return move(re);
}
template <class T = ll> T read(const T &dec) {
  T re;
  cin >> re;
  return re - dec;
}
template <class T = ll> vector<T> readV(const int sz) {
  vector<T> re(sz);
  for (auto &x : re)
    x = read<T>();
  return move(re);
}
template <class T = ll> vector<T> readV(const int sz, const T &dec) {
  vector<T> re(sz);
  for (auto &x : re)
    x = read<T>(dec);
  return move(re);
}
vv<int> readG(const int &n, const int &m) {
  vv<int> g(n);
  rep(_, m) {
    cauto a = read<int>(1), b = read<int>(1);
    g[a].pb(b);
    g[b].pb(a);
  }
  return move(g);
}
vv<int> readG(const int &n) { return readG(n, n - 1); }
const ll MOD = 1e9 + 7; // 998244353

ll modpow_(ll r, ll n, ll m = MOD) {
  ll re = 1, d = r % m;
  if (n < 0)
    (n %= m - 1) += m - 1;
  for (; n; n /= 2) {
    if (n & 1)
      (re *= d) %= m;
    (d *= d) %= m;
  }
  return re;
}
template <int mod = MOD> struct ModInt {
  int v;
  ModInt(int v = 0) : v(v) {}
  ModInt operator+(const ModInt &n) const {
    return v + n.v < mod ? v + n.v : v + n.v - mod;
  }
  ModInt operator-(const ModInt &n) const {
    return v - n.v < 0 ? v - n.v + mod : v - n.v;
  }
  ModInt operator*(const ModInt &n) const { return ll(v) * n.v % mod; }
  ModInt operator/(const ModInt &n) const {
    return ll(v) * modpow_(n.v % mod, -1, mod) % mod;
  }
  ModInt operator+(const ll &n) const {
    return v + n < mod ? v + n : v + n - mod;
  }
  ModInt operator-(const ll &n) const {
    return v - n < 0 ? v - n + mod : v - n;
  }
  ModInt operator*(const ll &n) const { return ll(v) * (n % mod) % mod; }
  ModInt operator/(const ll &n) const {
    return ll(v) * modpow_(n % mod, -1, mod) % mod;
  }
  ModInt &operator+=(const ModInt &n) {
    v += n.v;
    if (v >= mod)
      v -= mod;
    return *this;
  }
  ModInt &operator-=(const ModInt &n) {
    v -= n.v;
    if (v < 0)
      v += mod;
    return *this;
  }
  ModInt &operator*=(const ModInt &n) {
    v = ll(v) * n.v % mod;
    return *this;
  }
  ModInt &operator/=(const ModInt &n) {
    v = ll(v) * modpow_(n.v, -1, mod) % mod;
    return *this;
  }
  ModInt &operator+=(const ll &n) {
    v += n;
    if (v >= mod)
      v -= mod;
    return *this;
  }
  ModInt &operator-=(const ll &n) {
    v -= n;
    if (v < 0)
      v += mod;
    return *this;
  }
  ModInt &operator*=(const ll &n) {
    v = ll(v) * n % mod;
    return *this;
  }
  ModInt &operator/=(const ll &n) {
    v = ll(v) * modpow_(n, -1, mod) % mod;
    return *this;
  }
  bool operator==(const ModInt &n) const { return v == n.v; };
  bool operator!=(const ModInt &n) const { return v != n.v; };
  ModInt &operator++() { return operator+=(1); }
  ModInt &operator--() { return operator-=(1); }
  ModInt operator++(int i) {
    ModInt tmp(*this);
    operator++();
    return tmp;
  }
  ModInt operator--(int i) {
    ModInt tmp(*this);
    operator--();
    return tmp;
  }
};
#ifdef NUIP
class MINT_FRAC____ {
public:
  unordered_map<int, pii> dict;
  MINT_FRAC____(int n) {
    rep(p, n + 1) reps(q, 1, n + 1) if (__gcd(p, q) == 1) {
      dict[1ll * p * modpow_(q, -1) % MOD] = pii(p, q);
      dict[MOD - 1ll * p * modpow_(q, -1) % MOD] = pii(-p, q);
    }
  }
} Il1Il1Il1(1000);
template <int mod> ostream &operator<<(ostream &os, const ModInt<mod> &n) {
  if (Il1Il1Il1.dict.count(n.v))
    os << n.v << "(" << Il1Il1Il1.dict[n.v].X << "/" << Il1Il1Il1.dict[n.v].Y
       << ")";
  else
    os << n.v;
  return os;
};
#else
template <int mod> ostream &operator<<(ostream &os, const ModInt<mod> &n) {
  return os << n.v;
};
#endif
template <int mod> ModInt<mod> operator+(const ll &n, const ModInt<mod> &m) {
  return m.v + n < mod ? m.v + n : m.v + n - mod;
}
template <int mod> ModInt<mod> operator-(const ll &n, const ModInt<mod> &m) {
  return n - m.v < 0 ? n - m.v + mod : n - m.v;
}
template <int mod> ModInt<mod> operator*(const ll &n, const ModInt<mod> &m) {
  return ll(m.v) * (n % mod) % mod;
}
template <int mod> ModInt<mod> operator/(const ll &n, const ModInt<mod> &m) {
  return ModInt<mod>(n % mod) / m;
}
typedef ModInt<MOD> mint;
template <int mod> ModInt<mod> modpow(ModInt<mod> r, ll n) {
  ModInt<mod> re(1);
  if (n < 0)
    (n %= mod - 1) += mod - 1;
  for (; n; n /= 2) {
    if (n & 1)
      re *= r;
    r *= r;
  }
  return re;
}
vector<mint> fact, finv, inv;
mint comb(ll n, ll r) {
  if (n < r || r < 0)
    return 0;
  return fact[n] * finv[n - r] * finv[r];
}
class Doralion {
  void Modinvs(vector<mint> &re, int n) {
    re.resize(n + 1);
    re[1] = 1;
    for (int i = 2; i <= n; ++i)
      re[i] = re[MOD % i] * (MOD - MOD / i);
  }
  void Facts(vector<mint> &re, int n) {
    re.resize(n + 1);
    re[0] = 1;
    rep(i, n) re[i + 1] = re[i] * (i + 1);
  }
  void Factinvs(vector<mint> &re, const vector<mint> &inv, int n) {
    re.resize(n + 1);
    re[0] = 1;
    rep(i, n) re[i + 1] = re[i] * inv[i + 1];
  }

public:
  Doralion(int n) {
    Modinvs(inv, n);
    Facts(fact, n);
    Factinvs(finv, inv, n);
  }
} doralion(1123456);
using pmm = pair<mint, mint>;
mint modpow(ll r, ll n) { return modpow_(r, n); }

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(0);
  cauto h = read(1);
  cauto n = 1 << h;
  cauto perm = readV(n, 1);
  // vector<int> perm(n); iota(all(perm),0); random_shuffle(all(perm));
  out(perm, 1);
  vector<mint> prods(2 << h);
  iota(all(prods), 0);
  reps(i, 2, prods.size()) prods[i] *= prods[i / 2];
  mint re = 0;
  vector<vector<pair<int, mint>>> mps(n + n);
  rep(d, h) {
    rep(i, n) {
      cauto lbl = (i + n) >> d;
      cauto lca = (i + n) >> (d + 1);
      auto val = prods[i + n] / prods[lca]; // pre calc
      if (lbl % 2)
        val *= lca;
      cauto dst = n + perm[i];
      mps[dst].eb(lbl, val * dst);
    }
  }
  rreps(i, 1, n) {
    auto &l = mps[i * 2];
    auto &r = mps[i * 2 + 1];
    // for(auto [lbl,val]:r){
    // 	re+=l[lbl^1]*val*i;
    // }
    auto &cur = mps[i];
    cur.reserve(l.size() + r.size());
    while (l.size() && r.size()) {
      if (l.back().X == r.back().X) {
        cur.pb(l.back());
        cur.back().Y += r.back().Y;
        if (l.size() > 1 && l[l.size() - 2].X == (r.back().X ^ 1))
          re += r.back().Y * l[l.size() - 2].Y * i;
        if (r.size() > 1 && r[r.size() - 2].X == (l.back().X ^ 1))
          re += l.back().Y * r[r.size() - 2].Y * i;
        l.pop_back();
        r.pop_back();
      } else if (l.back().X < r.back().X) {
        cur.pb(l.back());
        if (r.back().X == (l.back().X ^ 1))
          re += r.back().Y * l.back().Y * i;
        l.pop_back();
      } else {
        cur.pb(r.back());
        if (l.back().X == (r.back().X ^ 1))
          re += r.back().Y * l.back().Y * i;
        r.pop_back();
      }
    }
    for (; l.size(); l.pop_back())
      cur.eb(l.back());
    for (; r.size(); r.pop_back())
      cur.eb(r.back());
    reverse(all(mps[i]));
    for (auto &p : mps[i])
      p.Y *= i;
    out(i, mps[i], 1);
  }

  cout << re NL;
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <list>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <unordered_map>
#include <unordered_set>
#include <vector>
#define X first
#define Y second
#define pb push_back
#define rep(X, Y) for (int(X) = 0; (X) < (int)(Y); ++(X))
#define reps(X, S, Y) for (int(X) = (int)(S); (X) < (int)(Y); ++(X))
#define rrep(X, Y) for (int(X) = (int)(Y)-1; (X) >= 0; --(X))
#define rreps(X, S, Y) for (int(X) = (int)(Y)-1; (X) >= (int)(S); --(X))
#define repe(X, Y) for ((X) = 0; (X) < (Y); ++(X))
#define peat(X, Y) for (; (X) < (Y); ++(X))
#define all(X) (X).begin(), (X).end()
#define rall(X) (X).rbegin(), (X).rend()
#define eb emplace_back
#define UNIQUE(X) (X).erase(unique(all(X)), (X).end())
#define Endl endl
#define NL << "\\n"
#define cauto const auto

using namespace std;
using ll = long long;
using pii = pair<int, int>;
using pll = pair<ll, ll>;
template <class T> using vv = vector<vector<T>>;
template <class T> inline bool MX(T &l, const T &r) {
  return l < r ? l = r, 1 : 0;
}
template <class T> inline bool MN(T &l, const T &r) {
  return l > r ? l = r, 1 : 0;
}
// #undef NUIP
#ifdef NUIP
#include "benri.h"
#else
#define out(args...)
#endif
#ifdef __cpp_init_captures
template <typename T> vector<T> table(int n, T v) { return vector<T>(n, v); }
template <class... Args> auto table(int n, Args... args) {
  auto val = table(args...);
  return vector<decltype(val)>(n, move(val));
}
#endif
template <class A, class B>
pair<A, B> operator+(const pair<A, B> &p, const pair<A, B> &q) {
  return {p.X + q.X, p.Y + q.Y};
}
template <class A, class B, class C, class D>
pair<A, B> &operator+=(pair<A, B> &p, const pair<C, D> &q) {
  p.X += q.X;
  p.Y += q.Y;
  return p;
}
template <class A, class B>
pair<A, B> operator-(const pair<A, B> &p, const pair<A, B> &q) {
  return {p.X - q.X, p.Y - q.Y};
}
template <class A, class B, class C, class D>
pair<A, B> &operator-=(pair<A, B> &p, const pair<C, D> &q) {
  p.X -= q.X;
  p.Y -= q.Y;
  return p;
}
template <class A, class B> istream &operator>>(istream &is, pair<A, B> &p) {
  is >> p.X >> p.Y;
  return is;
}
template <class T = ll> T read() {
  T re;
  cin >> re;
  return move(re);
}
template <class T = ll> T read(const T &dec) {
  T re;
  cin >> re;
  return re - dec;
}
template <class T = ll> vector<T> readV(const int sz) {
  vector<T> re(sz);
  for (auto &x : re)
    x = read<T>();
  return move(re);
}
template <class T = ll> vector<T> readV(const int sz, const T &dec) {
  vector<T> re(sz);
  for (auto &x : re)
    x = read<T>(dec);
  return move(re);
}
vv<int> readG(const int &n, const int &m) {
  vv<int> g(n);
  rep(_, m) {
    cauto a = read<int>(1), b = read<int>(1);
    g[a].pb(b);
    g[b].pb(a);
  }
  return move(g);
}
vv<int> readG(const int &n) { return readG(n, n - 1); }
const ll MOD = 1e9 + 7; // 998244353

ll modpow_(ll r, ll n, ll m = MOD) {
  ll re = 1, d = r % m;
  if (n < 0)
    (n %= m - 1) += m - 1;
  for (; n; n /= 2) {
    if (n & 1)
      (re *= d) %= m;
    (d *= d) %= m;
  }
  return re;
}
template <int mod = MOD> struct ModInt {
  int v;
  ModInt(int v = 0) : v(v) {}
  ModInt operator+(const ModInt &n) const {
    return v + n.v < mod ? v + n.v : v + n.v - mod;
  }
  ModInt operator-(const ModInt &n) const {
    return v - n.v < 0 ? v - n.v + mod : v - n.v;
  }
  ModInt operator*(const ModInt &n) const { return ll(v) * n.v % mod; }
  ModInt operator/(const ModInt &n) const {
    return ll(v) * modpow_(n.v % mod, -1, mod) % mod;
  }
  ModInt operator+(const ll &n) const {
    return v + n < mod ? v + n : v + n - mod;
  }
  ModInt operator-(const ll &n) const {
    return v - n < 0 ? v - n + mod : v - n;
  }
  ModInt operator*(const ll &n) const { return ll(v) * (n % mod) % mod; }
  ModInt operator/(const ll &n) const {
    return ll(v) * modpow_(n % mod, -1, mod) % mod;
  }
  ModInt &operator+=(const ModInt &n) {
    v += n.v;
    if (v >= mod)
      v -= mod;
    return *this;
  }
  ModInt &operator-=(const ModInt &n) {
    v -= n.v;
    if (v < 0)
      v += mod;
    return *this;
  }
  ModInt &operator*=(const ModInt &n) {
    v = ll(v) * n.v % mod;
    return *this;
  }
  ModInt &operator/=(const ModInt &n) {
    v = ll(v) * modpow_(n.v, -1, mod) % mod;
    return *this;
  }
  ModInt &operator+=(const ll &n) {
    v += n;
    if (v >= mod)
      v -= mod;
    return *this;
  }
  ModInt &operator-=(const ll &n) {
    v -= n;
    if (v < 0)
      v += mod;
    return *this;
  }
  ModInt &operator*=(const ll &n) {
    v = ll(v) * n % mod;
    return *this;
  }
  ModInt &operator/=(const ll &n) {
    v = ll(v) * modpow_(n, -1, mod) % mod;
    return *this;
  }
  bool operator==(const ModInt &n) const { return v == n.v; };
  bool operator!=(const ModInt &n) const { return v != n.v; };
  ModInt &operator++() { return operator+=(1); }
  ModInt &operator--() { return operator-=(1); }
  ModInt operator++(int i) {
    ModInt tmp(*this);
    operator++();
    return tmp;
  }
  ModInt operator--(int i) {
    ModInt tmp(*this);
    operator--();
    return tmp;
  }
};
#ifdef NUIP
class MINT_FRAC____ {
public:
  unordered_map<int, pii> dict;
  MINT_FRAC____(int n) {
    rep(p, n + 1) reps(q, 1, n + 1) if (__gcd(p, q) == 1) {
      dict[1ll * p * modpow_(q, -1) % MOD] = pii(p, q);
      dict[MOD - 1ll * p * modpow_(q, -1) % MOD] = pii(-p, q);
    }
  }
} Il1Il1Il1(1000);
template <int mod> ostream &operator<<(ostream &os, const ModInt<mod> &n) {
  if (Il1Il1Il1.dict.count(n.v))
    os << n.v << "(" << Il1Il1Il1.dict[n.v].X << "/" << Il1Il1Il1.dict[n.v].Y
       << ")";
  else
    os << n.v;
  return os;
};
#else
template <int mod> ostream &operator<<(ostream &os, const ModInt<mod> &n) {
  return os << n.v;
};
#endif
template <int mod> ModInt<mod> operator+(const ll &n, const ModInt<mod> &m) {
  return m.v + n < mod ? m.v + n : m.v + n - mod;
}
template <int mod> ModInt<mod> operator-(const ll &n, const ModInt<mod> &m) {
  return n - m.v < 0 ? n - m.v + mod : n - m.v;
}
template <int mod> ModInt<mod> operator*(const ll &n, const ModInt<mod> &m) {
  return ll(m.v) * (n % mod) % mod;
}
template <int mod> ModInt<mod> operator/(const ll &n, const ModInt<mod> &m) {
  return ModInt<mod>(n % mod) / m;
}
typedef ModInt<MOD> mint;
template <int mod> ModInt<mod> modpow(ModInt<mod> r, ll n) {
  ModInt<mod> re(1);
  if (n < 0)
    (n %= mod - 1) += mod - 1;
  for (; n; n /= 2) {
    if (n & 1)
      re *= r;
    r *= r;
  }
  return re;
}
vector<mint> fact, finv, inv;
mint comb(ll n, ll r) {
  if (n < r || r < 0)
    return 0;
  return fact[n] * finv[n - r] * finv[r];
}
class Doralion {
  void Modinvs(vector<mint> &re, int n) {
    re.resize(n + 1);
    re[1] = 1;
    for (int i = 2; i <= n; ++i)
      re[i] = re[MOD % i] * (MOD - MOD / i);
  }
  void Facts(vector<mint> &re, int n) {
    re.resize(n + 1);
    re[0] = 1;
    rep(i, n) re[i + 1] = re[i] * (i + 1);
  }
  void Factinvs(vector<mint> &re, const vector<mint> &inv, int n) {
    re.resize(n + 1);
    re[0] = 1;
    rep(i, n) re[i + 1] = re[i] * inv[i + 1];
  }

public:
  Doralion(int n) {
    Modinvs(inv, n);
    Facts(fact, n);
    Factinvs(finv, inv, n);
  }
} doralion(1123456);
using pmm = pair<mint, mint>;
mint modpow(ll r, ll n) { return modpow_(r, n); }

int main() {
  ios_base::sync_with_stdio(false);
  cin.tie(0);
  cout << fixed << setprecision(0);
  cauto h = read(1);
  cauto n = 1 << h;
  cauto perm = readV(n, 1);
  out(perm, 1);
  vector<mint> prods(2 << h);
  iota(all(prods), 0);
  reps(i, 2, prods.size()) prods[i] *= prods[i / 2];
  mint re = 0;
  rep(d, h) {
    vector<unordered_map<int, mint>> mps(n + n);
    rep(i, n) {
      cauto lbl = i >> d;
      cauto lca = (i + n) >> (d + 1);
      auto val = prods[i + n] / prods[lca]; // pre calc
      if (lbl % 2)
        val *= lca;
      cauto dst = n + perm[i];
      mps[dst][lbl] += val * dst;
    }
    rreps(i, 1, n) {
      auto &l = mps[i * 2];
      auto &r = mps[i * 2 + 1];
      for (auto [lbl, val] : r) {
        re += l[lbl ^ 1] * val * i;
      }
      for (auto [lbl, val] : l)
        mps[i][lbl] += val * i;
      for (auto [lbl, val] : r)
        mps[i][lbl] += val * i;
    }
  }
  cout << re NL;
  return 0;
}