alexjercan/bugnet huggingface dataset:
Problem p02589 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int cnt[26];
string v[200010];
int nxt[1000010][26];
bool ok[1000010];
int num;
ll ans = 0;
string s;

void buildtree(int node, int k) {
  if (k == s.size()) {
    ok[node] = 1;
    return;
  }
  if (nxt[node][s[k] - 'a'] == 0) {
    nxt[node][s[k] - 'a'] = ++num;
  }
  buildtree(nxt[node][s[k] - 'a'], k + 1);
}

void dfs(int node, int k) {
  // cout << s << " " << node << " " << k << endl;
  if (k == s.size() - 1) {
    cnt[s[k] - 'a']--;
    return;
  }

  for (int i = 0; i < 26; i++) {
    if (cnt[i] == 0)
      continue;
    if (ok[nxt[node][i]]) {
      ans++;
      // cout << s << " " << node << " " << k << endl;
    }
  }

  cnt[s[k] - 'a']--;
  dfs(nxt[node][s[k] - 'a'], k + 1);
}

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    string ss;
    cin >> ss;
    reverse(ss.begin(), ss.end());
    v[i] = ss;
  }

  sort(v, v + n);

  for (int i = 0; i < n; i++) {
    s = v[i];
    buildtree(0, 0);
  }

  for (int i = 0; i < n; i++) {
    s = v[i];
    for (int j = 0; j < s.size(); j++) {
      cnt[s[j] - 'a']++;
    }
    dfs(0, 0);
  }

  cout << ans << endl;

  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;

int cnt[26];
vector<string> v;
int nxt[1000010][26];
bool ok[1000010];
int num;
ll ans = 0;

void buildtree(int node, string s, int k) {
  if (k == s.size()) {
    ok[node] = 1;
    return;
  }
  if (nxt[node][s[k] - 'a'] == 0) {
    nxt[node][s[k] - 'a'] = ++num;
  }
  buildtree(nxt[node][s[k] - 'a'], s, k + 1);
}

void dfs(int node, string s, int k) {
  // cout << s << " " << node << " " << k << endl;
  if (k == s.size() - 1) {
    return;
  }

  for (int i = 0; i < 26; i++) {
    if (cnt[i] == 0)
      continue;
    if (ok[nxt[node][i]])
      ans++;
  }

  cnt[s[k]]--;
  dfs(nxt[node][s[k] - 'a'], s, k + 1);
}

int main() {
  int n;
  cin >> n;
  for (int i = 0; i < n; i++) {
    string ss;
    cin >> ss;
    reverse(ss.begin(), ss.end());
    v.push_back(ss);
  }

  sort(v.begin(), v.end());

  for (int i = 0; i < v.size(); i++) {
    string s = v[i];
    buildtree(0, s, 0);
  }

  for (int i = 0; i < v.size(); i++) {
    string s = v[i];
    for (int j = 0; j < s.size(); j++) {
      cnt[s[j] - 'a']++;
    }
    dfs(0, s, 0);
  }

  cout << ans << endl;

  return 0;
}
Problem p02589 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <algorithm>
#include <iostream>
using namespace std;
#define maxn 200010
#define ll long long

int n, len[maxn], t[maxn];
string s[maxn];
struct node {
  int t[26];
  node *son[26];
  node() {
    for (int i = 0; i < 26; i++)
      t[i] = 0, son[i] = NULL;
  }
} *root = new node();
void insert(string &S, int &l) {
  for (int i = 0; i < l; i++)
    t[S[i] - 'a']++;
  node *now = root;
  for (int i = l - 1; i >= 0; i--) {
    for (int j = 0; j < 26; j++)
      if (t[j])
        now->t[j]++;
    if (!now->son[S[i] - 'a'])
      now->son[S[i] - 'a'] = new node();
    now = now->son[S[i] - 'a'];
    t[S[i] - 'a']--;
  }
}
ll ans = 0;

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> s[i];
    len[i] = s[i].length();
    insert(s[i], len[i]);
  }
  for (int i = 1; i <= n; i++) {
    node *now = root;
    for (int j = len[i] - 1; j >= 1; j--)
      now = now->son[s[i][j] - 'a'];
    ans += now->t[s[i][0] - 'a'] - 1;
  }
  cout << ans;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <iostream>
#include <map>
using namespace std;
#define maxn 200010
#define mod 9824353998244357ll
#define ll long long

int n, len[maxn], t[maxn];
string s[maxn];
map<ll, int> mp;
ll ans = 0;

int main() {
  cin >> n;
  for (int i = 1; i <= n; i++) {
    cin >> s[i];
    len[i] = s[i].length();
    for (int j = 0; j < len[i]; j++)
      t[s[i][j] - 'a' + 1]++;
    ll now = 0;
    for (int j = len[i] - 1; j >= 0; j--) {
      for (int k = 1; k <= 26; k++)
        if (t[k])
          mp[(now * 37ll + k) % mod]++;
      now = (now * 27ll + s[i][j] - 'a' + 1) % mod;
      t[s[i][j] - 'a' + 1]--;
    }
  }
  for (int i = 1; i <= n; i++) {
    ll now = 0;
    for (int j = len[i] - 1; j >= 1; j--)
      now = (now * 27ll + s[i][j] - 'a' + 1) % mod;
    now = (now * 37ll + s[i][0] - 'a' + 1) % mod;
    ans += mp[now] - 1;
  }
  cout << ans;
}
Problem p02589 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

#define fi first
#define se second

const int N = 200100;
const int Q = 1000100;

using namespace std;

int n;
string s[N];
int G;
int t[Q][26];
int f[Q];
bool good[Q][26];

int main() {
  ios_base::sync_with_stdio(0);

  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);

  cin >> n;
  vector<int> ord;
  for (int i = 1; i <= n; i++) {
    cin >> s[i];
    reverse(s[i].begin(), s[i].end());
    ord.push_back(i);
  }
  sort(ord.begin(), ord.end(),
       [&](int x, int y) { return s[x].size() < s[y].size(); });
  long long res = 0;
  for (int i : ord) {
    int cur = 0;
    for (int h = 0; h < 26; h++) {
      good[s[i].size()][h] = false;
    }
    for (int j = s[i].size() - 1; j >= 0; j--) {
      for (int h = 0; h < 26; h++) {
        good[j][h] = good[j + 1][h];
      }
      good[j][s[i][j] - 'a'] = true;
    }
    for (int j = 0, g; j < s[i].size(); j++) {
      for (int h = 0; h < 26; h++) {
        if (good[j][h] && t[cur][h]) {
          res += f[t[cur][h]];
        }
      }
      g = s[i][j] - 'a';
      if (!t[cur][g]) {
        t[cur][g] = ++G;
      }
      cur = t[cur][g];
    }
    f[cur] += 1;
  }
  cout << res << "\\n";
}


Error: -11

Fixed submission:

#include <bits/stdc++.h>

#define fi first
#define se second

const int N = 100100;
const int Q = 1000100;

using namespace std;

int n;
string s[N];
int G;
int t[Q][26];
int f[Q];
bool good[Q][26];

int main() {
  ios_base::sync_with_stdio(0);

  // freopen("input.txt", "r", stdin);
  // freopen("output.txt", "w", stdout);

  cin >> n;
  vector<int> ord;
  for (int i = 1; i <= n; i++) {
    cin >> s[i];
    reverse(s[i].begin(), s[i].end());
    ord.push_back(i);
  }
  sort(ord.begin(), ord.end(),
       [&](int x, int y) { return s[x].size() < s[y].size(); });
  long long res = 0;
  for (int i : ord) {
    int cur = 0;
    for (int h = 0; h < 26; h++) {
      good[s[i].size()][h] = false;
    }
    for (int j = s[i].size() - 1; j >= 0; j--) {
      for (int h = 0; h < 26; h++) {
        good[j][h] = good[j + 1][h];
      }
      good[j][s[i][j] - 'a'] = true;
    }
    for (int j = 0, g; j < s[i].size(); j++) {
      for (int h = 0; h < 26; h++) {
        if (good[j][h] && t[cur][h]) {
          res += f[t[cur][h]];
        }
      }
      g = s[i][j] - 'a';
      if (!t[cur][g]) {
        t[cur][g] = ++G;
      }
      cur = t[cur][g];
    }
    f[cur] += 1;
  }
  cout << res << "\\n";
}
Problem p02589 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int M = 1e6 + 50;
int n, ch[M][26], cnt, num[M][26], pp[M], l;
char s[M];
bool fl[M], hav[26];
LL ans;
void insert() {
  int p = 0;
  for (int i = 1; i <= l; i++) {
    int c = s[i] - 'a';
    if (!ch[p][c])
      ch[p][c] = ++cnt;
    p = ch[p][c];
    pp[i] = p;
  }
  fl[p] = 1;
  memset(hav, 0, sizeof(hav));
  for (int i = l; i; i--) {
    hav[s[i] - 'a'] = 1;
    for (int j = 0; j < 26; j++)
      if (hav[j])
        num[pp[i - 1]][j]++;
  }
}
void dfs(int x) {
  for (int i = 0, y; i < 26; i++)
    if ((y = ch[x][i])) {
      dfs(y); // cout<<num[x][i]<<" ";
      if (fl[y])
        ans += num[x][i] - 1;
    }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%s", s + 1);
    l = strlen(s + 1);
    reverse(s + 1, s + l + 1);
    insert();
  }
  dfs(0);
  cout << ans;
  return 0;
}

Error: -11

Fixed submission:

#include <bits/stdc++.h>
#define LL long long
using namespace std;
const int N = 2e5 + 50, M = 1e6 + 50;
int n, ch[M][26], cnt, num[N][26], sz[N], pp[N], l;
char s[M];
bool fl[N], hav[26];
LL ans;
void insert() {
  int p = 0;
  for (int i = 1; i <= l; i++) {
    int c = s[i] - 'a';
    if (!ch[p][c])
      ch[p][c] = ++cnt;
    p = ch[p][c];
    sz[p]++;
    pp[i] = p;
  }
  fl[p] = 1;
  memset(hav, 0, sizeof(hav));
  for (int i = l; i; i--) {
    hav[s[i] - 'a'] = 1;
    for (int j = 0; j < 26; j++)
      if (hav[j])
        num[pp[i - 1]][j]++;
  }
}
void dfs(int x) {
  for (int i = 0, y; i < 26; i++)
    if ((y = ch[x][i])) {
      dfs(y); // cout<<num[x][i]<<" ";
      if (fl[y])
        ans += num[x][i] - 1;
    }
}
int main() {
  scanf("%d", &n);
  for (int i = 1; i <= n; i++) {
    scanf("%s", s + 1);
    l = strlen(s + 1);
    reverse(s + 1, s + l + 1);
    insert();
  }
  dfs(0);
  cout << ans;
  return 0;
}
Problem p02589 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;

const int M = 1e6 + 10;
const int N = 2e5 + 10;

int n;
char ch[M];

pair<int, int> a[N];
int g[M][26];

bool cmp(pair<int, int> a, pair<int, int> b) { return a.first < b.first; }

int tr[M][26], rt = 1, num[M], cnt = 1;

void Ins(int st, int len) {
  int now = rt;
  for (int i = 0; i < len; i++) {
    int j = ch[st + i] - 'a';
    if (!tr[now][j])
      tr[now][j] = ++cnt;
    now = tr[now][j];
  }
  num[now]++;
}

int main() {
  cin >> n;
  int hav = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%s", ch + hav);
    int len = strlen(ch + hav);
    reverse(ch + hav, ch + hav + len);
    a[i].first = len;
    a[i].second = hav;
    hav += len;
  }
  sort(a + 1, a + n + 1, cmp);
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    int st = a[i].second, len = a[i].first;
    for (int j = 0; j < 26; j++)
      g[len][j] = 0;
    for (int j = len - 1; j >= 0; j--) {
      for (int k = 0; k < 26; k++)
        g[j][k] = g[j + 1][k];
      g[j][ch[st + j] - 'a'] = 1;
    }
    int now = rt;
    for (int j = 0; j < len; j++) {
      for (int k = 0; k < 26; k++) {
        if (!g[j][k] || !tr[now][k])
          continue;
        ans += num[tr[now][k]];
      }
      now = tr[now][ch[st + j] - 'a'];
      if (!now)
        break;
    }
    Ins(st, len);
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

const int M = 1e6 + 10;
const int N = 2e5 + 10;

int n;
char ch[M];

pair<int, int> a[N];
int g[M][26];

unordered_map<unsigned long long, int> f;

bool cmp(pair<int, int> a, pair<int, int> b) { return a.first < b.first; }

int main() {
  cin >> n;
  int hav = 0;
  for (int i = 1; i <= n; i++) {
    scanf("%s", ch + hav);
    int len = strlen(ch + hav);
    reverse(ch + hav, ch + hav + len);
    a[i].first = len;
    a[i].second = hav;
    hav += len;
  }
  sort(a + 1, a + n + 1, cmp);
  long long ans = 0;
  for (int i = 1; i <= n; i++) {
    unsigned long long S = 0;
    int st = a[i].second, len = a[i].first;
    for (int j = 0; j < 26; j++)
      g[len][j] = 0;
    for (int j = len - 1; j >= 0; j--) {
      for (int k = 0; k < 26; k++)
        g[j][k] = g[j + 1][k];
      g[j][ch[st + j] - 'a'] = 1;
    }
    for (int j = 0; j < len; j++) {
      for (int k = 0; k < 26; k++) {
        if (!g[j][k])
          continue;
        unsigned long long now = S * 131 + 'a' + k;
        ans += f[now];
      }
      S = S * 131 + ch[st + j];
    }
    f[S]++;
  }
  cout << ans << endl;
}
Problem p02589 (C++) — buggy submission, verdict: Memory Limit Exceeded

#include <map>
#include <stdio.h>
#include <string.h>
#include <vector>

#define N 200200
#define M 1000100
#define ll long long

typedef struct trie {
  int cnt[26]{};
  int nxt[26]{};
} trie;

std::vector<trie> tree;

int acnt[30];
int gc = 1;

int insert(std::vector<int> &astr) {
  int akt = 0;
  int anxt = 0;
  int res = 0;
  for (int i = 0; i < astr.size(); i++) {
    if (i == astr.size() - 1)
      res = akt;
    int ac = astr[i];
    if (tree[akt].nxt[ac]) {
      anxt = tree[akt].nxt[ac];
    } else {
      trie tmp;
      tree.push_back(tmp);
      tree[akt].nxt[ac] = gc;
      anxt = gc;
      gc++;
    };
    for (int j = 0; j < 30; j++) {
      if (acnt[j]) {
        tree[akt].cnt[j]++;
      };
    };
    acnt[ac]--;
    akt = anxt;
  };
  return res;
};

char str[M];

std::vector<int> nstr;
int lst[N];
std::vector<int> rr;

int main(void) {
  int n;
  scanf("%d", &n);
  trie tt;
  tree.push_back(tt);
  for (int i = 0; i < n; i++) {
    scanf("%s", str);
    int m = strlen(str);
    nstr.clear();
    for (int j = 0; j < 30; j++)
      acnt[j] = 0;
    for (int j = m - 1; j >= 0; j--) {
      acnt[str[j] - 'a']++;
      nstr.push_back(str[j] - 'a');
    };
    rr.push_back(insert(nstr));
    lst[i] = nstr[nstr.size() - 1];
  };
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    int node = rr[i];
    int ch = lst[i];
    ans += (tree[node].cnt[ch] - 1);
  };
  printf("%lld\\n", ans);
  return 0;
};


Error: MLE

Fixed submission:

#include <map>
#include <stdio.h>
#include <string.h>
#include <vector>

#define N 200200
#define M 1000100
#define ll long long

typedef struct trie {
  std::map<char, int> cnt;
  std::map<char, int> nxt;
} trie;

std::vector<trie> tree;

int acnt[30];
int gc = 1;

int insert(std::vector<int> &astr) {
  int akt = 0;
  int anxt = 0;
  int res = 0;
  for (int i = 0; i < astr.size(); i++) {
    if (i == astr.size() - 1)
      res = akt;
    int ac = astr[i];
    if (tree[akt].nxt.count(ac)) {
      anxt = tree[akt].nxt[ac];
    } else {
      trie tmp;
      tree.push_back(tmp);
      tree[akt].nxt[ac] = gc;
      anxt = gc;
      gc++;
    };
    for (int j = 0; j < 30; j++) {
      if (acnt[j]) {
        if (tree[akt].cnt.count(j)) {
          tree[akt].cnt[j]++;
        } else {
          tree[akt].cnt[j] = 1;
        };
      };
    };
    acnt[ac]--;
    akt = anxt;
  };
  return res;
};

char str[M];

std::vector<int> nstr;
int lst[N];
std::vector<int> rr;

int main(void) {
  int n;
  scanf("%d", &n);
  trie tt;
  tree.push_back(tt);
  for (int i = 0; i < n; i++) {
    scanf("%s", str);
    int m = strlen(str);
    nstr.clear();
    for (int j = 0; j < 30; j++)
      acnt[j] = 0;
    for (int j = m - 1; j >= 0; j--) {
      acnt[str[j] - 'a']++;
      nstr.push_back(str[j] - 'a');
    };
    rr.push_back(insert(nstr));
    lst[i] = nstr[nstr.size() - 1];
  };
  ll ans = 0;
  for (int i = 0; i < n; i++) {
    int node = rr[i];
    int ch = lst[i];
    ans += (tree[node].cnt[ch] - 1);
  };
  printf("%lld\\n", ans);
  return 0;
};
Problem p02589 (C++) — buggy submission, verdict: Runtime Error

#include <bits/stdc++.h>

typedef long long LL;

const int MAXN = 2e5 + 10, MAXL = 1e6 + 10;

int n;
int tmp[30];
std::string s[MAXN];

template <int N, int S> struct Trie {
  int tot;
  int ed[N], ch[N][S];
  inline void insert(const std::string &s) {
    int u = 0;
    for (const auto &cur : s) {
      int v = cur - 'a';
      if (!ch[u][v])
        ch[u][v] = ++tot;
      u = ch[u][v];
    }
    ++ed[u];
  }
};
Trie<MAXL, 26> T;

int main() {
  std::ios::sync_with_stdio(0);
  std::cin.tie(0);
  std::cin >> n;
  for (int i = 1; i <= n; ++i) {
    std::cin >> s[i];
    std::reverse(s[i].begin(), s[i].end());
    T.insert(s[i]);
  }
  LL ans = 0;
  for (int i = 1; i <= n; ++i) {
    for (int j = 0; j < 26; ++j)
      tmp[j] = 0;
    for (const auto &cur : s[i])
      ++tmp[cur - 'a'];
    int u = 0;
    for (const auto &cur : s[i]) {
      int v = cur - 'a';
      for (int j = 0; j < 26; ++j)
        tmp[j] && T.ch[u][j] && (ans += T.ed[T.ch[u][j]]);
      --tmp[v], u = T.ch[u][v];
    }
  }
  std::cout << ans - n << '\\n';
  return 0;
}

Error: WA

Fixed submission:

#include <bits/stdc++.h>

typedef long long LL;

const int MAXN = 2e5 + 10, MAXL = 1e6 + 10;

int n;
int tmp[30];
std::string s[MAXN];

template <int N, int S> struct Trie {
  int tot;
  int ed[N], ch[N][S];
  inline void insert(const std::string &s) {
    int u = 0;
    for (const auto &cur : s) {
      int v = cur - 'a';
      if (!ch[u][v])
        ch[u][v] = ++tot;
      u = ch[u][v];
    }
    ++ed[u];
  }
};
Trie<MAXN, 26> T;

int main() {
  std::ios::sync_with_stdio(false);
  std::cin.tie(NULL);
  scanf("%d", &n);
  for (int i = 1; i <= n; ++i) {
    std::cin >> s[i];
    std::reverse(s[i].begin(), s[i].end());
    T.insert(s[i]);
  }
  LL ans = 0;
  for (int i = 1; i <= n; ++i) {
    for (int j = 0; j < 26; ++j)
      tmp[j] = 0;
    for (const auto &cur : s[i])
      ++tmp[cur - 'a'];
    int u = 0;
    for (const auto &cur : s[i]) {
      int v = cur - 'a';
      for (int j = 0; j < 26; ++j)
        tmp[j] && T.ch[u][j] && (ans += T.ed[T.ch[u][j]]);
      --tmp[v], u = T.ch[u][v];
    }
  }
  printf("%lld\\n", ans - n);
  return 0;
}
Problem p02590 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>

#define int long long
#define db(a, ...) cerr << #a << " = " << a << '\\n'
#define db2(a, b) cerr << #a << " = " << a << ", ", db(b)
#define db3(a, b, c) cerr << #a << " = " << a << ", ", db2(b, c)
#define db4(a, b, c, d) cerr << #a << " = " << a << ", ", db3(b, c, d)

using namespace std;

const int MOD = 200003;

using cd = complex<double>;
const double PI = acos(-1);

void fft(vector<cd> &a, bool invert) {
  int n = a.size();

  for (int i = 1, j = 0; i < n; i++) {
    int bit = n >> 1;
    for (; j & bit; bit >>= 1)
      j ^= bit;
    j ^= bit;

    if (i < j)
      swap(a[i], a[j]);
  }

  for (int len = 2; len <= n; len <<= 1) {
    double ang = 2 * PI / len * (invert ? -1 : 1);
    cd wlen(cos(ang), sin(ang));
    for (int i = 0; i < n; i += len) {
      cd w(1);
      for (int j = 0; j < len / 2; j++) {
        cd u = a[i + j], v = a[i + j + len / 2] * w;
        a[i + j] = u + v;
        a[i + j + len / 2] = u - v;
        w *= wlen;
      }
    }
  }

  if (invert) {
    for (cd &x : a)
      x /= n;
  }
}

vector<int> multiply(vector<int> const &a, vector<int> const &b) {
  vector<cd> fa(a.begin(), a.end()), fb(b.begin(), b.end());
  int n = 1;
  while (n < a.size() + b.size())
    n <<= 1;
  fa.resize(n);
  fb.resize(n);

  fft(fa, false);
  fft(fb, false);
  for (int i = 0; i < n; i++)
    fa[i] *= fb[i];
  fft(fa, true);

  vector<int> result(n);
  for (int i = 0; i < n; i++)
    result[i] = round(fa[i].real());
  return result;
}

int n, M[MOD];

int mul(int x, int k) {
  if (k == 0)
    return 1;
  int tmp = mul(x, k / 2);
  if (k % 2 == 0)
    return tmp * tmp % MOD;
  return tmp * tmp % MOD * x % MOD;
}

int32_t main() {
#ifdef ACM
  freopen("A.in", "r", stdin);
#endif
  for (int i = 0; i < MOD - 1; i++)
    M[mul(2, i)] = i;
  cin >> n;
  int x, rs = 0;
  vector<int> V(MOD);
  while (n--) {
    cin >> x;
    if (x == 0)
      continue;
    rs -= x * x % MOD;
    V[M[x]]++;
  }
  vector<int> A = multiply(V, V);
  for (int i = 0; i < A.size(); i++)
    rs += mul(2, i) * A[i];
  cout << rs / 2;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#pragma GCC optimize("Ofast")
#pragma GCC target("avx,avx2,fma")

// naive with optimization

const long long mod = 2 * 1e5 + 3;
int n;
long long a[200003], sum = 0, ans = 0;

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cin >> n;
  for (int i = 0; i < n; i++)
    cin >> a[i];
  for (int i = 0; i < n; i++) {
    for (int j = i + 1; j < n; j++) {
      ans += (a[i] * a[j]) % mod;
    }
  }
  cout << ans << "\\n";
}
Problem p02591 (C++) — buggy submission, verdict: Time Limit Exceeded

#pragma GCC target("avx2")
#pragma GCC optimization("O3")
#pragma GCC optimization("unroll-loops")

#include <bits/stdc++.h>

using namespace std;

const int MX = 1 << 18, md = 1000000007;

unordered_map<int, int> here[18];
bool old[18];

int a[MX];

int main() {
  int h = 18;
  ignore = scanf("%d", &h);

  int n = 1 << (h - 1);
  for (int i = 0; i < n; i++)
    ignore = scanf("%d", a + i);
  //	srand(42);
  //	iota(a, a + n, 1);
  //	random_shuffle(a, a + n);

  int ans = 0;
  for (int i = 0; i < n; i++) {
    for (int u = n + i, p = 1, d = 0; u != 1; d++, u /= 2) {
      p = p * 1LL * u % md;

      if (u % 2 == 0) {
        if (old[d]) {
          here[d].clear();
          old[d] = false;
        }

        for (int v = n + a[i] - 1, q = p; v != 1; v /= 2) {
          q = q * 1LL * v % md;

          int &val = here[d][v];
          val += q;
          if (val >= md)
            val -= md;
        }
      } else {
        old[d] = true;

        for (int v = n + a[i] - 1, q = p; v != 1; v /= 2) {
          q = q * 1LL * v % md;

          auto it = here[d].find(v ^ 1);
          if (it != here[d].end()) {
            int there = it->second;
            ans += q * 1LL * there % md * (v / 2) % md * (u / 2) % md;
            if (ans >= md)
              ans -= md;
          }
        }
      }
    }
  }

  printf("%d\\n", ans);

  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>

using namespace std;

const int MX = 1 << 18, md = 1000000007;

unordered_map<int, int> here[18];
bool old[18];

int a[MX];

int main() {
  int h = 18;
  ignore = scanf("%d", &h);

  int n = 1 << (h - 1);
  for (int i = 0; i < n; i++)
    ignore = scanf("%d", a + i);
  //	srand(42);
  //	iota(a, a + n, 1);
  //	random_shuffle(a, a + n);

  int ans = 0;
  for (int i = 0; i < n; i++) {
    for (int u = n + i, p = 1, d = 0; u != 1; d++, u /= 2) {
      p = p * 1LL * u % md;

      if (u % 2 == 0) {
        if (old[d]) {
          here[d].clear();
          old[d] = false;
        }

        for (int v = n + a[i] - 1, q = p; v != 1; v /= 2) {
          q = q * 1LL * v % md;

          int &val = here[d][v];
          val += q;
          if (val >= md)
            val -= md;
        }
      } else {
        old[d] = true;

        for (int v = n + a[i] - 1, q = p; v != 1; v /= 2) {
          q = q * 1LL * v % md;

          auto it = here[d].find(v ^ 1);
          if (it != here[d].end()) {
            int there = it->second;
            ans += q * 1LL * there % md * (v / 2) % md * (u / 2) % md;
            if (ans >= md)
              ans -= md;
          }
        }
      }
    }
  }

  printf("%d\\n", ans);

  return 0;
}
Problem p02591 (C++) — buggy submission, verdict: Time Limit Exceeded

#include <bits/stdc++.h>
using namespace std;
using LL = long long;
constexpr LL mod = 1'000'000'007;
constexpr int maxn = (1 << 17) + 1;
int P[maxn];
LL mark[2][maxn];
vector<pair<int, LL>> nodes[2][maxn];
void add(LL &x, LL y) {
  x += y;
  if (x >= mod)
    x -= mod;
}
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int H;
  cin >> H;
  int M = 1 << (H - 1);
  for (int i = 1; i <= M; i += 1)
    cin >> P[i];
  for (int i = 1; i <= M; i += 1)
    for (LL u = i + M - 1, p = u; u > 1; u >>= 1) {
      nodes[u & 1][u >> 1].push_back({P[i] + M - 1, p});
      p = (u >> 1) * p % mod;
    }
  LL ans = 0;
  for (int i = 1; i < M; i += 1) {
    for (auto [u, p] : nodes[0][i])
      for (; u > 1; u >>= 1) {
        p = p * u % mod;
        add(mark[u & 1][u >> 1], p);
      }
    for (auto [u, p] : nodes[1][i])
      for (; u > 1; u >>= 1) {
        p = p * u % mod;
        add(ans,
            mark[(u & 1) ^ 1][u >> 1] * p % mod * i % mod * (u >> 1) % mod);
      }
    for (auto [u, _] : nodes[0][i])
      for (; u > 1; u >>= 1)
        mark[u & 1][u >> 1] = 0;
  }
  cout << ans;
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using LL = long long;
constexpr LL mod = 1'000'000'007;
constexpr int maxn = (1 << 17) + 1;
int P[maxn];
map<int, LL> mp[4][maxn];
vector<tuple<int, int, LL>> vp[maxn];
int main() {
  ios::sync_with_stdio(false);
  cin.tie(nullptr);
  cout.tie(nullptr);
  int H;
  cin >> H;
  int M = 1 << (H - 1);
  for (int i = 1; i <= M; i += 1) {
    cin >> P[i];
    for (LL u = i + M - 1, p = u; u > 1; u >>= 1) {
      vp[i].push_back({u >> 1, u & 1, p});
      p = (u >> 1) * p % mod;
    }
  }
  for (int i = 1; i <= M; i += 1)
    for (auto p : vp[i])
      for (auto q : vp[P[i]]) {
        auto [a, b, c] = p;
        auto [x, y, z] = q;
        LL &r = mp[(b << 1) | y][a][x];
        r = (r + c * z) % mod;
      }
  LL ans = 0;
  for (int k = 0; k < 2; k += 1)
    for (int i = 1; i < M; i += 1)
      for (auto [j, r] : mp[k][i])
        ans = (ans + mp[k ^ 3][i][j] * r % mod * i % mod * j % mod) % mod;
  cout << ans;
  return 0;
}