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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define all(a) a.begin(), a.end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define pb push_back
#define debug(x) cerr << #x << ':' << (x) << '\\n'
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> P;
typedef complex<ld> com;
constexpr int inf = 1000000010;
constexpr ll INF = 1000000000000000010;
constexpr ld eps = 1e-12;
constexpr ld pi = 3.141592653589793238;
template <class T, class U> inline bool chmax(T &a, const U &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T, class U> inline bool chmin(T &a, const U &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

constexpr ll mod = 1000000007;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);

  int h;
  cin >> h;
  int sz = 1 << (h - 1);
  vector<int> p(sz);
  rep(i, sz) {
    cin >> p[i];
    p[i]--;
  }
  vector<vector<ll>> prod(sz, vector<ll>(h - 1));
  rep(i, sz) {
    ll x = i + sz;
    ll tmp = x;
    rep(j, h - 1) {
      prod[i][j] = tmp;
      x >>= 1;
      tmp *= x;
      tmp %= mod;
    }
  }
  vector<vector<P>> vec(2 * sz, vector<P>());
  vector<int> lst(2 * sz, -1);
  int bor = 1;
  int cap = 0;
  rep(i, 2 * sz) {
    if (i >= bor) {
      bor <<= 1;
      cap++;
    }
    int x = 1 << (h - cap);
    x *= (h - 1);
    if (i >= 2)
      vec[i].reserve(x);
  }
  rep(step, h - 1) {
    for (int i = sz - 1; i >= 0; i--) {
      int x = i + sz;
      int y = p[i] + sz;
      x >>= step;
      int idx = y;
      rep(j, h - 1) {
        if (lst[idx] == x) {
          vec[idx].back().first += prod[i][step] * prod[p[i]][j];
          vec[idx].back().first %= mod;
        } else {
          vec[idx].pb({prod[i][step] * prod[p[i]][j] % mod, x});
          lst[idx] = x;
        }
        idx >>= 1;
      }
    }
  }
  rep(i, 2 * sz) {
    reverse(all(vec[i]));
    if (i & 1) {
      for (int j = 0; j < vec[i].size(); j++) {
        vec[i][j].second ^= 1;
        if (j > 0 && vec[i][j].second < vec[i][j - 1].second)
          swap(vec[i][j], vec[i][j - 1]);
      }
    }
  }
  ll ans = 0;
  for (int i = 0; i < 2 * sz; i += 2) {
    int x = 0, y = 0;
    while (x < vec[i].size() && y < vec[i + 1].size()) {
      if (vec[i][x].second < vec[i + 1][y].second)
        x++;
      else if (vec[i][x].second > vec[i + 1][y].second)
        y++;
      else {
        ans += vec[i][x].first * vec[i + 1][y].first % mod * (i >> 1) % mod *
               (vec[i][x].second >> 1);
        ans %= mod;
        x++;
        y++;
      }
    }
  }
  cout << ans << '\\n';
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <stdio.h>
#include <string>
#include <time.h>
#include <tuple>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define all(a) a.begin(), a.end()
#define rep(i, n) for (ll i = 0; i < (n); i++)
#define pb push_back
#define debug(x) cerr << #x << ':' << (x) << '\\n'
#pragma GCC optimize("O3")
#pragma GCC optimize("unroll-loops")
using namespace std;
typedef long long ll;
typedef unsigned long long ull;
typedef long double ld;
typedef pair<ll, ll> P;
typedef complex<ld> com;
constexpr int inf = 1000000010;
constexpr ll INF = 1000000000000000010;
constexpr ld eps = 1e-12;
constexpr ld pi = 3.141592653589793238;
template <class T, class U> inline bool chmax(T &a, const U &b) {
  if (a < b) {
    a = b;
    return true;
  }
  return false;
}
template <class T, class U> inline bool chmin(T &a, const U &b) {
  if (a > b) {
    a = b;
    return true;
  }
  return false;
}

constexpr ll mod = 1000000007;

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  cout << fixed << setprecision(20);

  int h;
  cin >> h;
  int sz = 1 << (h - 1);
  vector<int> p(sz);
  rep(i, sz) {
    cin >> p[i];
    p[i]--;
  }
  vector<vector<ll>> prod(sz, vector<ll>(h - 1));
  rep(i, sz) {
    ll x = i + sz;
    ll tmp = x;
    rep(j, h - 1) {
      prod[i][j] = tmp;
      x >>= 1;
      tmp *= x;
      tmp %= mod;
    }
  }
  vector<vector<P>> vec(2 * sz, vector<P>());
  int bor = 1;
  int cap = 0;
  rep(i, 2 * sz) {
    if (i >= bor) {
      bor <<= 1;
      cap++;
    }
    int x = 1 << (h - cap);
    x *= (h - 1);
    if (i >= 2)
      vec[i].reserve(x);
  }
  rep(step, h - 1) {
    for (int i = sz - 1; i >= 0; i--) {
      int x = i + sz;
      int y = p[i] + sz;
      x >>= step;
      int idx = y;
      rep(j, h - 1) {
        if (!vec[idx].empty() && vec[idx].back().second == x) {
          vec[idx].back().first += prod[i][step] * prod[p[i]][j];
          vec[idx].back().first %= mod;
        } else
          vec[idx].pb({prod[i][step] * prod[p[i]][j] % mod, x});
        idx >>= 1;
      }
    }
  }
  rep(i, 2 * sz) {
    reverse(all(vec[i]));
    if (i & 1) {
      for (int j = 0; j < vec[i].size(); j++) {
        vec[i][j].second ^= 1;
        if (j > 0 && vec[i][j].second < vec[i][j - 1].second)
          swap(vec[i][j], vec[i][j - 1]);
      }
    }
  }
  ll ans = 0;
  for (int i = 0; i < 2 * sz; i += 2) {
    int x = 0, y = 0;
    while (x < vec[i].size() && y < vec[i + 1].size()) {
      if (vec[i][x].second < vec[i + 1][y].second)
        x++;
      else if (vec[i][x].second > vec[i + 1][y].second)
        y++;
      else {
        ans += vec[i][x].first * vec[i + 1][y].first % mod * (i >> 1) % mod *
               (vec[i][x].second >> 1);
        ans %= mod;
        x++;
        y++;
      }
    }
  }
  cout << ans << '\\n';
}