Problem p02944 (C++)

Buggy submission (status: Runtime Error):
#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define ld double

#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()

#define pb emplace_back
#define X first
#define Y second

const int N = 2e5 + 5;

typedef pair<int, int> ii;

int a[N], t[N];
int f1[N], f2[N];
int s1[N], s2[N];

int pre[N];
int nxt[N];

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  int n;
  cin >> n;
  int L;
  cin >> L;

  priority_queue<ii, vector<ii>, greater<ii>> pq;

  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    f1[i] = f2[i] = 1;
    pre[i] = i - 1;
    nxt[i] = i + 1;

    pq.push(ii(a[i], i));
  }
  nxt[n] = 0;

  int tot = n;
  ll ans = 0;

  while (pq.size()) {
    int p = pq.top().Y;
    int v = pq.top().X;

    pq.pop();

    if (t[p])
      continue;

    int l = p;
    int r = p;

    while (a[pre[l]] == a[l])
      l = pre[l];
    while (a[nxt[r]] == a[r])
      r = nxt[r];

    int cnt = 0;
    int prv = 0;

    for (int i = l; i != nxt[r]; t[i] = 1, i = nxt[i]) {
      cnt++;

      if (i <= n)
        prv = cnt;

      s1[cnt] = s1[cnt - 1] + f1[i];
      s2[cnt] = s2[cnt - 1] + f2[i];

      if (prv && cnt >= L)
        ans += 1ll * f2[i] * s1[min(prv, cnt - L + 1)];
    }
    if (cnt < L) {
      nxt[pre[l]] = 0;
      pre[nxt[r]] = 0;
      continue;
    }
    int S = cnt / L;

    for (int i = 1; i < S; ++i) {
      nxt[tot + i] = 1 + tot + i;
      pre[tot + i + 1] = tot + i;
    }
    for (int i = 1; i <= S; ++i)
      a[tot + i] = a[p] + 1;

    nxt[pre[l]] = tot + 1;
    pre[tot + 1] = pre[l];
    pre[nxt[r]] = tot + S;
    nxt[tot + S] = nxt[r];

    for (int i = 1; i <= S; ++i) {
      int j = S - i + 1;

      f2[tot + i] = s2[min((i + 1) * L - 1, cnt)] - s2[i * L - 1];
      f1[tot + i] = s1[cnt - j * L + 1] - s1[max(cnt - j * L - L + 1, 0)];
    }
    pq.push(ii(a[tot + 1], tot + 1));
    tot += S;
  }
  cout << ans + n << endl;
}

Fixed submission:
#include <bits/stdc++.h>

using namespace std;

#define ll long long
#define ld double

#define sz(x) (int)x.size()
#define all(x) x.begin(), x.end()

#define pb emplace_back
#define X first
#define Y second

const int N = 5e6 + 5;

typedef pair<int, int> ii;

int a[N], t[N];
int f1[N], f2[N];
int s1[N], s2[N];

int pre[N];
int nxt[N];

int main() {
  ios_base::sync_with_stdio(0);
  cin.tie(0);
  cout.tie(0);

  int n;
  cin >> n;
  int L;
  cin >> L;

  priority_queue<ii, vector<ii>, greater<ii>> pq;

  for (int i = 1; i <= n; ++i) {
    cin >> a[i];
    f1[i] = f2[i] = 1;
    pre[i] = i - 1;
    nxt[i] = i + 1;

    pq.push(ii(a[i], i));
  }
  nxt[n] = 0;

  int tot = n;
  ll ans = 0;

  while (pq.size()) {
    int p = pq.top().Y;
    int v = pq.top().X;

    pq.pop();

    if (t[p])
      continue;

    int l = p;
    int r = p;

    while (a[pre[l]] == a[l])
      l = pre[l];
    while (a[nxt[r]] == a[r])
      r = nxt[r];

    int cnt = 0;
    int prv = 0;

    for (int i = l; i != nxt[r]; t[i] = 1, i = nxt[i]) {
      cnt++;

      if (i <= n)
        prv = cnt;

      s1[cnt] = s1[cnt - 1] + f1[i];
      s2[cnt] = s2[cnt - 1] + f2[i];

      if (prv && cnt >= L)
        ans += 1ll * f2[i] * s1[min(prv, cnt - L + 1)];
    }
    if (cnt < L) {
      nxt[pre[l]] = 0;
      pre[nxt[r]] = 0;
      continue;
    }
    int S = cnt / L;

    for (int i = 1; i < S; ++i) {
      nxt[tot + i] = 1 + tot + i;
      pre[tot + i + 1] = tot + i;
    }
    for (int i = 1; i <= S; ++i)
      a[tot + i] = a[p] + 1;

    nxt[pre[l]] = tot + 1;
    pre[tot + 1] = pre[l];
    pre[nxt[r]] = tot + S;
    nxt[tot + S] = nxt[r];

    for (int i = 1; i <= S; ++i) {
      int j = S - i + 1;

      f2[tot + i] = s2[min((i + 1) * L - 1, cnt)] - s2[i * L - 1];
      f1[tot + i] = s1[cnt - j * L + 1] - s1[max(cnt - j * L - L + 1, 0)];
    }
    pq.push(ii(a[tot + 1], tot + 1));
    tot += S;
  }
  cout << ans + n << endl;
}