Problem p02618 (C++) — buggy submission, verdict: Time Limit Exceeded

/**
 * @brief atcoder
 * @author yao
 */
#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <utility>
#include <vector>
#define ft first
#define sd second
#ifdef DBG
#define dbg_pri(x...) fprintf(stderr, x)
#else
#define dbg_pri(x...) 0
#define NDEBUG
#endif // DBG
#include <cassert>

typedef unsigned int uint;
typedef long long int lli;
typedef unsigned long long int ulli;

#define N 512
#define M 32
#define K (1048576 * 12)
#define T (512 * 2)

int c[M];
int s[N][M];
int t[N];
std::vector<int> poi[M];

constexpr int n = 365, m = 26;

int count() {
  for (int i = 0; i < m; ++i)
    poi[i] = {-1};
  int res = 0;
  for (int i = 0; i < n; ++i) {
    int l = poi[t[i]].back(), d = i - l;
    poi[t[i]].push_back(i);
    res += s[i][t[i]];
    res -= c[t[i]] * d * (d - 1) / 2;
  }
  for (int i = 0; i < m; ++i) {
    int l = poi[i].back(), d = n - l;
    poi[i].push_back(n);
    res -= c[i] * d * (d - 1) / 2;
  }
  return res;
}

int update(int x, int k) {
  int pk = t[x];
  int res = s[x][k] - s[x][pk];
  std::vector<int>::iterator itl, it, itr;
  it = std::find(poi[pk].begin(), poi[pk].end(), x), itl = std::prev(it),
  itr = std::next(it);
  res -= c[pk] * (x - *itl) * (*itr - x);
  poi[pk].erase(it);
  itr = std::upper_bound(poi[k].begin(), poi[k].end(), x), itl = std::prev(itr);
  res += c[k] * (x - *itl) * (*itr - x);
  poi[k].insert(itr, x);
  t[x] = k;
  return res;
}

int main() {
  int d;
  scanf("%d", &d);
  assert(d == n);
  for (int i = 0; i < m; ++i)
    scanf("%d", &c[i]);
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j)
      scanf("%d", &s[i][j]);
  for (int i = 0; i < n; ++i)
    t[i] = rand() % m;
  int val = count();
#ifdef DBG
  int init_val = val;
  int ca = 0, cb = 0;
#endif // DBG
  for (int i = 0; i < K; ++i) {
    int x = rand() % n, k = rand() % m;
    int pre_t = t[x];
    int nxt_v = val + update(x, k);
    if (nxt_v >= val ||
        rand() <= (int)(exp((double)(nxt_v - val) / T) * ((lli)RAND_MAX + 1))) {
#ifdef DBG
      (nxt_v < val ? ca : cb)++;
#endif // DBG
      val = nxt_v;
    } else
      update(x, pre_t);
  }
  for (int i = 0; i < n; ++i)
    printf("%d\\n", t[i] + 1);
  dbg_pri("poi 3: init=%d val=%d %d %d\\n", init_val, val, ca, cb);
  return 0;
}


Error: TLE

Fixed submission:

/**
 * @brief atcoder
 * @author yao
 */
#include <algorithm>
#include <cctype>
#include <climits>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <functional>
#include <utility>
#include <vector>
#define ft first
#define sd second
#ifdef DBG
#define dbg_pri(x...) fprintf(stderr, x)
#else
#define dbg_pri(x...) 0
#define NDEBUG
#endif // DBG
#include <cassert>

typedef unsigned int uint;
typedef long long int lli;
typedef unsigned long long int ulli;

#define N 512
#define M 32
#define K (1048576 * 16)
#define T (512 * 2)

int c[M];
int s[N][M];
int t[N];
std::vector<int> poi[M];

constexpr int n = 365, m = 26;

int count() {
  for (int i = 0; i < m; ++i)
    poi[i] = {-1};
  int res = 0;
  for (int i = 0; i < n; ++i) {
    int l = poi[t[i]].back(), d = i - l;
    poi[t[i]].push_back(i);
    res += s[i][t[i]];
    res -= c[t[i]] * d * (d - 1) / 2;
  }
  for (int i = 0; i < m; ++i) {
    int l = poi[i].back(), d = n - l;
    poi[i].push_back(n);
    res -= c[i] * d * (d - 1) / 2;
  }
  return res;
}

int update(int x, int k) {
  int pk = t[x];
  int res = s[x][k] - s[x][pk];
  std::vector<int>::iterator itl, it, itr;
  it = std::find(poi[pk].begin(), poi[pk].end(), x), itl = std::prev(it),
  itr = std::next(it);
  res -= c[pk] * (x - *itl) * (*itr - x);
  poi[pk].erase(it);
  itr = std::upper_bound(poi[k].begin(), poi[k].end(), x), itl = std::prev(itr);
  res += c[k] * (x - *itl) * (*itr - x);
  poi[k].insert(itr, x);
  t[x] = k;
  return res;
}

int main() {
  int d;
  scanf("%d", &d);
  assert(d == n);
  for (int i = 0; i < m; ++i)
    scanf("%d", &c[i]);
  for (int i = 0; i < n; ++i)
    for (int j = 0; j < m; ++j)
      scanf("%d", &s[i][j]);
  for (int i = 0; i < n; ++i)
    t[i] = rand() % m;
  int val = count();
#ifdef DBG
  int init_val = val;
  int ca = 0, cb = 0;
#endif // DBG
  for (int i = 0; i < K; ++i) {
    int x = rand() % n, k = rand() % m;
    int pre_t = t[x];
    int nxt_v = val + update(x, k);
    if (nxt_v >= val ||
        rand() <= (int)(exp((double)(nxt_v - val) / T) * ((lli)RAND_MAX + 1))) {
#ifdef DBG
      (nxt_v < val ? ca : cb)++;
#endif // DBG
      val = nxt_v;
    } else
      update(x, pre_t);
  }
  for (int i = 0; i < n; ++i)
    printf("%d\\n", t[i] + 1);
  dbg_pri("poi 3: init=%d val=%d %d %d\\n", init_val, val, ca, cb);
  return 0;
}