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

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 100000000
#define EPS 1e-10
#define MOD 1000000007
using namespace std;

int n, m;
int s[100000], t[100000];
int pre[2][100000];
vector<int> p;

void Pre() {
  rep(i, p.size()) {
    if (i == 0) {
      pre[0][i] = 0;
      pre[1][i] = 0;
      continue;
    }
    pre[0][i] = pre[1][i - 1] + p[i] - p[i - 1];
    pre[1][i] = pre[0][i - 1];
  }
}

int main() {
  cin >> n >> m;
  rep(i, n) { cin >> s[i] >> t[i]; }
  rep(i, m) {
    int tmp;
    cin >> tmp;
    p.push_back(tmp);
  }
  sort(p.begin(), p.end());
  Pre();

  long long ans = 0;
  rep(i, n) {
    if (s[i] < t[i]) {
      int stt = (upper_bound(p.begin(), p.end(), s[i]) - p.begin());
      int ett = (upper_bound(p.begin(), p.end(), t[i]) - p.begin());
      if (ett == 0) {
        ans += t[i] - s[i];
        continue;
      }
      ett--;
      if (stt == p.size() || p[stt] > t[i]) {
        ans += t[i] - s[i];
        continue;
      }
      ans += p[stt] - s[i];
      if ((ett - stt) % 2 == 0) {
        ans += pre[0][ett] - pre[0][stt];
      } else {
        ans += t[i] - p[ett];
        ans += pre[1][ett] - pre[0][stt];
      }
    } else {
      int stt = (upper_bound(p.begin(), p.end(), t[i]) - p.begin());
      int ett = (upper_bound(p.begin(), p.end(), s[i]) - p.begin());
      if (ett == 0) {
        ans += s[i] - t[i];
        continue;
      }
      ett--;
      if (stt == p.size() || p[stt] > s[i]) {
        ans += s[i] - t[i];
        continue;
      }
      ans += s[i] - p[ett];
      if ((ett - stt) % 2 == 0) {
        ans += pre[1][ett] - pre[1][stt];
      } else {
        ans += p[stt] - t[i];
        ans += pre[1][ett] - pre[0][stt];
      }
    }
  }
  cout << ans << endl;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>
#define rep(i, n) for (int i = 0; i < n; i++)
#define INF 100000000
#define EPS 1e-10
#define MOD 1000000007
using namespace std;

int n, m;
int s[100000], t[100000];
vector<int> p;
vector<int> pp;

int main() {
  cin >> n >> m;
  rep(i, n) { cin >> s[i] >> t[i]; }
  rep(i, m) {
    int tmp;
    cin >> tmp;
    p.push_back(tmp);
    pp.push_back(-1 * tmp);
  }
  sort(p.begin(), p.end());
  sort(pp.begin(), pp.end());
  long long ans = 0;
  rep(i, n) {
    if (s[i] < t[i]) {
      int stt = (upper_bound(p.begin(), p.end(), s[i]) - p.begin());
      if (stt == p.size() || p[stt] > t[i]) {
        ans += t[i] - s[i];
        continue;
      }
      ans += p[stt] - s[i];
      while (true) {
        if (stt + 1 >= p.size() || p[stt + 1] > t[i])
          break;
        if (stt + 2 >= p.size() || p[stt + 2] > t[i])
          ans += t[i] - p[stt + 1];
        else
          ans += p[stt + 2] - p[stt + 1];
        stt += 2;
      }
    } else {
      s[i] *= -1;
      t[i] *= -1;
      int stt = (upper_bound(pp.begin(), pp.end(), s[i]) - pp.begin());
      if (stt == pp.size() || pp[stt] > t[i]) {
        ans += t[i] - s[i];
        continue;
      }
      ans += pp[stt] - s[i];
      while (true) {
        if (stt + 1 >= pp.size() || pp[stt + 1] > t[i])
          break;
        ;
        if (stt + 2 >= pp.size() || pp[stt + 2] > t[i])
          ans += t[i] - pp[stt + 1];
        else
          ans += pp[stt + 2] - pp[stt + 1];
        stt += 2;
      }
    }
  }
  cout << ans << endl;
}