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

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <ostream>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <typeinfo>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define INIT                                                                   \\
  std::ios::sync_with_stdio(false);                                            \\
  std::cin.tie(0);
#define VAR(type, ...)                                                         \\
  type __VA_ARGS__;                                                            \\
  Scan(__VA_ARGS__);
template <typename T> void Scan(T &t) { std::cin >> t; }
template <typename First, typename... Rest>
void Scan(First &first, Rest &...rest) {
  std::cin >> first;
  Scan(rest...);
}
#define OUT(d) std::cout << d;
#define FOUT(n, d) std::cout << std::fixed << std::setprecision(n) << d;
#define SOUT(n, c, d) std::cout << std::setw(n) << std::setfill(c) << d;
#define SP std::cout << " ";
#define TAB std::cout << "\\t";
#define BR std::cout << "\\n";
#define ENDL std::cout << std::endl;
#define FLUSH std::cout << std::flush;
#define VEC(type, c, n)                                                        \\
  std::vector<type> c(n);                                                      \\
  for (auto &i : c)                                                            \\
    std::cin >> i;
#define MAT(type, c, m, n)                                                     \\
  std::vector<std::vector<type>> c(m, std::vector<type>(n));                   \\
  for (auto &r : c)                                                            \\
    for (auto &i : r)                                                          \\
      std::cin >> i;
#define ALL(a) (a).begin(), (a).end()
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); --i)
#define REP(i, n) for (int i = 0; i < int(n); ++i)
#define RREP(i, n) for (int i = (n)-1; i >= 0; --i)
#define FORLL(i, a, b) for (ll i = ll(a); i < ll(b); ++i)
#define RFORLL(i, a, b) for (ll i = ll(b) - 1; i >= ll(a); --i)
#define REPLL(i, n) for (ll i = 0; i < ll(n); ++i)
#define RREPLL(i, n) for (ll i = ll(n) - 1; i >= 0; --i)
#define PAIR std::pair<int, int>
#define PAIRLL std::pair<ll, ll>
#define IN(a, x, b) (a <= x && x < b)
#define SHOW(d)                                                                \\
  { std::cout << #d << "\\t:" << d << "\\t"; }
#define SHOWVECTOR(v)                                                          \\
  {                                                                            \\
    std::cout << #v << "\\t:";                                                  \\
    for (const auto &i : v) {                                                  \\
      std::cout << i << " ";                                                   \\
    }                                                                          \\
    std::cout << "\\n";                                                         \\
  }
#define SHOWVECTOR2(v)                                                         \\
  {                                                                            \\
    std::cout << #v << "\\t:\\n";                                                \\
    for (const auto &i : v) {                                                  \\
      for (const auto &j : i) {                                                \\
        std::cout << j << " ";                                                 \\
      }                                                                        \\
      std::cout << "\\n";                                                       \\
    }                                                                          \\
  }
#define SHOWPAIRVECTOR2(v)                                                     \\
  {                                                                            \\
    std::cout << #v << "\\t:\\n";                                                \\
    for (const auto &i : v) {                                                  \\
      for (const auto &j : i) {                                                \\
        std::cout << '(' << j.first << ", " << j.second << ") ";               \\
      }                                                                        \\
      std::cout << "\\n";                                                       \\
    }                                                                          \\
  }
#define SHOWPAIRVECTOR(v)                                                      \\
  {                                                                            \\
    for (const auto &i : v) {                                                  \\
      std::cout << '(' << i.first << ", " << i.second << ") ";                 \\
    }                                                                          \\
    std::cout << "\\n";                                                         \\
  }
#define CHECKTIME(state)                                                       \\
  {                                                                            \\
    auto start = std::chrono::system_clock::now();                             \\
    state();                                                                   \\
    auto end = std::chrono::system_clock::now();                               \\
    auto res =                                                                 \\
        std::chrono::duration_cast<std::chrono::nanoseconds>(end - start)      \\
            .count();                                                          \\
    std::cerr << "[Time:" << res << "ns  (" << res / (1.0e9) << "s)]\\n";       \\
  }
#define SHOWQUEUE(a)                                                           \\
  {                                                                            \\
    std::queue<decltype(a.front())> tmp(a);                                    \\
    std::cout << #a << "\\t:";                                                  \\
    for (int i = 0; i < static_cast<int>(a.size()); ++i) {                     \\
      std::cout << tmp.front() << "\\n";                                        \\
      tmp.pop();                                                               \\
    }                                                                          \\
    std::cout << "\\n";                                                         \\
  }
#define CHMAX(a, b) a = (((a) < (b)) ? (b) : (a))
#define CHMIN(a, b) a = (((a) > (b)) ? (b) : (a))

#define int ll
using ll = long long;
using ull = unsigned long long;
constexpr int INFINT = 1 << 30;
constexpr ll INFLL = 1LL << 60;
constexpr double EPS = 0.0000000001;
constexpr int MOD = 1000000007;

// SegmentTree(?????°??¨)
class SegmentTree {
private:
  int init_val;
  std::vector<int> ary;
  std::function<int(int, int)> func;

public:
  // ?????¨???(??????????????§??????N??¨??????)
  // SegmentTree st(N,  INF, [](int l, int r){return std::min(l, r);});
  // ????°???? SegmentTree st(N, -INF, [](int l, int r){return std::max(l,
  // r);}); ?????§??? SegmentTree st(N,    0, [](int l, int r){return l+r;});
  // ????¨?
  SegmentTree(int n, int val, std::function<int(int, int)> f)
      : init_val(val), func(f) {
    ary.resize(static_cast<int>(
                   std::pow(2, std::ceil(std::log(n) / std::log(2)) + 1) - 1),
               val);
  }
  void init(int n, int val, std::function<int(int, int)> f) {
    ary.resize(
        static_cast<int>(
            std::pow(2, std::ceil(1.0 * std::log(n) / std::log(2)) + 1) - 1),
        val);
    init_val = val;
    func = f;
  }
  void update(int i, int val) {
    i += ary.size() / 2;
    ary[i] = val;
    while (i > 0) {
      i = (i - 1) / 2;
      ary[i] = func(ary[2 * i + 1], ary[2 * i + 2]);
    }
  }
  // ??????[a, b)???func????????????????????? (query_(a, b, 0, 0,
  // n)??¨????????????)
  int query_(int a, int b, int i, int l, int r) {
    if (r <= a || b <= l)
      return init_val;
    if (a <= l && r <= b)
      return ary[i];
    else {
      ll vl = query_(a, b, 2 * i + 1, l, (l + r) / 2);
      ll vr = query_(a, b, 2 * i + 2, (l + r) / 2, r);
      return func(vl, vr);
    }
  }
  // ??????(b)????°??????§?????????????????????
  ll query(int a, int b) { return query_(a, b, 0, 0, ary.size() / 2 + 1); }
};

signed main() {
  INIT;
  VAR(ll, t);
  VAR(int, n);
  VEC(ll, s, n * 2);
  std::vector<ll> sum(n, 0);
  SegmentTree st(n, 0, [](ll l, ll r) { return l + r; });
  REP(i, n) {
    sum[i] = s[2 * i + 1] - s[2 * i];
    st.update(i, sum[i]);
  }
  ll max = 0;
  REP(i, n) {
    ll time = 0;
    ll tmp = s[2 * i] + t;
    if (tmp < 0)
      tmp = 1000000000000000;
    auto it = std::lower_bound(ALL(s), tmp);
    int pos = std::distance(s.begin(), it);
    if (--pos % 2 == 0) {
      time += tmp - s[pos];
    } else {
      ++pos;
    }
    time += st.query(i, pos / 2);
    CHMAX(max, time);
  }
  OUT(max) BR;
  return 0;
}

Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <chrono>
#include <complex>
#include <deque>
#include <functional>
#include <iomanip>
#include <iostream>
#include <istream>
#include <iterator>
#include <limits>
#include <list>
#include <map>
#include <memory>
#include <numeric>
#include <ostream>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <string>
#include <tuple>
#include <typeinfo>
#include <unordered_map>
#include <unordered_set>
#include <utility>
#include <vector>
#define INIT                                                                   \\
  std::ios::sync_with_stdio(false);                                            \\
  std::cin.tie(0);
#define VAR(type, ...)                                                         \\
  type __VA_ARGS__;                                                            \\
  Scan(__VA_ARGS__);
template <typename T> void Scan(T &t) { std::cin >> t; }
template <typename First, typename... Rest>
void Scan(First &first, Rest &...rest) {
  std::cin >> first;
  Scan(rest...);
}
#define OUT(d) std::cout << d;
#define FOUT(n, d) std::cout << std::fixed << std::setprecision(n) << d;
#define SOUT(n, c, d) std::cout << std::setw(n) << std::setfill(c) << d;
#define SP std::cout << " ";
#define TAB std::cout << "\\t";
#define BR std::cout << "\\n";
#define ENDL std::cout << std::endl;
#define FLUSH std::cout << std::flush;
#define VEC(type, c, n)                                                        \\
  std::vector<type> c(n);                                                      \\
  for (auto &i : c)                                                            \\
    std::cin >> i;
#define MAT(type, c, m, n)                                                     \\
  std::vector<std::vector<type>> c(m, std::vector<type>(n));                   \\
  for (auto &r : c)                                                            \\
    for (auto &i : r)                                                          \\
      std::cin >> i;
#define ALL(a) (a).begin(), (a).end()
#define FOR(i, a, b) for (int i = (a); i < (b); ++i)
#define RFOR(i, a, b) for (int i = (b)-1; i >= (a); --i)
#define REP(i, n) for (int i = 0; i < int(n); ++i)
#define RREP(i, n) for (int i = (n)-1; i >= 0; --i)
#define FORLL(i, a, b) for (ll i = ll(a); i < ll(b); ++i)
#define RFORLL(i, a, b) for (ll i = ll(b) - 1; i >= ll(a); --i)
#define REPLL(i, n) for (ll i = 0; i < ll(n); ++i)
#define RREPLL(i, n) for (ll i = ll(n) - 1; i >= 0; --i)
#define PAIR std::pair<int, int>
#define PAIRLL std::pair<ll, ll>
#define IN(a, x, b) (a <= x && x < b)
#define SHOW(d)                                                                \\
  { std::cout << #d << "\\t:" << d << "\\t"; }
#define SHOWVECTOR(v)                                                          \\
  {                                                                            \\
    std::cout << #v << "\\t:";                                                  \\
    for (const auto &i : v) {                                                  \\
      std::cout << i << " ";                                                   \\
    }                                                                          \\
    std::cout << "\\n";                                                         \\
  }
#define SHOWVECTOR2(v)                                                         \\
  {                                                                            \\
    std::cout << #v << "\\t:\\n";                                                \\
    for (const auto &i : v) {                                                  \\
      for (const auto &j : i) {                                                \\
        std::cout << j << " ";                                                 \\
      }                                                                        \\
      std::cout << "\\n";                                                       \\
    }                                                                          \\
  }
#define SHOWPAIRVECTOR2(v)                                                     \\
  {                                                                            \\
    std::cout << #v << "\\t:\\n";                                                \\
    for (const auto &i : v) {                                                  \\
      for (const auto &j : i) {                                                \\
        std::cout << '(' << j.first << ", " << j.second << ") ";               \\
      }                                                                        \\
      std::cout << "\\n";                                                       \\
    }                                                                          \\
  }
#define SHOWPAIRVECTOR(v)                                                      \\
  {                                                                            \\
    for (const auto &i : v) {                                                  \\
      std::cout << '(' << i.first << ", " << i.second << ") ";                 \\
    }                                                                          \\
    std::cout << "\\n";                                                         \\
  }
#define CHECKTIME(state)                                                       \\
  {                                                                            \\
    auto start = std::chrono::system_clock::now();                             \\
    state();                                                                   \\
    auto end = std::chrono::system_clock::now();                               \\
    auto res =                                                                 \\
        std::chrono::duration_cast<std::chrono::nanoseconds>(end - start)      \\
            .count();                                                          \\
    std::cerr << "[Time:" << res << "ns  (" << res / (1.0e9) << "s)]\\n";       \\
  }
#define SHOWQUEUE(a)                                                           \\
  {                                                                            \\
    std::queue<decltype(a.front())> tmp(a);                                    \\
    std::cout << #a << "\\t:";                                                  \\
    for (int i = 0; i < static_cast<int>(a.size()); ++i) {                     \\
      std::cout << tmp.front() << "\\n";                                        \\
      tmp.pop();                                                               \\
    }                                                                          \\
    std::cout << "\\n";                                                         \\
  }
#define CHMAX(a, b) a = (((a) < (b)) ? (b) : (a))
#define CHMIN(a, b) a = (((a) > (b)) ? (b) : (a))

// #define int ll
using ll = long long;
using ull = unsigned long long;
constexpr int INFINT = 1 << 30;
constexpr ll INFLL = 1LL << 60;
constexpr double EPS = 0.0000000001;
constexpr int MOD = 1000000007;

signed main() {
  INIT;
  VAR(ll, t);
  VAR(int, n);
  VEC(ll, s, n * 2);
  std::vector<ll> sum(n, 0);
  REP(i, n) { sum[i] = s[2 * i + 1] - s[2 * i]; }
  ll max = 0;
  REP(i, n) {
    ll time = 0;
    ll tmp = s[2 * i] + t;
    if (tmp < 0)
      tmp = 1000000000000000;
    auto it = std::lower_bound(ALL(s), tmp);
    int pos = std::distance(s.begin(), it);
    if (--pos % 2 == 0) {
      time += tmp - s[pos];
    } else {
      ++pos;
    }
    while (pos > i * 2) {
      pos -= 2;
      time += sum[pos / 2];
    }
    CHMAX(max, time);
  }
  OUT(max) BR;
  return 0;
}