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

#include <bits/stdc++.h>
using namespace std;

long long d, score = -1;
const long long t = 26;
vector<long long> c, res;
vector<vector<long long>> s;
vector<set<int>> st;

void solve();
long long calc();
long long change(int day, int id, bool ch = false);

int main() {
  cin >> d;
  c.resize(t);
  res.resize(d);
  for (auto &p : c)
    cin >> p;
  s.resize(d, vector<long long>(t, 0));
  for (auto &v : s)
    for (auto &p : v)
      cin >> p;
  for (int i = 0; i < d; ++i) {
    cin >> res[i];
    --res[i];
    // change(i, --res[i]);
  }
  long long m;
  cin >> m;
  for (int i = 0; i < m; ++i) {
    long long p, q;
    cin >> p >> q;
    cout << change(--p, --q, i == 0) << endl;
  }
  //   solve();
  //   calc();
  //   for (auto p : res) cout << p + 1 << endl;
  return 0;
}

void solve() {
  long long lim = 20000;
  vector<long long> memo(t, 0);
  for (int i = 0; i < d; ++i) {
    int id = 0;
    for (int j = 0; j < t; ++j)
      if (s[i][id] < s[i][j])
        id = j;
    if (s[i][id] >= lim)
      res[i] = id;
    else {
      int cid = 0;
      long long sum = 0;
      for (int j = 0; j < t; ++j) {
        sum += memo[j] + c[j];
        if (memo[j] + c[j] > memo[cid] + c[cid])
          cid = j;
      }
      sum -= memo[cid] + c[cid];
      if (s[i][id] > sum)
        res[i] = id;
      else
        res[i] = cid;
    }
    for (int j = 0; j < t; ++j)
      if (j != res[i])
        memo[j] += c[j];
      else
        memo[j] = 0;
  }
  cerr << calc() << endl;
}

long long calc() {
  long long sum = 0;
  vector<long long> memo(t, 0);
  for (int i = 0; i < d; ++i) {
    int num = res[i];
    sum += s[i][num];
    for (int j = 0; j < t; ++j)
      if (j != num)
        sum -= (memo[j] += c[j]);
      else
        memo[j] = 0;
  }
  return sum;
}

long long change(int day, int id, bool ch) {
  if (ch) {
    res[day] = id;
    score = calc();
    st.resize(t);
    for (int i = 0; i < t; ++i)
      st[i].insert(0), st[i].insert(d + 1);
    for (int i = 0; i < d; ++i)
      st[res[i]].insert(i + 1);
    return score;
  }
  int bf = res[day];
  score -= s[day][bf];
  score += s[day][id];
  st[bf].erase(day + 1);
  auto it = st[bf].upper_bound(day + 1);
  int righ = *it, lef;
  --it;
  lef = *it;
  score += ((righ - day - 1) * (righ - day - 2) +
            (day + 1 - lef) * (day - lef) - (righ - lef) * (righ - lef - 1)) *
           c[bf] / 2;
  it = st[id].upper_bound(day + 1);
  righ = *it;
  --it;
  lef = *it;
  score -= ((righ - day - 1) * (righ - day - 2) +
            (day + 1 - lef) * (day - lef) - (righ - lef) * (righ - lef - 1)) *
           c[id] / 2;
  res[day] = id;
  st[id].insert(day + 1);
  return score;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;

long long d, score = -1;
const long long t = 26;
vector<long long> c, res;
vector<vector<long long>> s;
vector<set<int>> st;

void solve();
long long calc();
long long change(int day, int id);

int main() {
  cin >> d;
  c.resize(t);
  res.resize(d);
  for (auto &p : c)
    cin >> p;
  s.resize(d, vector<long long>(t, 0));
  for (auto &v : s)
    for (auto &p : v)
      cin >> p;
  for (int i = 0; i < d; ++i) {
    cin >> res[i];
    --res[i];
    // change(i, --res[i]);
  }
  long long m;
  cin >> m;
  for (int i = 0; i < m; ++i) {
    long long p, q;
    cin >> p >> q;
    cout << change(--p, --q) << endl;
  }
  //   solve();
  //   calc();
  //   for (auto p : res) cout << p + 1 << endl;
  return 0;
}

void solve() {
  long long lim = 20000;
  vector<long long> memo(t, 0);
  for (int i = 0; i < d; ++i) {
    int id = 0;
    for (int j = 0; j < t; ++j)
      if (s[i][id] < s[i][j])
        id = j;
    if (s[i][id] >= lim)
      res[i] = id;
    else {
      int cid = 0;
      long long sum = 0;
      for (int j = 0; j < t; ++j) {
        sum += memo[j] + c[j];
        if (memo[j] + c[j] > memo[cid] + c[cid])
          cid = j;
      }
      sum -= memo[cid] + c[cid];
      if (s[i][id] > sum)
        res[i] = id;
      else
        res[i] = cid;
    }
    for (int j = 0; j < t; ++j)
      if (j != res[i])
        memo[j] += c[j];
      else
        memo[j] = 0;
  }
  cerr << calc() << endl;
}

long long calc() {
  long long sum = 0;
  vector<long long> memo(t, 0);
  for (int i = 0; i < d; ++i) {
    int num = res[i];
    sum += s[i][num];
    for (int j = 0; j < t; ++j)
      if (j != num)
        sum -= (memo[j] += c[j]);
      else
        memo[j] = 0;
  }
  return sum;
}

long long change(int day, int id) {
  if (score < 0) {
    res[day] = id;
    score = calc();
    st.resize(t);
    for (int i = 0; i < t; ++i)
      st[i].insert(0), st[i].insert(d + 1);
    for (int i = 0; i < d; ++i)
      st[res[i]].insert(i + 1);
    return score;
  }
  int bf = res[day];
  score -= s[day][bf];
  score += s[day][id];
  st[bf].erase(day + 1);
  auto it = st[bf].upper_bound(day + 1);
  int righ = *it, lef;
  --it;
  lef = *it;
  score += ((righ - day - 1) * (righ - day - 2) +
            (day + 1 - lef) * (day - lef) - (righ - lef) * (righ - lef - 1)) *
           c[bf] / 2;
  it = st[id].upper_bound(day + 1);
  righ = *it;
  --it;
  lef = *it;
  score -= ((righ - day - 1) * (righ - day - 2) +
            (day + 1 - lef) * (day - lef) - (righ - lef) * (righ - lef - 1)) *
           c[id] / 2;
  res[day] = id;
  st[id].insert(day + 1);
  return score;
}