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

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <tuple>
#include <unistd.h>
#include <vector>
#define _USE_MATH_DEFINES
#define _GLIBCXX_DEBUG
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> plglg;
typedef pair<double, ll> pdlg;
typedef tuple<int, int, int> tiii;
typedef tuple<ll, ll, ll> tlglglg;
typedef tuple<ll, ll, ll, ll> tlglglglg;
typedef tuple<double, double, double> tddd;
typedef complex<double> xy_t;
typedef vector<ll> vll;
typedef vector<vector<ll>> matrix;
typedef unsigned int uint;
typedef unsigned long long ull;
template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;
template <class T> using VVV = V<VV<T>>;
#define REP(i, x, y) for (ll i = (ll)x; i < (ll)y; i++)
#define DREP(i, x, y, d) for (ll i = (ll)x; i < (ll)y; i += (ll)d)
#define PER(i, x, y) for (ll i = (ll)x; i > (ll)y; i--)
#define DPER(i, x, y, d) for (ll i = (ll)x; i > (ll)y; i -= (ll)d)
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
double pi = 3.141592653589793;
ll mod = 1000000007;
int intmax = 2147483647;
int intmin = -2147483648;
ll llmax = 9223372036854775807;
ll llmin = -9223372036854775807;
int iinf = intmax / 8;
ll inf = llmax / 8;
double eps = 1e-12;
void yesno(bool ok, string yes = "Yes", string no = "No") {
  if (ok) {
    cout << yes << endl;
  } else {
    cout << no << endl;
  }
}

ll D;
ll c[26];
ll s[365][26];
ll score[26];
ll last[26];
set<ll> st[26];

void initrand() { srand((ll)time(NULL)); }

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  initrand();
  cin >> D;
  REP(i, 0, 26) { cin >> c[i]; }
  REP(i, 0, D) {
    REP(j, 0, 26) { cin >> s[i][j]; }
  }
  REP(i, 0, 26) {
    st[i].insert(-1);
    st[i].insert(D);
  }
  ll t[D];
  REP(i, 0, D) {
    t[i] = i % 26;
    st[t[i]].insert(i);
  }
  fill(last, last + 26, -1);
  REP(i, 0, D) {
    ll contest = t[i];
    REP(j, 0, 26) {
      if (j == contest) {
        score[j] += s[i][j];
        last[j] = i;
      } else {
        score[j] -= c[j] * (i - last[j]);
      }
    }
  }
  ll sum = 0;
  REP(j, 0, 26) { sum += score[j]; }
  REP(i, 0, 1300000) {
    ll d = rand() % D;
    ll nowsummax = sum;
    ll maxind = t[d];
    ll oldq = t[d];
    ll tmpsum[26];
    REP(q, 0, 26) {
      ll nowsum = sum;
      nowsum -= s[d][oldq];
      nowsum += s[d][q];
      auto oldit = st[oldq].upper_bound(d);
      ll oldnext = *oldit;
      oldit--;
      oldit--;
      ll oldpre = *oldit;
      nowsum -= c[oldq] * (d - oldpre) * (oldnext - d);
      auto newit = st[q].upper_bound(d);
      ll newnext = *newit;
      newit--;
      ll newpre = *newit;
      nowsum += c[q] * (d - newpre) * (newnext - d);
      tmpsum[q] = nowsum;
      if (nowsum > nowsummax) {
        nowsummax = nowsum;
        maxind = q;
      }
    }
    if (maxind != t[d]) {
      st[oldq].erase(d);
      st[maxind].insert(d);
      t[d] = maxind;
      sum = nowsummax;
    }
    // cout << sum << endl;
  }
  REP(i, 0, D) { cout << t[i] + 1 << endl; }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <bitset>
#include <cassert>
#include <cctype>
#include <complex>
#include <functional>
#include <iomanip>
#include <iostream>
#include <map>
#include <math.h>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <stack>
#include <string>
#include <time.h>
#include <tuple>
#include <unistd.h>
#include <vector>
#define _USE_MATH_DEFINES
#define _GLIBCXX_DEBUG
using namespace std;
typedef long long ll;
typedef pair<int, int> pii;
typedef pair<ll, ll> plglg;
typedef pair<double, ll> pdlg;
typedef tuple<int, int, int> tiii;
typedef tuple<ll, ll, ll> tlglglg;
typedef tuple<ll, ll, ll, ll> tlglglglg;
typedef tuple<double, double, double> tddd;
typedef complex<double> xy_t;
typedef vector<ll> vll;
typedef vector<vector<ll>> matrix;
typedef unsigned int uint;
typedef unsigned long long ull;
template <class T> using V = vector<T>;
template <class T> using VV = V<V<T>>;
template <class T> using VVV = V<VV<T>>;
#define REP(i, x, y) for (ll i = (ll)x; i < (ll)y; i++)
#define DREP(i, x, y, d) for (ll i = (ll)x; i < (ll)y; i += (ll)d)
#define PER(i, x, y) for (ll i = (ll)x; i > (ll)y; i--)
#define DPER(i, x, y, d) for (ll i = (ll)x; i > (ll)y; i -= (ll)d)
int dx[4] = {1, 0, -1, 0};
int dy[4] = {0, 1, 0, -1};
double pi = 3.141592653589793;
ll mod = 1000000007;
int intmax = 2147483647;
int intmin = -2147483648;
ll llmax = 9223372036854775807;
ll llmin = -9223372036854775807;
int iinf = intmax / 8;
ll inf = llmax / 8;
double eps = 1e-12;
void yesno(bool ok, string yes = "Yes", string no = "No") {
  if (ok) {
    cout << yes << endl;
  } else {
    cout << no << endl;
  }
}

ll D;
ll c[26];
ll s[365][26];
ll score[26];
ll last[26];
set<ll> st[26];

void initrand() { srand((ll)time(NULL)); }

int main() {
  cin.tie(0);
  ios::sync_with_stdio(false);
  initrand();
  cin >> D;
  REP(i, 0, 26) { cin >> c[i]; }
  REP(i, 0, D) {
    REP(j, 0, 26) { cin >> s[i][j]; }
  }
  REP(i, 0, 26) {
    st[i].insert(-1);
    st[i].insert(D);
  }
  ll t[D];
  REP(i, 0, D) {
    t[i] = i % 26;
    st[t[i]].insert(i);
  }
  fill(last, last + 26, -1);
  REP(i, 0, D) {
    ll contest = t[i];
    REP(j, 0, 26) {
      if (j == contest) {
        score[j] += s[i][j];
        last[j] = i;
      } else {
        score[j] -= c[j] * (i - last[j]);
      }
    }
  }
  ll sum = 0;
  REP(j, 0, 26) { sum += score[j]; }
  REP(i, 0, 1400000) {
    ll d = rand() % D;
    ll nowsummax = sum;
    ll maxind = t[d];
    ll oldq = t[d];
    ll tmpsum[26];
    REP(q, 0, 26) {
      ll nowsum = sum;
      nowsum -= s[d][oldq];
      nowsum += s[d][q];
      auto oldit = st[oldq].upper_bound(d);
      ll oldnext = *oldit;
      oldit--;
      oldit--;
      ll oldpre = *oldit;
      nowsum -= c[oldq] * (d - oldpre) * (oldnext - d);
      auto newit = st[q].upper_bound(d);
      ll newnext = *newit;
      newit--;
      ll newpre = *newit;
      nowsum += c[q] * (d - newpre) * (newnext - d);
      tmpsum[q] = nowsum;
      if (nowsum > nowsummax) {
        nowsummax = nowsum;
        maxind = q;
      }
    }
    ll rd = rand() % 8;
    if (rd > 0) {
      if (maxind != t[d]) {
        st[oldq].erase(d);
        st[maxind].insert(d);
        t[d] = maxind;
        sum = nowsummax;
      }
    } else {
      ll q = rand() % 26;
      if (q != t[d]) {
        st[oldq].erase(d);
        st[q].insert(d);
        t[d] = q;
        sum = tmpsum[q];
      }
    }
    // cout << sum << endl;
  }
  REP(i, 0, D) { cout << t[i] + 1 << endl; }
}