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

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <chrono>
#include <climits>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

template <class T1, class T2>
ostream &operator<<(ostream &out, const pair<T1, T2> p) {
  out << '(' << p.first << ',' << p.second << ')';
  return out;
}

template <class T1, class T2>
istream &operator>>(istream &in, pair<T1, T2> &p) {
  in >> p.first >> p.second;
  return in;
}

template <class T> istream &operator>>(istream &in, vector<T> &v) {
  for (T &x : v)
    in >> x;
  return in;
}

template <class T>
ostream &operator<<(ostream &out, const vector<vector<T>> &v) {
  for (const vector<T> &x : v)
    out << x << '\\n';
  return out;
}

template <class T> ostream &operator<<(ostream &out, const vector<T> &v) {
  for (const T &x : v)
    out << x << ' ';
  return out;
}

long long gcd(long long a, long long b) {
  if (b > a)
    swap(a, b);
  return (b ? gcd(b, a % b) : a);
}

using ll = long long;
using pii = pair<int, int>;
using pll = pair<long long, long long>;
using tiii = pair<pair<int, int>, int>;
using vi = vector<int>;
using vl = vector<long long>;
using vvi = vector<vector<int>>;
using vvl = vector<vector<long long>>;

#define F first
#define S second
#define First first.first
#define Second first.second
#define Third second
#define mp make_pair
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define per(i, b, a) for (int i = (b); i > (a); i--)
#define all(x) x.begin(), x.end()
#define ret(x) return cout << x, 0;
#define throwex throw runtime_error("Found the error.");

// const int h = 1000000007;

template <class elem_T = long long, class upd_T = long long> class SegTree {
private:
  const int underlying_array_size;
  const elem_T merge_identity = INT_MAX;
  const upd_T update_identity = 0;
  elem_T merge(const elem_T x, const elem_T y) { return min(x, y); }
  elem_T update_elem(const elem_T x, const upd_T y) { return x + y; }
  upd_T update_lazy(const upd_T x, const upd_T y) { return x + y; }
  vector<elem_T> seg_tree;
  vector<upd_T> lazy;
  void build(int node, int start, int end, vector<elem_T> &v) {
    if (start == end)
      seg_tree[node] = v[start];
    else {
      const int mid = (start + end) / 2;
      build(2 * node + 1, start, mid, v);
      build(2 * node + 2, mid + 1, end, v);
      seg_tree[node] = merge(seg_tree[2 * node + 1], seg_tree[2 * node + 2]);
    }
  }
  void update_range(int node, int start, int end, int l, int r, upd_T val) {
    if (l <= start and end <= r)
      lazy[node] = update_lazy(lazy[node], val);
    if (lazy[node] != update_identity) {
      if (start != end) {
        lazy[2 * node + 1] = update_lazy(lazy[2 * node + 1], lazy[node]);
        lazy[2 * node + 2] = update_lazy(lazy[2 * node + 2], lazy[node]);
      }
      seg_tree[node] = update_elem(seg_tree[node], lazy[node]);
      lazy[node] = update_identity;
    }
    if (end < l or start > r)
      return;
    if (l <= start and end <= r)
      return;
    const int mid = (start + end) / 2;
    update_range(2 * node + 1, start, mid, l, r, val);
    update_range(2 * node + 2, mid + 1, end, l, r, val);
    seg_tree[node] = merge(seg_tree[2 * node + 1], seg_tree[2 * node + 2]);
  }
  elem_T query(int node, int start, int end, int l, int r) {
    if (end < l or start > r)
      return merge_identity;
    if (lazy[node] != update_identity) {
      if (start != end) {
        lazy[2 * node + 1] = update_lazy(lazy[2 * node + 1], lazy[node]);
        lazy[2 * node + 2] = update_lazy(lazy[2 * node + 2], lazy[node]);
      }
      seg_tree[node] = update_elem(seg_tree[node], lazy[node]);
      lazy[node] = update_identity;
    }
    if (l <= start and end <= r)
      return seg_tree[node];
    const int mid = (start + end) / 2;
    return merge(query(2 * node + 1, start, mid, l, r),
                 query(2 * node + 2, mid + 1, end, l, r));
  }

public:
  SegTree(const int n) : underlying_array_size(n) {
    vector<elem_T> v(underlying_array_size, merge_identity);
    seg_tree.resize(4 * underlying_array_size, merge_identity);
    lazy.resize(4 * underlying_array_size, update_identity);
    build(0, 0, underlying_array_size - 1, v);
  }
  SegTree(vector<elem_T> &v) : underlying_array_size(v.size()) {
    seg_tree.resize(4 * underlying_array_size, merge_identity);
    lazy.resize(4 * underlying_array_size, update_identity);
    build(0, 0, underlying_array_size - 1, v);
  }
  void update_range(int l, int r, upd_T val) {
    update_range(0, 0, underlying_array_size - 1, l, r, val);
  }
  elem_T query(int l, int r) {
    return query(0, 0, underlying_array_size - 1, l, r);
  }
};

signed main() {
  ios::sync_with_stdio(false);
#ifdef ONLINE_JUDGE
  cin.tie(nullptr);
  cerr.setstate(ios::failbit);
#endif

  int h, w;
  cin >> h >> w;

  SegTree<> seg(w + 1);
  seg.update_range(1, w, 1 - INT_MAX);
  set<int> s;
  rep(i, 1, w + 1) s.insert(i);
  rep(i, 0, h) {
    int a, b;
    cin >> a >> b;
    s.insert(b + 1);
    while (1) {
      if (a > b)
        break;
      auto x = s.upper_bound(a);
      int en = (x == s.end() ? w : *x - 1);
      seg.update_range(a, en, seg.query(a - 1, a - 1) + 1 - seg.query(a, a));
      s.erase(a);
      a = en + 1;
    }
    cout << (seg.query(0, w) >= INT_MAX ? -1 : seg.query(0, w)) << '\\n';
    seg.update_range(0, w, 1);
  }
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <array>
#include <bitset>
#include <cassert>
#include <cfloat>
#include <chrono>
#include <climits>
#include <cmath>
#include <cstdio>
#include <deque>
#include <iomanip>
#include <iostream>
#include <map>
#include <numeric>
#include <queue>
#include <random>
#include <set>
#include <sstream>
#include <stack>
#include <stdexcept>
#include <unordered_map>
#include <unordered_set>
#include <vector>
using namespace std;

template <class T1, class T2>
ostream &operator<<(ostream &out, const pair<T1, T2> p) {
  out << '(' << p.first << ',' << p.second << ')';
  return out;
}

template <class T1, class T2>
istream &operator>>(istream &in, pair<T1, T2> &p) {
  in >> p.first >> p.second;
  return in;
}

template <class T> istream &operator>>(istream &in, vector<T> &v) {
  for (T &x : v)
    in >> x;
  return in;
}

template <class T>
ostream &operator<<(ostream &out, const vector<vector<T>> &v) {
  for (const vector<T> &x : v)
    out << x << '\\n';
  return out;
}

template <class T> ostream &operator<<(ostream &out, const vector<T> &v) {
  for (const T &x : v)
    out << x << ' ';
  return out;
}

long long gcd(long long a, long long b) {
  if (b > a)
    swap(a, b);
  return (b ? gcd(b, a % b) : a);
}

using ll = long long;
using pii = pair<int, int>;
using pll = pair<long long, long long>;
using tiii = pair<pair<int, int>, int>;
using vi = vector<int>;
using vl = vector<long long>;
using vvi = vector<vector<int>>;
using vvl = vector<vector<long long>>;

#define F first
#define S second
#define First first.first
#define Second first.second
#define Third second
#define mp make_pair
#define rep(i, a, b) for (int i = (a); i < (b); i++)
#define per(i, b, a) for (int i = (b); i > (a); i--)
#define all(x) x.begin(), x.end()
#define ret(x) return cout << x, 0;
#define throwex throw runtime_error("Found the error.");

// const int h = 1000000007;

template <class elem_T = long long, class upd_T = long long> class SegTree {
private:
  const int underlying_array_size;
  const elem_T merge_identity = INT_MAX;
  const upd_T update_identity = 0;
  elem_T merge(const elem_T x, const elem_T y) { return min(x, y); }
  elem_T update_elem(const elem_T x, const upd_T y) { return x + y; }
  upd_T update_lazy(const upd_T x, const upd_T y) { return x + y; }
  vector<elem_T> seg_tree;
  vector<upd_T> lazy;
  void build(int node, int start, int end, vector<elem_T> &v) {
    if (start == end)
      seg_tree[node] = v[start];
    else {
      const int mid = (start + end) / 2;
      build(2 * node + 1, start, mid, v);
      build(2 * node + 2, mid + 1, end, v);
      seg_tree[node] = merge(seg_tree[2 * node + 1], seg_tree[2 * node + 2]);
    }
  }
  void update_range(int node, int start, int end, int l, int r, upd_T val) {
    if (l <= start and end <= r)
      lazy[node] = update_lazy(lazy[node], val);
    if (lazy[node] != update_identity) {
      if (start != end) {
        lazy[2 * node + 1] = update_lazy(lazy[2 * node + 1], lazy[node]);
        lazy[2 * node + 2] = update_lazy(lazy[2 * node + 2], lazy[node]);
      }
      seg_tree[node] = update_elem(seg_tree[node], lazy[node]);
      lazy[node] = update_identity;
    }
    if (end < l or start > r)
      return;
    if (l <= start and end <= r)
      return;
    const int mid = (start + end) / 2;
    update_range(2 * node + 1, start, mid, l, r, val);
    update_range(2 * node + 2, mid + 1, end, l, r, val);
    seg_tree[node] = merge(seg_tree[2 * node + 1], seg_tree[2 * node + 2]);
  }
  elem_T query(int node, int start, int end, int l, int r) {
    if (end < l or start > r)
      return merge_identity;
    if (lazy[node] != update_identity) {
      if (start != end) {
        lazy[2 * node + 1] = update_lazy(lazy[2 * node + 1], lazy[node]);
        lazy[2 * node + 2] = update_lazy(lazy[2 * node + 2], lazy[node]);
      }
      seg_tree[node] = update_elem(seg_tree[node], lazy[node]);
      lazy[node] = update_identity;
    }
    if (l <= start and end <= r)
      return seg_tree[node];
    const int mid = (start + end) / 2;
    return merge(query(2 * node + 1, start, mid, l, r),
                 query(2 * node + 2, mid + 1, end, l, r));
  }

public:
  SegTree(const int n) : underlying_array_size(n) {
    vector<elem_T> v(underlying_array_size, merge_identity);
    seg_tree.resize(4 * underlying_array_size, merge_identity);
    lazy.resize(4 * underlying_array_size, update_identity);
    build(0, 0, underlying_array_size - 1, v);
  }
  SegTree(vector<elem_T> &v) : underlying_array_size(v.size()) {
    seg_tree.resize(4 * underlying_array_size, merge_identity);
    lazy.resize(4 * underlying_array_size, update_identity);
    build(0, 0, underlying_array_size - 1, v);
  }
  void update_range(int l, int r, upd_T val) {
    update_range(0, 0, underlying_array_size - 1, l, r, val);
  }
  elem_T query(int l, int r) {
    return query(0, 0, underlying_array_size - 1, l, r);
  }
};

signed main() {
  ios::sync_with_stdio(false);
#ifdef ONLINE_JUDGE
  cin.tie(nullptr);
  cerr.setstate(ios::failbit);
#endif

  int h, w;
  cin >> h >> w;

  SegTree<> seg(w + 1);
  seg.update_range(1, w, 1 - INT_MAX);
  set<int> s;
  rep(i, 1, w + 1) s.insert(i);
  // rep(i,0,w+1)
  //     cerr << seg.query(i, i) << ' ';
  // cerr << '\\n';
  rep(i, 0, h) {
    int a, b;
    cin >> a >> b;
    while (1) {
      if (a > b)
        break;
      auto x = s.upper_bound(a);
      int en = (x == s.end() ? w : *x - 1);
      seg.update_range(a, en, seg.query(a - 1, a - 1) + 1 - seg.query(a, a));
      a = en + 1;
      // cerr << a << ' ' << b << '\\n';
    }
    // rep(i,0,w+1)
    //     cerr << seg.query(i, i) << ' ';
    // cerr << '\\n';
    cout << (seg.query(0, w) >= INT_MAX ? -1 : seg.query(0, w)) << '\\n';
    seg.update_range(0, w, 1);
  }
}