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

#include <bits/stdc++.h>
using namespace std;
using lint = long long;
constexpr lint inf = 1LL << 60;
constexpr lint mod = 1000000007;

struct Timer {
  chrono::system_clock::time_point start;
  int ms;
  Timer(int ms) : start(chrono::system_clock::now()), ms(ms) {}
  bool stop() {
    chrono::system_clock::time_point curr = chrono::system_clock::now();
    double t = static_cast<double>(
        chrono::duration_cast<chrono::microseconds>(curr - start).count() /
        1000.0);
    return t > ms;
  }
  int partial() {
    chrono::system_clock::time_point curr = chrono::system_clock::now();
    double t = static_cast<double>(
        chrono::duration_cast<chrono::microseconds>(curr - start).count() /
        1000.0);
    return 1000 * t / ms;
  }
};

using ulint = unsigned long long;
struct RandomNumberGenerator {
  mt19937_64 mt;
  RandomNumberGenerator()
      : mt(chrono::steady_clock::now().time_since_epoch().count()) {}
  ulint operator()(ulint a, ulint b) { // [a, b)
    uniform_int_distribution<ulint> dist(a, b - 1);
    return dist(mt);
  }
  ulint operator()(ulint b) { // [0, b)
    return (*this)(0, b);
  }
  ulint operator()() { return mt(); }
};

template <class T> bool chmax(T &a, const T &b) {
  return (a < b) ? (a = b, 1) : 0;
}
template <class T> bool chmin(T &a, const T &b) {
  return (b < a) ? (a = b, 1) : 0;
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  Timer t(1950);
  RandomNumberGenerator r;

  int d;
  cin >> d;
  vector<lint> c(26);
  for (int i = 0; i < 26; ++i) {
    cin >> c[i];
  }
  vector<vector<lint>> s(d, vector<lint>(26));
  lint maxi = 0;
  for (int i = 0; i < d; ++i) {
    for (int j = 0; j < 26; ++j) {
      cin >> s[i][j];
      maxi = max(maxi, s[i][j]);
    }
  }
  vector<lint> ret(d, -1);
  vector<lint> mx(d, -inf);

  vector<lint> l(26);
  for (int i = 0; i < d; ++i) {
    for (int j = 0; j < 26; ++j) {
      lint score = s[i][j] + c[j] * (i + 1 - l[j]);
      if (chmax(mx[i], score))
        ret[i] = j;
      l[ret[i]] = i + 1;
    }
  }
  lint score = 0;
  vector<vector<lint>> last(d, vector<lint>(26, 0));
  vector<lint> sc(26, 0);
  for (int i = 0; i < d; ++i) {
    if (i != 0) {
      for (int j = 0; j < 26; ++j) {
        last[i][j] = last[i - 1][j];
      }
    }
    sc[ret[i]] += s[i][ret[i]];
    last[i][ret[i]] = i + 1;
    for (int j = 0; j < 26; ++j) {
      sc[j] -= c[j] * (i + 1 - last[i][j]);
    }
  }
  for (int i = 0; i < 26; ++i) {
    score += sc[i];
  }

  maxi /= 8;

  cerr << score << endl;

  while (!t.stop()) {
    int p = t.partial(); // 0 - 1000
    double temp = double(maxi) * (1000 - p) / 1000;
    // bool swap = 1000 - p > rand;
    // cerr << 1000 - p << " " << rand << endl;
    for (int z = 0; z < 100; ++z) {
      int idx = r(d);
      int curr = ret[idx];
      vector<lint> sv(27, 0);
      for (int i = 1; i <= 26; ++i) {
        sv[i] = s[idx][i - 1] + (idx + 1 - last[idx][i - 1]) * c[i - 1];
      }
      for (int i = 1; i <= 26; ++i) {
        sv[i] += sv[i - 1];
      }
      lint ra = r(sv[26]);
      int newr = upper_bound(sv.begin(), sv.end(), ra) - 1 - sv.begin();
      if (curr == newr)
        continue;
      ret[idx] = newr;

      lint nscore = score;

      lint scurr = sc[curr];
      lint snew = sc[newr];

      nscore -= scurr + snew;
      sc[curr] = 0;
      sc[newr] = 0;
      vector<vector<lint>> lastt(d, vector<lint>(2, 0));
      for (int i = 0; i < d; ++i) {
        if (i) {
          lastt[i] = lastt[i - 1];
        }
        if (ret[i] == curr) {
          sc[curr] += s[i][curr];
          lastt[i][0] = i + 1;
        } else if (ret[i] == newr) {
          sc[newr] += s[i][newr];
          lastt[i][1] = i + 1;
        }
        sc[curr] -= c[curr] * (i + 1 - lastt[i][0]);
        sc[newr] -= c[newr] * (i + 1 - lastt[i][1]);
      }
      nscore += sc[curr] + sc[newr];

      // cerr << nscore << "\\n";
      if (nscore > score || exp((nscore - score) / temp) * 1000 > r(1000)) {
        score = nscore;
        for (int i = 0; i < d; ++i) {
          last[i][curr] = lastt[i][0];
          last[i][newr] = lastt[i][1];
        }
      } else { // revert
        ret[idx] = curr;
        sc[curr] = scurr;
        sc[newr] = snew;
      }
    }
  }
  cerr << score << endl;
  for (int i = 0; i < d; ++i) {
    cout << ret[i] + 1 << "\\n";
  }
  return 0;
}


Error: WA

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
using lint = long long;
constexpr lint inf = 1LL << 60;
constexpr lint mod = 1000000007;

struct Timer {
  chrono::system_clock::time_point start;
  int ms;
  Timer(int ms) : start(chrono::system_clock::now()), ms(ms) {}
  bool stop() {
    chrono::system_clock::time_point curr = chrono::system_clock::now();
    double t = static_cast<double>(
        chrono::duration_cast<chrono::microseconds>(curr - start).count() /
        1000.0);
    return t > ms;
  }
  int partial() {
    chrono::system_clock::time_point curr = chrono::system_clock::now();
    double t = static_cast<double>(
        chrono::duration_cast<chrono::microseconds>(curr - start).count() /
        1000.0);
    return 1000 * t / ms;
  }
};

using ulint = unsigned long long;
struct RandomNumberGenerator {
  mt19937_64 mt;
  RandomNumberGenerator()
      : mt(chrono::steady_clock::now().time_since_epoch().count()) {}
  ulint operator()(ulint a, ulint b) { // [a, b)
    uniform_int_distribution<ulint> dist(a, b - 1);
    return dist(mt);
  }
  ulint operator()(ulint b) { // [0, b)
    return (*this)(0, b);
  }
  ulint operator()() { return mt(); }
};

// borrowed from
// https://atcoder.jp/contests/rco-contest-2018-qual/submissions/2088742?lang=ja
class XorShift {
  unsigned int x, y, z, w, t;

public:
  XorShift() {
    x = 133553533;
    y = 314867339;
    z = 664298413;
    w = 999999937;
    t = 0;
  }
  unsigned int rand() {
    t = x ^ (x << 11);
    x = y;
    y = z;
    z = w;
    w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));
    return w & 0x7fffffff;
  }
  unsigned int operator()(int u) {
    t = x ^ (x << 11);
    x = y;
    y = z;
    z = w;
    w = (w ^ (w >> 19)) ^ (t ^ (t >> 8));
    return (w & 0x7fffffff) % u;
  }
};

template <class T> bool chmax(T &a, const T &b) {
  return (a < b) ? (a = b, 1) : 0;
}
template <class T> bool chmin(T &a, const T &b) {
  return (b < a) ? (a = b, 1) : 0;
}

int main() {
  cin.tie(nullptr);
  ios::sync_with_stdio(false);

  // RandomNumberGenerator r;
  XorShift r;
  int d;
  cin >> d;
  vector<lint> c(26);
  for (int i = 0; i < 26; ++i) {
    cin >> c[i];
  }
  vector<vector<lint>> s(d, vector<lint>(26));
  lint maxi = 0;
  for (int i = 0; i < d; ++i) {
    for (int j = 0; j < 26; ++j) {
      cin >> s[i][j];
      maxi = max(maxi, s[i][j]);
    }
  }

  Timer t(1970);
  vector<lint> ret(d, -1);
  vector<lint> mx(d, -1);

  vector<lint> l(26);
  for (int i = 0; i < d; ++i) {
    for (int j = 0; j < 26; ++j) {
      lint score = s[i][j] - c[j] * (i + 1 - l[j]);
      if (chmax(mx[i], score))
        ret[i] = j;
      l[ret[i]] = i + 1;
    }
  }
  lint score = 0;
  vector<vector<lint>> last(d, vector<lint>(26, 0));
  vector<lint> sc(26, 0);
  for (int i = 0; i < d; ++i) {
    if (i != 0) {
      for (int j = 0; j < 26; ++j) {
        last[i][j] = last[i - 1][j];
      }
    }
    sc[ret[i]] += s[i][ret[i]];
    last[i][ret[i]] = i + 1;
    for (int j = 0; j < 26; ++j) {
      sc[j] -= c[j] * (i + 1 - last[i][j]);
    }
  }
  for (int i = 0; i < 26; ++i) {
    score += sc[i];
  }

  maxi /= 16;

  int cnt = 0;
  while (!t.stop()) {
    int p = t.partial(); // 0 - 1000
    double temp = double(maxi) * (1000 - p) / 1000;
    // bool swap = 1000 - p > rand;
    // cerr << 1000 - p << " " << rand << endl;
    for (int z = 0; z < 100; ++z) {
      cnt++;
      int idx = r(d);
      int curr = ret[idx];
      vector<lint> sv(27, 0);
      for (int i = 1; i <= 26; ++i) {
        sv[i] = s[idx][i - 1] + (idx + 1 - last[idx][i - 1]) * c[i - 1];
      }
      for (int i = 1; i <= 26; ++i) {
        sv[i] += sv[i - 1];
      }
      lint ra = r(sv[26]);
      int newr = upper_bound(sv.begin(), sv.end(), ra) - 1 - sv.begin();
      if (curr == newr)
        continue;
      ret[idx] = newr;

      lint nscore = score;

      lint scurr = sc[curr];
      lint snew = sc[newr];

      nscore -= scurr + snew;
      sc[curr] = 0;
      sc[newr] = 0;
      vector<vector<lint>> lastt(d, vector<lint>(2, 0));
      for (int i = 0; i < d; ++i) {
        if (i) {
          lastt[i] = lastt[i - 1];
        }
        if (ret[i] == curr) {
          sc[curr] += s[i][curr];
          lastt[i][0] = i + 1;
        } else if (ret[i] == newr) {
          sc[newr] += s[i][newr];
          lastt[i][1] = i + 1;
        }
        sc[curr] -= c[curr] * (i + 1 - lastt[i][0]);
        sc[newr] -= c[newr] * (i + 1 - lastt[i][1]);
      }
      nscore += sc[curr] + sc[newr];

      // cerr << nscore << "\\n";
      if (nscore > score || exp((nscore - score) / temp) * 1000 > r(1000)) {
        score = nscore;
        for (int i = 0; i < d; ++i) {
          last[i][curr] = lastt[i][0];
          last[i][newr] = lastt[i][1];
        }
      } else { // revert
        ret[idx] = curr;
        sc[curr] = scurr;
        sc[newr] = snew;
      }
    }
  }
  // cerr << cnt << endl;
  // cerr << score << endl;
  for (int i = 0; i < d; ++i) {
    cout << ret[i] + 1 << "\\n";
  }
  return 0;
}