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

#include <bits/stdc++.h>
using namespace std;
#define int long long
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const int MAX = 200002;
const int MOD = 1000000007;
template <typename T, typename E> struct SegmentTree {
  using F = function<T(T, T)>;
  using G = function<T(T, E)>;
  using H = function<E(E, E)>;
  int n, height;
  F f;
  G g;
  H h;
  T ti;
  E ei;
  vector<T> dat;
  vector<E> laz;
  SegmentTree(int n_, F f, G g, H h, T ti, E ei, vector<T> v = vector<T>())
      : f(f), g(g), h(h), ti(ti), ei(ei) {
    init(n_);
    if (n_ == (int)v.size())
      build(n_, v);
  }

  void init(int n_) {
    n = 1;
    height = 0;
    while (n < n_)
      n <<= 1, height++;
    dat.assign(2 * n, ti);
    laz.assign(2 * n, ei);
  }

  void build(int n_, const vector<T> &v) {
    for (int i = 0; i < n_; i++)
      dat[n + i] = v[i];
    for (int i = n - 1; i; i--)
      dat[i] = f(dat[(i << 1) | 0], dat[(i << 1) | 1]);
  }

  inline T reflect(int k) { return laz[k] == ei ? dat[k] : g(dat[k], laz[k]); }

  inline void propagate(int k) {
    if (laz[k] == ei)
      return;
    laz[(k << 1) | 0] = h(laz[(k << 1) | 0], laz[k]);
    laz[(k << 1) | 1] = h(laz[(k << 1) | 1], laz[k]);
    dat[k] = reflect(k);
    laz[k] = ei;
  }

  inline void thrust(int k) {
    for (int i = height; i; i--)
      propagate(k >> i);
  }

  inline void recalc(int k) {
    while (k >>= 1)
      dat[k] = f(reflect((k << 1) | 0), reflect((k << 1) | 1));
  }

  void update(int a, int b, E x) {
    if (a >= b)
      return;
    thrust(a += n);
    thrust(b += n - 1);
    for (int l = a, r = b + 1; l < r; l >>= 1, r >>= 1) {
      if (l & 1)
        laz[l] = h(laz[l], x), l++;
      if (r & 1)
        --r, laz[r] = h(laz[r], x);
    }
    recalc(a);
    recalc(b);
  }

  void set_val(int a, T x) {
    thrust(a += n);
    dat[a] = x;
    laz[a] = ei;
    recalc(a);
  }

  T query(int a, int b) {
    if (a >= b)
      return ti;
    thrust(a += n);
    thrust(b += n - 1);
    T vl = ti, vr = ti;
    for (int l = a, r = b + 1; l < r; l >>= 1, r >>= 1) {
      if (l & 1)
        vl = f(vl, reflect(l++));
      if (r & 1)
        vr = f(reflect(--r), vr);
    }
    return f(vl, vr);
  }

  template <typename C>
  int find(int st, C &check, T &acc, int k, int l, int r) {
    if (l + 1 == r) {
      acc = f(acc, reflect(k));
      return check(acc) ? k - n : -1;
    }
    propagate(k);
    int m = (l + r) >> 1;
    if (m <= st)
      return find(st, check, acc, (k << 1) | 1, m, r);
    if (st <= l && !check(f(acc, dat[k]))) {
      acc = f(acc, dat[k]);
      return -1;
    }
    int vl = find(st, check, acc, (k << 1) | 0, l, m);
    if (~vl)
      return vl;
    return find(st, check, acc, (k << 1) | 1, m, r);
  }

  template <typename C> int find(int st, C &check) {
    T acc = ti;
    return find(st, check, acc, 1, 0, n);
  }
};
signed main() {
  int h, w;
  cin >> h >> w;
  SegmentTree<pair<int, int>, pair<int, int>> rmq(
      w + 1,
      [](pair<int, int> a, pair<int, int> b) {
        return make_pair(max(a.first, b.first), min(a.second, b.second));
      },
      [](pair<int, int> a, pair<int, int> b) { return b; },
      [](pair<int, int> a, pair<int, int> b) { return b; }, {-2, MOD * MOD},
      {-1, -1});
  for (int i = 0; i < w; i++)
    rmq.update(i, i + 1, make_pair(i, 0));
  int a, b;
  for (int i = 0; i < h; i++) {
    cin >> a >> b;
    a--;
    b--;
    pair<int, int> p = rmq.query(a, b + 1), q;
    //    cerr<<p.first<<' '<<p.second<<endl;
    if (b != w - 1) {
      q = rmq.query(b + 1, b + 2);

      if (p.first != -2) {
        chmax(q.first, p.first);
        chmin(q.second, b + 1 - p.first);
        rmq.update(b + 1, b + 2, q);
      }
    }

    rmq.update(a, b + 1, make_pair(-2, MOD * MOD));

    p = rmq.query(0, w);
    if (p.second == MOD * MOD)
      cout << -1 << endl;
    else
      cout << p.second + i + 1 << endl;
  }
}


Error: TLE

Fixed submission:

#include <bits/stdc++.h>
using namespace std;
#define int long long
template <class T> bool chmax(T &a, const T &b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <class T> bool chmin(T &a, const T &b) {
  if (b < a) {
    a = b;
    return 1;
  }
  return 0;
}
const int MAX = 200002;
const int MOD = 1000000007;
template <typename T, typename E> struct SegmentTree {
  using F = function<T(T, T)>;
  using G = function<T(T, E)>;
  using H = function<E(E, E)>;
  int n, height;
  F f;
  G g;
  H h;
  T ti;
  E ei;
  vector<T> dat;
  vector<E> laz;
  SegmentTree(int n_, F f, G g, H h, T ti, E ei, vector<T> v = vector<T>())
      : f(f), g(g), h(h), ti(ti), ei(ei) {
    init(n_);
    if (n_ == (int)v.size())
      build(n_, v);
  }

  void init(int n_) {
    n = 1;
    height = 0;
    while (n < n_)
      n <<= 1, height++;
    dat.assign(2 * n, ti);
    laz.assign(2 * n, ei);
  }

  void build(int n_, const vector<T> &v) {
    for (int i = 0; i < n_; i++)
      dat[n + i] = v[i];
    for (int i = n - 1; i; i--)
      dat[i] = f(dat[(i << 1) | 0], dat[(i << 1) | 1]);
  }

  inline T reflect(int k) { return laz[k] == ei ? dat[k] : g(dat[k], laz[k]); }

  inline void propagate(int k) {
    if (laz[k] == ei)
      return;
    laz[(k << 1) | 0] = h(laz[(k << 1) | 0], laz[k]);
    laz[(k << 1) | 1] = h(laz[(k << 1) | 1], laz[k]);
    dat[k] = reflect(k);
    laz[k] = ei;
  }

  inline void thrust(int k) {
    for (int i = height; i; i--)
      propagate(k >> i);
  }

  inline void recalc(int k) {
    while (k >>= 1)
      dat[k] = f(reflect((k << 1) | 0), reflect((k << 1) | 1));
  }

  void update(int a, int b, E x) {
    if (a >= b)
      return;
    thrust(a += n);
    thrust(b += n - 1);
    for (int l = a, r = b + 1; l < r; l >>= 1, r >>= 1) {
      if (l & 1)
        laz[l] = h(laz[l], x), l++;
      if (r & 1)
        --r, laz[r] = h(laz[r], x);
    }
    recalc(a);
    recalc(b);
  }

  void set_val(int a, T x) {
    thrust(a += n);
    dat[a] = x;
    laz[a] = ei;
    recalc(a);
  }

  T query(int a, int b) {
    if (a >= b)
      return ti;
    thrust(a += n);
    thrust(b += n - 1);
    T vl = ti, vr = ti;
    for (int l = a, r = b + 1; l < r; l >>= 1, r >>= 1) {
      if (l & 1)
        vl = f(vl, reflect(l++));
      if (r & 1)
        vr = f(reflect(--r), vr);
    }
    return f(vl, vr);
  }

  template <typename C>
  int find(int st, C &check, T &acc, int k, int l, int r) {
    if (l + 1 == r) {
      acc = f(acc, reflect(k));
      return check(acc) ? k - n : -1;
    }
    propagate(k);
    int m = (l + r) >> 1;
    if (m <= st)
      return find(st, check, acc, (k << 1) | 1, m, r);
    if (st <= l && !check(f(acc, dat[k]))) {
      acc = f(acc, dat[k]);
      return -1;
    }
    int vl = find(st, check, acc, (k << 1) | 0, l, m);
    if (~vl)
      return vl;
    return find(st, check, acc, (k << 1) | 1, m, r);
  }

  template <typename C> int find(int st, C &check) {
    T acc = ti;
    return find(st, check, acc, 1, 0, n);
  }
};
signed main() {
  int h, w;
  cin >> h >> w;
  SegmentTree<pair<int, int>, pair<int, int>> rmq(
      w + 1,
      [](pair<int, int> a, pair<int, int> b) {
        return make_pair(max(a.first, b.first), min(a.second, b.second));
      },
      [](pair<int, int> a, pair<int, int> b) { return b; },
      [](pair<int, int> a, pair<int, int> b) { return b; }, {-2, MOD * MOD},
      {-1, -1});
  for (int i = 0; i < w; i++)
    rmq.update(i, i + 1, make_pair(i, 0));
  int a, b;
  for (int i = 0; i < h; i++) {
    cin >> a >> b;
    a--;
    b--;
    pair<int, int> p = rmq.query(a, b + 1), q;
    //    cerr<<p.first<<' '<<p.second<<endl;
    if (b != w - 1) {
      q = rmq.query(b + 1, b + 2);

      if (p.first != -2) {
        chmax(q.first, p.first);
        chmin(q.second, b + 1 - p.first);
        rmq.update(b + 1, b + 2, q);
      }
    }
    for (int j = 0; j < w; j++) {
      p = rmq.query(0, j + 1);
      //  cerr<<p.first<<' '<<p.second<<' ';
    }
    rmq.update(a, b + 1, make_pair(-2, MOD * MOD));
    for (int j = 0; j < w; j++) {
      p = rmq.query(j, j + 1);
      // cerr<<p.first<<' '<<p.second<<' ';
    }
    p = rmq.query(0, w);
    if (p.second == MOD * MOD)
      cout << -1 << endl;
    else
      cout << p.second + i + 1 << endl;
  }
}