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

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cout << *i << " ";
  cout << endl;
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;
struct Service {
  int index;
  ll w;
  int t, x;
  bool operator<(const Service &s) const {
    if (w != s.w)
      return w < s.w;
    else
      return index < s.index;
  }
};

int main() {
  int N, Q;
  while (cin >> N >> Q) {
    vector<Service> ss(N);
    REP(i, N) cin >> ss[i].w >> ss[i].t >> ss[i].x;
    REP(i, N) ss[i].index = i;
    sort(ss.begin(), ss.end());
    const int D = 3652425;
    vector<ll> rp(D + 2, 0);
    ll k[3] = {};
    k[0] = 1;
    static ll dk[D + 2][3] = {};
    memset(dk, 0, sizeof(dk));
    vector<int> rp_day(N, INF);
    int ans = INF;
    int ss_idx = 0;
    for (ll d = 1; d <= D + 1; d++) {
      ll p = rp[d - 1];
      while (ss_idx < N && p >= ss[ss_idx].w) {
        Service &s = ss[ss_idx++];
        rp_day[s.index] = d - 1;
        if (s.t == 0) {
          k[0]++;
          if (d + s.x <= D + 1)
            dk[d + s.x][0]++;
        } else if (s.t == 1) {
          k[1]++;
          k[0] += -(d - 1);
          if (d + s.x <= D + 1) {
            dk[d + s.x][1]++;
            dk[d + s.x][0] += -(d - 1);
          }
        } else {
          k[2]++;
          k[1] += -2 * (d - 1);
          k[0] += (d - 1) * (d - 1);
          if (d + s.x <= D + 1) {
            dk[d + s.x][2]++;
            dk[d + s.x][1] += -2 * (d - 1);
            dk[d + s.x][0] += (d - 1) * (d - 1);
          }
        }
      }
      REP(i, 3) k[i] -= dk[d][i];
      rp[d] = p + k[0] + k[1] * d + k[2] * d * d;
    }
    REP(i, N) {
      if (rp_day[i] > D)
        cout << "Many years later" << endl;
      else
        cout << rp_day[i] << endl;
    }
    REP(i, Q) {
      int y;
      cin >> y;
      cout << rp[y] << endl;
    }
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstdlib>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <vector>

using namespace std;

#define FOR(i, k, n) for (int i = (k); i < (int)n; ++i)
#define REP(i, n) FOR(i, 0, n)
#define FORIT(i, c)                                                            \\
  for (__typeof((c).begin()) i = (c).begin(); i != (c).end(); ++i)

template <class T> void debug(T begin, T end) {
  for (T i = begin; i != end; ++i)
    cout << *i << " ";
  cout << endl;
}

typedef long long ll;
const int INF = 100000000;
const double EPS = 1e-8;
const int MOD = 1000000007;

int main() {
  int N, Q;
  while (cin >> N >> Q) {
    vector<ll> w(N);
    vector<int> t(N);
    vector<int> x(N);
    REP(i, N) cin >> w[i] >> t[i] >> x[i];
    const int D = 3652425;
    vector<ll> rp(D + 2, 0);
    ll k[3] = {};
    k[0] = 1;
    static ll dk[D + 2][3] = {};
    memset(dk, 0, sizeof(dk));
    vector<int> rp_day(N, INF);
    int ans = INF;
    for (int d = 1; d <= D + 1; d++) {
      ll p = rp[d - 1];
      for (int i = 0; i < N; i++)
        if (rp_day[i] == INF && p >= w[i]) {
          rp_day[i] = d - 1;
          if (t[i] == 0) {
            k[0]++;
            if (d + x[i] <= D + 1)
              dk[d + x[i]][0]++;
          } else if (t[i] == 1) {
            k[1]++;
            k[0] += -(d - 1);
            if (d + x[i] <= D + 1) {
              dk[d + x[i]][1]++;
              dk[d + x[i]][0] += -(d - 1);
            }
          } else {
            k[2]++;
            k[1] += -2 * (d - 1);
            k[0] += (d - 1) * (d - 1);
            if (d + x[i] <= D + 1) {
              dk[d + x[i]][2]++;
              dk[d + x[i]][1] += -2 * (d - 1);
              dk[d + x[i]][0] += (d - 1) * (d - 1);
            }
          }
        }
      REP(i, 3) k[i] -= dk[d][i];
      rp[d] = p + k[0] + k[1] * d + k[2] * d * d;
    }
    REP(i, N) {
      if (rp_day[i] > D)
        cout << "Many years later" << endl;
      else
        cout << rp_day[i] << endl;
    }
    REP(i, Q) {
      int y;
      cin >> y;
      cout << rp[y] << endl;
    }
  }
  return 0;
}