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

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define For(i, n, k) for (int i = (n); i < (k); i++)
#define ALL(a) (a).begin(), (a).end()
int d = 365, n = 26, score = 0;
int c[27];
int s[366][27];
int sat[366] = {};
int last[27] = {};
vector<int> a(366, 0);
ofstream fout("out.txt"), flog("log.txt");
uint64_t get_rand_range(uint64_t min_val, uint64_t max_val) {

  srand((int)time(NULL));
  static mt19937_64 mt64(rand());

  uniform_int_distribution<uint64_t> get_rand_uni_int(min_val, max_val);

  return get_rand_uni_int(mt64);
}

int calcdiff_update(int t, int q, vector<int> &a) {
  int p = a[t];
  int lp = 0, rp = d + 1, lq = 0, rq = d + 1;
  For(i, 1, d + 1) {
    if (a[i] == p) {
      if (i < t)
        lp = max(lp, i);
      else if (i > t)
        rp = min(rp, i);
    }
    if (a[i] == q) {
      if (i < t)
        lq = max(lq, i);
      else if (i > t)
        rq = min(rq, i);
    }
  }
  int diff = s[t][q] - s[t][p];
  diff -= (t - lp) * (rp - t) * c[p];
  diff += (t - lq) * (rq - t) * c[q];
  return diff;
}

int calcdiff_swap(int l, int r, vector<int> &a) {
  int p = a[l], q = a[r];
  if (p == q)
    return 0;
  int diff = calcdiff_update(l, q, a);
  a[l] = q;
  diff += calcdiff_update(r, p, a);
  a[l] = p;
  return diff;
}

void greedy() {
  For(i, 1, d + 1) {
    int mx = -(1 << 30), action = -1;
    For(j, 1, n + 1) {
      sat[i] = sat[i - 1] + s[i][j];
      For(k, 1, n + 1) {
        if (k != j)
          sat[i] -= (i - last[k]) * c[k];
      }
      if (mx < sat[i]) {
        mx = sat[i];
        action = j;
      }
    }
    last[action] = i;
    sat[i] = mx;
    a[i] = action;
  }
  score = sat[d];
}

void Main() {
  clock_t start = clock();
  cin >> d;
  For(i, 1, n + 1) cin >> c[i];
  For(i, 1, d + 1) For(j, 1, n + 1) cin >> s[i][j];
  greedy();
  priority_queue<pair<int, vector<int>>, vector<pair<int, vector<int>>>,
                 greater<pair<int, vector<int>>>>
      que;
  que.push(make_pair(score, a));
  double time = static_cast<double>(clock() - start) / CLOCKS_PER_SEC * 1000.0;
  while (time < 1000) {
    int t = get_rand_range(1, d);
    int q = get_rand_range(1, n);
    priority_queue<pair<int, vector<int>>, vector<pair<int, vector<int>>>,
                   greater<pair<int, vector<int>>>>
        nque;
    while (!que.empty()) {
      auto p = que.top();
      que.pop();
      nque.push(p);
      int diff = calcdiff_update(t, q, p.second);
      p.second[t] = q;
      p.first += diff;
      nque.push(p);
    }
    while (nque.size() > 8)
      nque.pop();
    swap(nque, que);
    time = static_cast<double>(clock() - start) / CLOCKS_PER_SEC * 1000.0;
  }
  while (time < 1970) {
    int range = 1 + (1970 - time) / 40;
    int l = get_rand_range(1, d);
    int r = get_rand_range(1, range);
    if (l + r <= d)
      r += l;
    else
      r = l - r;
    priority_queue<pair<int, vector<int>>, vector<pair<int, vector<int>>>,
                   greater<pair<int, vector<int>>>>
        nque;
    while (!que.empty()) {
      auto p = que.top();
      que.pop();
      nque.push(p);
      int diff = calcdiff_swap(l, r, p.second);
      swap(p.second[l], p.second[r]);
      p.first += diff;
      nque.push(p);
    }
    while (nque.size() > 4)
      nque.pop();
    swap(nque, que);
    time = static_cast<double>(clock() - start) / CLOCKS_PER_SEC * 1000.0;
  }
  while (que.size() > 1)
    que.pop();
  auto p = que.top();
  For(i, 1, d + 1) cout << p.second[i] << endl;
}
int main() {
  Main();
  /*
  東方風神録は神が出てくるので当然神ゲー
  */
  return 0;
}

Error: -6
double free or corruption (out)


Fixed submission:

#include <bits/stdc++.h>
using namespace std;
typedef long long ll;
#define For(i, n, k) for (int i = (n); i < (k); i++)
#define ALL(a) (a).begin(), (a).end()
int d = 365, n = 26, score = 0;
int c[27];
int s[366][27];
int sat[366] = {};
int last[27] = {};
vector<int> a(366, 0);
int range = 8;
ofstream fout("out.txt"), flog("log.txt");
uint64_t get_rand_range(uint64_t min_val, uint64_t max_val) {

  srand((int)time(NULL));
  static mt19937_64 mt64(rand());

  uniform_int_distribution<uint64_t> get_rand_uni_int(min_val, max_val);

  return get_rand_uni_int(mt64);
}

int calcdiff_update(int t, int q, vector<int> &a) {
  int p = a[t];
  int lp = 0, rp = d + 1, lq = 0, rq = d + 1;
  For(i, 1, d + 1) {
    if (a[i] == p) {
      if (i < t)
        lp = max(lp, i);
      else if (i > t)
        rp = min(rp, i);
    }
    if (a[i] == q) {
      if (i < t)
        lq = max(lq, i);
      else if (i > t)
        rq = min(rq, i);
    }
  }
  int diff = s[t][q] - s[t][p];
  diff -= (t - lp) * (rp - t) * c[p];
  diff += (t - lq) * (rq - t) * c[q];
  return diff;
}

int calcdiff_swap(int l, int r, vector<int> &a) {
  int p = a[l], q = a[r];
  if (p == q)
    return 0;
  int diff = calcdiff_update(l, q, a);
  a[l] = q;
  diff += calcdiff_update(r, p, a);
  a[l] = p;
  return diff;
}

void greedy() {
  For(i, 1, d + 1) {
    int mx = -(1 << 30), action = -1;
    For(j, 1, n + 1) {
      sat[i] = sat[i - 1] + s[i][j];
      For(k, 1, n + 1) {
        if (k != j)
          sat[i] -= (i - last[k]) * c[k];
      }
      if (mx < sat[i]) {
        mx = sat[i];
        action = j;
      }
    }
    last[action] = i;
    sat[i] = mx;
    a[i] = action;
  }
  score = sat[d];
}

void Main() {
  clock_t start = clock();
  cin >> d;
  For(i, 1, n + 1) cin >> c[i];
  For(i, 1, d + 1) For(j, 1, n + 1) cin >> s[i][j];
  greedy();
  priority_queue<pair<int, vector<int>>, vector<pair<int, vector<int>>>,
                 greater<pair<int, vector<int>>>>
      que;
  que.push(make_pair(score, a));
  double time = static_cast<double>(clock() - start) / CLOCKS_PER_SEC * 1000.0;
  while (time < 1000) {
    int t = get_rand_range(1, d);
    int q = get_rand_range(1, n);
    priority_queue<pair<int, vector<int>>, vector<pair<int, vector<int>>>,
                   greater<pair<int, vector<int>>>>
        nque;
    while (!que.empty()) {
      auto p = que.top();
      que.pop();
      nque.push(p);
      int diff = calcdiff_update(t, q, p.second);
      p.second[t] = q;
      p.first += diff;
      nque.push(p);
    }
    while (nque.size() > range)
      nque.pop();
    swap(nque, que);
    time = static_cast<double>(clock() - start) / CLOCKS_PER_SEC * 1000.0;
  }
  while (time < 1970) {
    int range = 1 + (1970 - time) / 40;
    int l = get_rand_range(1, d);
    int r = get_rand_range(1, range);
    if (l + r <= d)
      r += l;
    else
      r = l - r;
    priority_queue<pair<int, vector<int>>, vector<pair<int, vector<int>>>,
                   greater<pair<int, vector<int>>>>
        nque;
    while (!que.empty()) {
      auto p = que.top();
      que.pop();
      nque.push(p);
      int diff = calcdiff_swap(l, r, p.second);
      swap(p.second[l], p.second[r]);
      p.first += diff;
      nque.push(p);
    }
    while (nque.size() > range / 2)
      nque.pop();
    swap(nque, que);
    time = static_cast<double>(clock() - start) / CLOCKS_PER_SEC * 1000.0;
  }
  while (que.size() > 1)
    que.pop();
  auto p = que.top();
  For(i, 1, d + 1) cout << p.second[i] << endl;
}
int main() {
  Main();
  /*
  東方風神録は神が出てくるので当然神ゲー
  */
  return 0;
}