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

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
// #pragma GCC optimize ("trapv")
using namespace std;

#define pii pair<int, int>
#define pll pair<long long, long long>
#define fi first
#define se second
#define mp make_pair
#define pb push_back

#define DEBUG 1
#define cerr                                                                   \\
  if (DEBUG)                                                                   \\
  cerr
#define test cerr << "hi\\n";

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
#define rand(l, r) uniform_int_distribution<long long>(l, r)(rng)
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    OST;

#define INF 0x3f3f3f3f3f3f3f3f
#define MAXN 1000006
// #define MOD 998244353LL
#define MOD 1000000007LL

int p[MAXN];
int h, l;
long long ans = 0;
long long sum[MAXN];
vector<int> changed;
long long root;

void dfs2(int x, long long acc = 1) {
  if (x >= l) {
    //        cerr << "transition " << x << '\\n';
    (acc *= x) %= MOD;
    x = p[x];
    while (x > 1) {
      (acc *= x) %= MOD;
      (ans += root * acc % MOD * sum[x ^ 1] % MOD * (x / 2) % MOD) %= MOD;
      x /= 2;
    }
  } else {
    dfs2(x << 1, acc * x % MOD);
    dfs2(x << 1 | 1, acc * x % MOD);
  }
}

void dfs1(int x, long long acc = 1) {
  if (x >= l) {
    //        cerr << "transition " << x << '\\n';
    (acc *= x) %= MOD;
    x = p[x];
    while (x > 1) {
      (acc *= x) %= MOD;
      changed.pb(x);
      (sum[x] += acc) %= MOD;
      x /= 2;
    }
  } else {
    dfs1(x << 1, acc * x % MOD);
    dfs1(x << 1 | 1, acc * x % MOD);
  }
}

void work(int x) {
  root = x;
  dfs1(x << 1, 1);
  dfs2(x << 1 | 1, 1);

  for (int x : changed)
    sum[x] = 0;
  changed.clear();
}

void solve() {
  cin >> h;
  l = 1 << (h - 1);
  for (int i = l; i < l * 2; i++) {
    cin >> p[i];
    p[i] = l + p[i] - 1;
  }
  for (int i = 1; i < l; i++)
    work(i);
  cout << ans << '\\n';
}

int main() {
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
  //    freopen("output.txt", "w", stdout);
  auto start_time = clock();
#endif
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  int t = 1;
  //    cin >> t;
  for (int no = 1; no <= t; no++) {
    //        cout << "Case #" << no << ": ";
    solve();
  }

#ifdef LOCAL
  cerr << "TIME ELAPSED: " << double(clock() - start_time) / CLOCKS_PER_SEC
       << '\\n';
#endif
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
// #pragma GCC optimize ("trapv")
using namespace std;

#define pii pair<int, int>
#define pll pair<long long, long long>
#define fi first
#define se second
#define mp make_pair
#define pb push_back

#define DEBUG 1
#define cerr                                                                   \\
  if (DEBUG)                                                                   \\
  cerr
#define test cerr << "hi\\n";

mt19937_64 rng(chrono::steady_clock::now().time_since_epoch().count());
#define rand(l, r) uniform_int_distribution<long long>(l, r)(rng)
using namespace __gnu_pbds;
typedef tree<int, null_type, less<int>, rb_tree_tag,
             tree_order_statistics_node_update>
    OST;

#define INF 0x3f3f3f3f3f3f3f3f
#define MAXN 1000006
// #define MOD 998244353LL
#define MOD 1000000007LL

int p[MAXN];
int h, l;
long long ans = 0;
long long sum[MAXN];
vector<int> changed;
long long root;

void dfs2(int x, long long acc = 1) {
  if (x >= l) {
    (acc *= x) %= MOD;
    x = p[x];
    while (x > 1) {
      (acc *= x) %= MOD;
      //            cerr << "dfs2 " << x << ' ' << acc << '\\n';
      (ans += root * acc % MOD * sum[x ^ 1] % MOD * (x / 2) % MOD) %= MOD;
      x /= 2;
    }
  } else {
    dfs2(x << 1, acc * x % MOD);
    dfs2(x << 1 | 1, acc * x % MOD);
  }
}

void dfs1(int x, long long acc = 1) {
  if (x >= l) {
    (acc *= x) %= MOD;
    x = p[x];
    while (x > 1) {
      (acc *= x) %= MOD;
      changed.pb(x);
      //            cerr << "dfs1 " << x << ' ' << acc << '\\n';
      (sum[x] += acc) %= MOD;
      x /= 2;
    }
  } else {
    //        cerr << "dfs " << x << ' ' << acc << '\\n';
    dfs1(x << 1, acc * x % MOD);
    dfs1(x << 1 | 1, acc * x % MOD);
  }
}

void work(int x) {
  root = x;
  dfs1(x << 1, 1);
  dfs2(x << 1 | 1, 1);

  for (int x : changed)
    sum[x] = 0;
}

void solve() {
  cin >> h;
  l = 1 << (h - 1);
  for (int i = l; i < l * 2; i++) {
    cin >> p[i];
    p[i] = l + p[i] - 1;
  }
  //    work(2);
  for (int i = 1; i < l; i++)
    work(i);
  cout << ans << '\\n';
}

int main() {
#ifdef LOCAL
  freopen("input.txt", "r", stdin);
  //    freopen("output.txt", "w", stdout);
  auto start_time = clock();
#endif
  ios_base::sync_with_stdio(0);
  cin.tie(0);

  int t = 1;
  //    cin >> t;
  for (int no = 1; no <= t; no++) {
    //        cout << "Case #" << no << ": ";
    solve();
  }

#ifdef LOCAL
  cerr << "TIME ELAPSED: " << double(clock() - start_time) / CLOCKS_PER_SEC
       << '\\n';
#endif
}