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

#include <bits/stdc++.h>

#define X first
#define Y second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define rep(i, a, b) for (unsigned i = (a); i <= (b); ++i)
#define per(i, a, b) for (unsigned i = (a); i >= (b); --i)

using namespace std;

using I = int;
using L = long;
using F = float;
using S = string;
using SS = stringstream;
using C = char;
using LL = long long;
using VI = vector<int>;
using VL = vector<long>;
using VF = vector<float>;
using VS = vector<string>;
using PLL = pair<long, long>;
using PLF = pair<long, float>;
using PLS = pair<long, string>;
using PSL = pair<string, long>;
using PSF = pair<string, float>;
using PSS = pair<string, string>;
using MLL = map<long, long>;
using MLF = map<long, float>;
using MLS = map<long, string>;
using MSL = map<string, long>;
using MSF = map<string, float>;
using MSS = map<string, string>;

const long kMax2 = 109;
const long kMax3 = 1009;
const long kMax4 = 10009;
const long kMax5 = 100009;
const long kMax6 = 1000009;
const long kInfI = 0x3F3F3F3F;
const float kInfF = 1e15;

L TempVar(L h, L s, L t) {
  if (h > 0) {
    return -s * h;
  } else {
    return -t * h;
  }
}

int main(int argc, char const *argv[]) {
  L n, q, s, t;
  VL a;
  cin >> n >> q >> s >> t;
  L prev_h = 0;
  L h;
  rep(i, 0, n) {
    cin >> h;
    if (i == 0) {
      continue;
    }
    a.pb(h - prev_h);
    prev_h = h;
  }

  L temp = 0;
  rep(i, 0, n - 1) { temp += TempVar(a[i], s, t); }

  rep(i, 1, q) {
    L l, r, x;
    cin >> l >> r >> x;
    temp -= TempVar(a[l - 1], s, t);
    if (r != n)
      temp -= TempVar(a[r], s, t);
    a[l - 1] += x;
    if (r != n) {
      a[r] -= x;
    }
    temp += TempVar(a[l - 1], s, t);
    if (r != n)
      temp += TempVar(a[r], s, t);

    cout << temp << endl;
  }

  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define X first
#define Y second
#define pb push_back
#define eb emplace_back
#define mp make_pair
#define rep(i, a, b) for (unsigned i = (a); i <= (b); ++i)
#define per(i, a, b) for (unsigned i = (a); i >= (b); --i)

using namespace std;

using I = int;
using L = long;
using F = float;
using S = string;
using SS = stringstream;
using C = char;
using LL = long long;
using VI = vector<int>;
using VL = vector<long>;
using VF = vector<float>;
using VS = vector<string>;
using PLL = pair<long, long>;
using PLF = pair<long, float>;
using PLS = pair<long, string>;
using PSL = pair<string, long>;
using PSF = pair<string, float>;
using PSS = pair<string, string>;
using MLL = map<long, long>;
using MLF = map<long, float>;
using MLS = map<long, string>;
using MSL = map<string, long>;
using MSF = map<string, float>;
using MSS = map<string, string>;

const long kMax2 = 109;
const long kMax3 = 1009;
const long kMax4 = 10009;
const long kMax5 = 100009;
const long kMax6 = 1000009;
const long kInfI = 0x3F3F3F3F;
const float kInfF = 1e15;

int main(int argc, char const *argv[]) {
  L n, q, s, t;
  VL a;
  cin >> n >> q >> s >> t;
  L prev_h = 0;
  L h;
  rep(i, 0, n) {
    cin >> h;
    if (i == 0) {
      continue;
    }
    a.pb(h - prev_h);
    prev_h = h;
  }

  rep(i, 1, q) {
    L l, r, x;
    cin >> l >> r >> x;
    L temp = 0;
    a[l - 1] += x;
    if (r != n) {
      a[r] -= x;
    }

    rep(i, 0, n - 1) {
      if (a[i] > 0) {
        temp -= s * a[i];
      } else {
        temp -= t * a[i];
      }
    }
    cout << temp << endl;
  }

  return 0;
}