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

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define sz(x) int(x.size())
#define pb push_back
#define eb emplace_back
using ll = long long;
using P = pair<int, int>;
using LP = pair<ll, int>;
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
const ll MOD = 1000000007, MOD2 = 998244353;

int D;
vector<int> c(26);
vector<vector<int>> s;
vector<int> spa(26, 0); // 経過日数
vector<int> t;
vector<set<int>> schedule(26); // 各コンテストが何日目に開催されるか
ll contest(int day,
           int x) { // day日にコンテストxを開催した時の満足度の増加量 1-indexed
  ll now = s.at(day - 1).at(x - 1);
  rep(i, 26) {
    if (i == x - 1) {
      spa.at(i) = 0;
      continue;
    }
    spa.at(i)++;
    now -= spa.at(i) * c.at(i);
  }
  return now;
}

int main() {
  std::chrono::system_clock::time_point start, end; // 型は auto で可
  start = std::chrono::system_clock::now();

  cin >> D;
  rep(i, 26) cin >> c.at(i);
  s.resize(D, vector<int>(26));
  rep(i, D) rep(j, 26) { cin >> s.at(i).at(j); }
  t.resize(D);

  // rep(i,D)cin>>t.at(i);
  rep(i, D) { t.at(i) = rand() % 26 + 1; }

  rep(i, 26) { schedule.at(i).insert(0); }
  rep(i, D) { schedule.at(t.at(i) - 1).insert(i + 1); }
  rep(i, 26) { schedule.at(i).insert(D + 1); }
  ll ans = 0;
  rep(i, D) {
    ans += contest(i + 1, t.at(i));
    // cout<<ans<<endl;問題A用
  }
  // 改善前のansがもとまっている

  double elapsed;

  while (elapsed < 1800) { // 時間が許す限り
    int d, q;
    // cin>>d>>q;
    d = rand() % D + 1;
    q = rand() % 26 + 1;
    ll oldans = ans;

    int x = t.at(d - 1);
    t.at(d - 1) = q;
    int bx, nx;
    auto p =
        lower_bound(schedule.at(x - 1).begin(), schedule.at(x - 1).end(), d);
    p--;
    bx = *p;
    p++;
    p++;
    nx = *p;
    schedule.at(x - 1).erase(d);
    int bq, nq;
    auto itr =
        lower_bound(schedule.at(q - 1).begin(), schedule.at(q - 1).end(), d);
    nq = *itr;
    itr--;
    bq = *itr;
    schedule.at(q - 1).insert(d);
    ans += s.at(d - 1).at(q - 1) - s.at(d - 1).at(x - 1);
    ans += (d - bq) * (nq - d) * c.at(q - 1);
    ans -= (d - bx) * (nx - d) * c.at(x - 1);
    // cout<<ans<<endl;
    if (ans < oldans) {
      schedule.at(x - 1).insert(d);
      schedule.at(q - 1).erase(d);
      ans = oldans;
    }
    end = std::chrono::system_clock::now(); // 計測終了時間
    elapsed = std::chrono::duration_cast<std::chrono::milliseconds>(end - start)
                  .count();
  }
  rep(i, D) { cout << t.at(i) << endl; }
}

Error: -6
terminate called after throwing an instance of 'std::out_of_range'
  what():  vector::_M_range_check: __n (which is 69395745) >= this->size() (which is 26)


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define rep(i, n) for (int i = 0; i < (n); i++)
#define sz(x) int(x.size())
#define pb push_back
#define eb emplace_back
using ll = long long;
using P = pair<int, int>;
using LP = pair<ll, int>;
#define chmax(x, y) x = max(x, y)
#define chmin(x, y) x = min(x, y)
const ll MOD = 1000000007, MOD2 = 998244353;

int D;
vector<int> c(26);
vector<vector<int>> s;
vector<int> spa(26, 0); // 経過日数
vector<int> t;
vector<set<int>> schedule(26); // 各コンテストが何日目に開催されるか
ll contest(int day,
           int x) { // day日にコンテストxを開催した時の満足度の増加量 1-indexed
  ll now = s.at(day - 1).at(x - 1);
  rep(i, 26) {
    if (i == x - 1) {
      spa.at(i) = 0;
      continue;
    }
    spa.at(i)++;
    now -= spa.at(i) * c.at(i);
  }
  return now;
}

int main() {
  cin >> D;
  rep(i, 26) cin >> c.at(i);
  s.resize(D, vector<int>(26));
  rep(i, D) rep(j, 26) { cin >> s.at(i).at(j); }
  t.resize(D);

  // rep(i,D)cin>>t.at(i);
  rep(i, D) { t.at(i) = rand() / 26 + 1; }

  rep(i, 26) { schedule.at(i).insert(0); }
  rep(i, D) { schedule.at(t.at(i) - 1).insert(i + 1); }
  rep(i, 26) { schedule.at(i).insert(D + 1); }
  ll ans = 0;
  rep(i, D) {
    ans += contest(i + 1, t.at(i));
    // cout<<ans<<endl;問題A用
  }
  cout << ans << endl;
}