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

#include <bits/stdc++.h>
using namespace std;
#define FO(x)                                                                  \\
  {                                                                            \\
    freopen(#x ".in", "r", stdin);                                             \\
    freopen(#x ".out", "w", stdout);                                           \\
  }
#define pa pair<int, int>
#define mod 1000000007
#define ll long long
#define mk make_pair
#define pb push_back
#define fi first
#define se second
#define cl(x) memset(x, 0, sizeof x)
#ifdef Devil_Gary
#define bug(x) cout << (#x) << " " << (x) << endl
#define debug(...) fprintf(stderr, __VA_ARGS__)
#else
#define bug(x)
#define debug(...)
#endif

const int INF = 0x7fffffff;
const int N = 6e5 + 5;
/*
char *TT,*mo,but[(1<<15)+2];
#define getchar()
((TT==mo&&(mo=(TT=but)+fread(but,1,1<<15,stdin),TT==mo))?-1:*TT++)//*/
inline int read() {
  int x = 0, rev = 0, ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      rev = 1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = (x << 1) + (x << 3) + ch - '0';
    ch = getchar();
  }
  return rev ? -x : x;
} /*
 struct Edge{
         int nxt,v;
 //	int w;
 //	int id;
 }e[N<<1];
 void add(int u,int v){
         e[++tot].v=v,e[tot].nxt=head[u],head[u]=tot;
         e[++tot].v=u,e[tot].nxt=head[v],head[v]=tot;
 }*/
ll n, m, ans, a[N], t[N], val[N];
void add(int l, int r, int op, int pos) {
  //	bug(1);
  // cerr<<l<<" "<<r<<endl;
  ll ret, tt, p2;
  for (int i = l; i <= r; i++) {
    tt = 0, ret = 1, p2 = a[i] + (n / 2) - 1;
    //		bug(p2),bug(val[i]);
    while (p2) {
      //			bug(p2);
      if (!op)
        (t[p2] += val[i] * ret) %= mod;
      else
        (ans += (t[p2] - tt + mod) * ret % mod * val[i] % mod * p2 % mod * pos %
                mod) %= mod,
            (tt = t[p2] * p2 % mod) %= mod;
      ret = ret * p2 % mod, p2 >>= 1;
    }
  }
}
void clear(int l, int r) {
  for (int i = l; i <= r; i++) {
    int p2 = a[i] + (n / 2) - 1;
    while (p2) {
      t[p2] = 0, p2 >>= 1;
    }
  }
}
void dfs(int pos, int l, int r) {
  if (l == r) {
    val[l] = 1LL;
    //		bug(1);
    //		bug(pos);
    return;
  }

  int mid = l + r >> 1;
  dfs(pos << 1, l, mid), dfs(pos << 1 | 1, mid + 1, r);

  for (int i = l; i <= mid; i++)
    val[i] = val[i] * (pos << 1) % mod;
  for (int i = mid + 1; i <= r; i++)
    val[i] = val[i] * (pos << 1 | 1) % mod;

  add(l, mid, 0, pos), add(mid + 1, r, 1, pos);
  clear(l, r);
  //	for(int i=l;i<=mid;i++)
}
int main() {
#ifdef Devil_Gary
  freopen("in.txt", "r", stdin);
//  freopen("out.txt","w",stdout);
#endif
  m = read(), n = pow(2, m);
  for (int i = 1; i <= n / 2; i++)
    a[i] = read();
  dfs(1, 1, n / 2);
  cout << ans << endl;
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define FO(x)                                                                  \\
  {                                                                            \\
    freopen(#x ".in", "r", stdin);                                             \\
    freopen(#x ".out", "w", stdout);                                           \\
  }
#define pa pair<int, int>
#define mod 1000000007
#define ll long long
#define mk make_pair
#define pb push_back
#define fi first
#define se second
#define cl(x) memset(x, 0, sizeof x)
#ifdef Devil_Gary
#define bug(x) cout << (#x) << " " << (x) << endl
#define debug(...) fprintf(stderr, __VA_ARGS__)
#else
#define bug(x)
#define debug(...)
#endif

const int INF = 0x7fffffff;
const int N = 6e5 + 5;
/*
char *TT,*mo,but[(1<<15)+2];
#define getchar()
((TT==mo&&(mo=(TT=but)+fread(but,1,1<<15,stdin),TT==mo))?-1:*TT++)//*/
inline int read() {
  int x = 0, rev = 0, ch = getchar();
  while (ch < '0' || ch > '9') {
    if (ch == '-')
      rev = 1;
    ch = getchar();
  }
  while (ch >= '0' && ch <= '9') {
    x = (x << 1) + (x << 3) + ch - '0';
    ch = getchar();
  }
  return rev ? -x : x;
} /*
 struct Edge{
         int nxt,v;
 //	int w;
 //	int id;
 }e[N<<1];
 void add(int u,int v){
         e[++tot].v=v,e[tot].nxt=head[u],head[u]=tot;
         e[++tot].v=u,e[tot].nxt=head[v],head[v]=tot;
 }*/
ll n, m, ans, a[N], t[N], val[N];
void add(int l, int r, int op, int pos) {
  //	bug(1);
  // cerr<<l<<" "<<r<<endl;
  ll ret = 1, tt, p2;
  for (int i = l; i <= r; i++) {
    tt = 0, ret = 1, p2 = a[i] + (n / 2) - 1;
    //		bug(p2),bug(val[i]);
    while (p2) {
      //			bug(p2);
      if (!op)
        (t[p2] += val[i] * ret) %= mod;
      else
        (ans += (t[p2] - tt + mod) * ret % mod * val[i] % mod * p2 % mod * pos %
                mod) %= mod,
            (tt = t[p2] * p2 % mod) %= mod;
      ;
      ret = ret * p2 % mod, p2 >>= 1;
    }
  }
}
void dfs(int pos, int l, int r) {
  if (l == r) {
    val[l] = 1LL;
    //		bug(pos);
    return;
  }

  int mid = l + r >> 1;
  dfs(pos << 1, l, mid), dfs(pos << 1 | 1, mid + 1, r);

  for (int i = l; i <= mid; i++)
    val[i] = val[i] * (pos << 1) % mod;
  for (int i = mid + 1; i <= r; i++)
    val[i] = val[i] * (pos << 1 | 1) % mod;

  add(l, mid, 0, pos), add(mid + 1, r, 1, pos);
  memset(t, 0, sizeof t);
}
int main() {
#ifdef Devil_Gary
  freopen("in.txt", "r", stdin);
//  freopen("out.txt","w",stdout);
#endif
  m = read(), n = pow(2, m);
  for (int i = 1; i <= n / 2; i++)
    a[i] = read();
  dfs(1, 1, n / 2);
  cout << ans << endl;
  return 0;
}