Problem p02618 (C++) — buggy submission, verdict: Runtime Error

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using namespace std;
using ll = int64_t;
using ld = long double;
using P = pair<int, int>;
using vs = vector<string>;
using vi = vector<ll>;
using vvi = vector<vi>;
template <class T> using PQ = priority_queue<T>;
template <class T> using PQG = priority_queue<T, vector<T>, greater<T>>;
const int INF = 100010001;
const ll LINF = (ll)INF * INF * 10;
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  return a < b && (a = b, true);
}
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  return a > b && (a = b, true);
}
template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
  return is >> p.first >> p.second;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  return os << p.first << ' ' << p.second;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int D;
  cin >> D;
  vi c(26);
  rep(i, 26) cin >> c[i];
  vvi s(D, vi(26));
  rep(i, D) rep(j, 26) { cin >> s[i][j]; }

  vi t(D);
  rep(i, D) { t[i] = i % 26; }
  vi last(26);
  vector<set<int>> se(26);
  rep(i, 26) { se[i].insert(D); }
  ll gh = 0;
  rep(d, D) {
    se[t[d]].insert(d);
    gh += s[d][t[d]];
    last[t[d]] = d + 1;
    rep(j, 26) { gh -= c[j] * (d + 1 - last[j]); }
  }

  vvi bits(26, vi(D + 1));
  auto add = [&](int al, int d, int x) {
    while (d <= D) {
      bits[al][d] += x;
      d += d & -d;
    }
  };
  auto sum = [&](int al, int d) {
    if (d == 0)
      return 0;
    int res = 0;
    while (d) {
      res += bits[al][d];
      d &= d - 1;
    }
    return res;
  };

  rep(i, D) {
    rep(j, 26) { add(j, i + 1, 1); }
  }

  rep(i, D) {
    int now = sum(t[i], i + 1);
    add(t[i], i + 1, -now);
  }

  int X = 100000;
  while (X--) {
    int nowd = s[rand() % D][rand() % 26] % D;
    int nowal = s[rand() % D][rand() % 26] % 26;
    if (t[nowd] == nowal)
      continue;
    ll nowgh = gh;
    nowgh += s[nowd][nowal] - s[nowd][t[nowd]];
    auto itr = se[nowal].lower_bound(nowd);
    nowgh += (*itr - nowd) * sum(nowal, nowd + 1) * c[nowd];
    itr = se[t[nowd]].upper_bound(nowd);
    nowgh -= (*itr - nowd) * (sum(t[nowd], nowd) + 1) * c[t[nowd]];
    if (nowgh > gh) {
      add(t[nowd], nowd + 1, sum(t[nowd], nowd) + 1);
      add(nowal, nowd + 1, -sum(nowal, nowd + 1));
      se[t[nowd]].erase(nowd);
      se[nowal].insert(nowd);
      t[nowd] = nowal;
      gh = nowgh;
    }
  }

  rep(i, D) { cout << t[i] + 1 << endl; }
}

Error: WA

Fixed submission:

// #define _GLIBCXX_DEBUG
#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; ++i)
#define all(v) v.begin(), v.end()
#define rall(v) v.rbegin(), v.rend()
using namespace std;
using ll = int64_t;
using ld = long double;
using P = pair<int, int>;
using vs = vector<string>;
using vi = vector<int>;
using vvi = vector<vi>;
template <class T> using PQ = priority_queue<T>;
template <class T> using PQG = priority_queue<T, vector<T>, greater<T>>;
const int INF = 100010001;
const ll LINF = (ll)INF * INF * 10;
template <typename T1, typename T2> inline bool chmax(T1 &a, T2 b) {
  return a < b && (a = b, true);
}
template <typename T1, typename T2> inline bool chmin(T1 &a, T2 b) {
  return a > b && (a = b, true);
}
template <typename T1, typename T2>
istream &operator>>(istream &is, pair<T1, T2> &p) {
  return is >> p.first >> p.second;
}
template <typename T1, typename T2>
ostream &operator<<(ostream &os, const pair<T1, T2> &p) {
  return os << p.first << ' ' << p.second;
}

int main() {
  ios::sync_with_stdio(false);
  cin.tie(0);
  int D;
  cin >> D;
  vi c(26);
  rep(i, 26) cin >> c[i];
  vvi s(D, vi(26));
  rep(i, D) rep(j, 26) { cin >> s[i][j]; }

  vi t(D);
  rep(i, D) { t[i] = i; }
  vi last(26);
  vector<set<int>> se(26);
  rep(i, 26) { se[i].insert(D); }
  int gh = 0;
  rep(d, D) {
    se[t[d]].insert(d);
    gh += s[d][t[d]];
    last[t[d]] = d + 1;
    rep(j, 26) { gh -= c[j] * (d + 1 - last[j]); }
  }

  vvi bits(26, vi(D + 1));
  auto add = [&](int al, int d, int x) {
    while (d <= D) {
      bits[al][d] += x;
      d += d & -d;
    }
  };
  auto sum = [&](int al, int d) {
    if (d == 0)
      return 0;
    int res = 0;
    while (d) {
      res += bits[al][d];
      d &= d - 1;
    }
    return res;
  };

  rep(i, D) {
    rep(j, 26) { add(j, i + 1, 1); }
  }

  rep(i, D) {
    int now = sum(t[i], i + 1);
    add(t[i], i + 1, -now);
  }

  int X = 100000;
  while (X--) {
    int nowd = s[rand() % D][rand() % 26] % D;
    int nowal = s[rand() % D][rand() % 26] % 26;
    if (t[nowd] == nowal)
      continue;
    int nowgh = gh;
    nowgh += s[nowd][nowal] - s[nowd][t[nowd]];
    auto itr = se[nowal].lower_bound(nowd);
    nowgh += (*itr - nowd) * sum(nowal, nowd + 1) * c[nowd];
    itr = se[t[nowd]].upper_bound(nowd);
    nowgh -= (*itr - nowd) * (sum(t[nowd], nowd) + 1) * c[t[nowd]];
    if (nowgh > gh) {
      add(t[nowd], nowd + 1, sum(t[nowd], nowd) + 1);
      add(nowal, nowd, -sum(nowal, nowd + 1));
      se[t[nowd]].erase(nowd);
      se[nowal].insert(nowd);
      t[nowd] = nowal;
    }
  }

  rep(i, D) { cout << t[i] + 1 << endl; }
}