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

#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define R cin >>
#define ll long long
#define ln cout << endl
#define in(a) insert(a)
#define pb(a) push_back(a)
#define pd(a) printf("%.10f\\n", a)
#define mem(a) memset(a, 0, sizeof(a))
#define all(c) (c).begin(), (c).end()
#define iter(c) __typeof((c).begin())
#define rrep(i, n) for (ll i = (ll)(n)-1; i >= 0; i--)
#define REP(i, m, n) for (ll i = (ll)(m); i < (ll)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define tr(it, c) for (iter(c) it = (c).begin(); it != (c).end(); it++)
ll check(ll n, ll m, ll x, ll y) { return x >= 0 && x < n && y >= 0 && y < m; }
void pr() { ln; }
template <class A, class... B> void pr(const A &a, const B &...b) {
  cout << a << (sizeof...(b) ? " " : "");
  pr(b...);
}
template <class A> void PR(A a, ll n) {
  rep(i, n) cout << (i ? " " : "") << a[i];
  ln;
}
const ll MAX = 1e9 + 7, MAXL = 1LL << 61, dx[8] = {-1, 0, 1, 0, -1, -1, 1, 1},
         dy[8] = {0, 1, 0, -1, -1, 1, 1, -1};
typedef pair<ll, ll> P;

ll calc(ll k, ll d) { return k * (k + 1) / 2 * d; }

const ll n = 26;
ll c[n], q[444];
ll a[444][n];
set<ll> s[n];

ll solve(ll x, ll y, ll t) {
  ll ans = 0, e = q[x];
  {
    ans -= a[x][q[x]];
    iter(s[q[x]]) it2 = s[q[x]].lower_bound(x);
    iter(s[q[x]]) it1 = it2, it3 = it2;
    it1--, it3++;
    ans += calc(*it2 - *it1 - 1, c[q[x]]);
    ans += calc(*it3 - *it2 - 1, c[q[x]]);
    ans -= calc(*it3 - *it1 - 1, c[q[x]]);
    s[q[x]].erase(it2);
  }
  {
    q[x] = y;
    s[q[x]].in(x);
    ans += a[x][q[x]];
    iter(s[q[x]]) it2 = s[q[x]].lower_bound(x);
    iter(s[q[x]]) it1 = it2, it3 = it2;
    it1--, it3++;
    ans -= calc(*it2 - *it1 - 1, c[q[x]]);
    ans -= calc(*it3 - *it2 - 1, c[q[x]]);
    ans += calc(*it3 - *it1 - 1, c[q[x]]);
  }
  if (!t) {
    s[q[x]].erase(x);
    q[x] = e;
    s[q[x]].in(x);
  }
  return ans;
}

void Main() {
  srand((unsigned)time(NULL));
  int T;
  R T;
  rep(i, n) R c[i];
  rep(i, T) rep(j, n) R a[i][j];
  rep(i, n) s[i].in(-1), s[i].in(T);
  rep(k, T) {
    q[k] = 0;
    s[0].in(k);
  }
  ll ans[T];
  mem(ans);
  clock_t t2 = clock();
  for (int tt = 0;; tt++) {
    ll x = rand() % T, y = rand() % n;
    if (solve(x, y, 0) > 0) {
      solve(x, y, 1);
      ans[x] = y;
    }
    clock_t t3 = clock();
    double t4 = (double)(t3 - t2) / CLOCKS_PER_SEC;
    if (tt >= 2 && (double)t3 / CLOCKS_PER_SEC + t4 * 2 > 1.500)
      break;
    t2 = t3;
  }
  rep(i, T) pr(ans[i] + 1);
}

int main() {
  Main();
  return 0;
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define F first
#define S second
#define R cin >>
#define ll long long
#define ln cout << endl
#define in(a) insert(a)
#define pb(a) push_back(a)
#define pd(a) printf("%.10f\\n", a)
#define mem(a) memset(a, 0, sizeof(a))
#define all(c) (c).begin(), (c).end()
#define iter(c) __typeof((c).begin())
#define rrep(i, n) for (ll i = (ll)(n)-1; i >= 0; i--)
#define REP(i, m, n) for (ll i = (ll)(m); i < (ll)(n); i++)
#define rep(i, n) REP(i, 0, n)
#define tr(it, c) for (iter(c) it = (c).begin(); it != (c).end(); it++)
ll check(ll n, ll m, ll x, ll y) { return x >= 0 && x < n && y >= 0 && y < m; }
void pr() { ln; }
template <class A, class... B> void pr(const A &a, const B &...b) {
  cout << a << (sizeof...(b) ? " " : "");
  pr(b...);
}
template <class A> void PR(A a, ll n) {
  rep(i, n) cout << (i ? " " : "") << a[i];
  ln;
}
const ll MAX = 1e9 + 7, MAXL = 1LL << 61, dx[8] = {-1, 0, 1, 0, -1, -1, 1, 1},
         dy[8] = {0, 1, 0, -1, -1, 1, 1, -1};
typedef pair<ll, ll> P;

ll calc(ll k, ll d) { return k * (k + 1) / 2 * d; }

const ll n = 26;
ll c[n], q[444];
ll a[444][n];
set<ll> s[n];

ll solve(ll x, ll y, ll t) {
  ll ans = 0, e = q[x];
  {
    ans -= a[x][q[x]];
    iter(s[q[x]]) it2 = s[q[x]].lower_bound(x);
    iter(s[q[x]]) it1 = it2, it3 = it2;
    it1--, it3++;
    ans += calc(*it2 - *it1 - 1, c[q[x]]);
    ans += calc(*it3 - *it2 - 1, c[q[x]]);
    ans -= calc(*it3 - *it1 - 1, c[q[x]]);
    s[q[x]].erase(it2);
  }
  {
    q[x] = y;
    s[q[x]].in(x);
    ans += a[x][q[x]];
    iter(s[q[x]]) it2 = s[q[x]].lower_bound(x);
    iter(s[q[x]]) it1 = it2, it3 = it2;
    it1--, it3++;
    ans -= calc(*it2 - *it1 - 1, c[q[x]]);
    ans -= calc(*it3 - *it2 - 1, c[q[x]]);
    ans += calc(*it3 - *it1 - 1, c[q[x]]);
  }
  if (!t) {
    s[q[x]].erase(x);
    q[x] = e;
    s[q[x]].in(x);
  }
  return ans;
}

void Main() {
  srand((unsigned)time(NULL));
  int T;
  R T;
  rep(i, n) R c[i];
  rep(i, T) rep(j, n) R a[i][j];
  rep(i, n) s[i].in(-1), s[i].in(T);
  rep(k, T) {
    q[k] = 0;
    s[0].in(k);
  }
  ll ans[T];
  mem(ans);
  clock_t t2 = clock();
  for (int tt = 0;; tt++) {
    ll x = rand() % T, y = rand() % n;
    if (solve(x, y, 0) > 0) {
      solve(x, y, 1);
      ans[x] = y;
    }
    clock_t t3 = clock();
    double t4 = (double)(t3 - t2) / CLOCKS_PER_SEC;
    if (tt >= 2 && (double)t3 / CLOCKS_PER_SEC + t4 * 2 > 1.997)
      break;
    t2 = t3;
  }
  rep(i, T) pr(ans[i] + 1);
}

int main() {
  Main();
  return 0;
}