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

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;

// template
#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define ALL(v) (v).begin(), (v).end()
typedef long long int ll;
const int inf = 0x3fffffff;
const ll INF = 0x1fffffffffffffff;
const double eps = 1e-12;
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
// end

typedef pair<ll, ll> P;
int n;
vector<ll> seg;
vector<P> laz;
void init(int _n) {
  n = 1;
  while (n < _n)
    n <<= 1;
  seg.assign(n * 2, 0);
  laz.assign(n * 2, P(1, 0));
}
void eval(int k, ll lb) {
  if (k < n) {
    if (laz[k].first) {
      laz[k * 2].second += laz[k].second;
      laz[k * 2 + 1].second += laz[k].second;
    } else {
      laz[k * 2] = laz[k];
      laz[k * 2 + 1] = laz[k];
    }
  }
  if (laz[k].first)
    seg[k] += laz[k].second;
  else
    seg[k] = lb + laz[k].second;
  laz[k] = P(1, 0);
}
void update(int a, int b, int x, int k = 1, int lb = 0, int rb = -1) {
  if (rb == -1)
    rb = n;
  eval(k, lb);
  if (rb <= a or b <= lb)
    return;
  else if (a <= lb and rb <= b) {
    laz[k] = P(0, x);
    eval(k, lb);
  } else {
    int m = (lb + rb) >> 1;
    update(a, b, x, k * 2, lb, m);
    update(a, b, x, k * 2 + 1, m, rb);
    seg[k] = min(seg[k * 2], seg[k * 2 + 1]);
  }
}
ll query(int a, int b, int k = 1, int lb = 0, int rb = -1) {
  if (rb == -1)
    rb = n;
  eval(k, lb);
  if (rb <= a or b <= lb)
    return inf;
  else if (a <= lb and rb <= b)
    return seg[k];
  else
    return min(query(a, b, k * 2, lb, (lb + rb) >> 1),
               query(a, b, k * 2 + 1, (lb + rb) >> 1, rb));
}

int main() {
  int n, q;
  cin >> q >> n;
  init(n);
  rep(i, 0, q) {
    int lb, rb;
    cin >> lb >> rb;
    lb--;
    if (lb == 0)
      update(0, rb, inf);
    else {
      ll val = query(lb - 1, lb) - (lb - 1);
      update(lb, rb, val);
    }
    ll res = query(0, n) + (i + 1);
    if (res >= inf)
      puts("-1");
    else
      cout << res << endl;
  }
  return 0;
}

Error: TLE

Fixed submission:

#define _USE_MATH_DEFINES
#include <bits/stdc++.h>
using namespace std;

// template
#define rep(i, a, b) for (int i = (int)(a); i < (int)(b); i++)
#define ALL(v) (v).begin(), (v).end()
typedef long long int ll;
const int inf = 0x3fffffff;
const ll INF = 0x1fffffffffffffff;
const double eps = 1e-12;
template <typename T> inline bool chmax(T &a, T b) {
  if (a < b) {
    a = b;
    return 1;
  }
  return 0;
}
template <typename T> inline bool chmin(T &a, T b) {
  if (a > b) {
    a = b;
    return 1;
  }
  return 0;
}
// end

struct LazySegmentTree {
private:
  int n;
  vector<int> node, lazy;
  vector<bool> lazyFlag;

public:
  LazySegmentTree(vector<int> v) {
    int sz = (int)v.size();
    n = 1;
    while (n < sz)
      n *= 2;
    node.resize(2 * n - 1);
    lazy.resize(2 * n - 1, inf);
    lazyFlag.resize(2 * n - 1, false);

    for (int i = 0; i < sz; i++)
      node[i + n - 1] = v[i];
    for (int i = n - 2; i >= 0; i--)
      node[i] = min(node[i * 2 + 1], node[i * 2 + 2]);
  }

  void lazyEvaluate(int k, int l, int r) {
    if (lazyFlag[k]) {
      node[k] = lazy[k];
      if (r - l > 1) {
        lazy[k * 2 + 1] = lazy[k * 2 + 2] = lazy[k];
        lazyFlag[k * 2 + 1] = lazyFlag[k * 2 + 2] = true;
      }
      lazyFlag[k] = false;
    }
  }

  void update(int a, int b, int x, int k = 0, int l = 0, int r = -1) {
    if (r < 0)
      r = n;
    lazyEvaluate(k, l, r);
    if (b <= l || r <= a)
      return;
    if (a <= l && r <= b) {
      lazy[k] = x;
      lazyFlag[k] = true;
      lazyEvaluate(k, l, r);
    } else {
      update(a, b, x, 2 * k + 1, l, (l + r) / 2);
      update(a, b, x, 2 * k + 2, (l + r) / 2, r);
      node[k] = min(node[2 * k + 1], node[2 * k + 2]);
    }
  }

  int find(int a, int b, int k = 0, int l = 0, int r = -1) {
    if (r < 0)
      r = n;
    lazyEvaluate(k, l, r);
    if (b <= l || r <= a)
      return inf;
    if (a <= l && r <= b)
      return node[k];
    int vl = find(a, b, 2 * k + 1, l, (l + r) / 2);
    int vr = find(a, b, 2 * k + 2, (l + r) / 2, r);
    return min(vl, vr);
  }
};

int main() {
  int h, w;
  cin >> h >> w;
  vector<int> a(h), b(h);
  rep(i, 0, h) {
    cin >> a[i] >> b[i];
    a[i]--;
    if (b[i] == w)
      b[i] += 3;
  }

  vector<int> tmp(w + 10);
  rep(i, 0, w + 10) tmp[i] = -i;
  LazySegmentTree seg(tmp);

  int cur = 0;
  rep(i, 0, h) {
    int val = (a[i] == 0 ? inf : seg.find(a[i] - 1, a[i]));
    seg.update(a[i], b[i], val);
    while (cur < w) {
      if (seg.find(cur, cur + 1) != inf)
        break;
      cur++;
    }
    if (cur == w)
      puts("-1");
    else
      cout << seg.find(cur, cur + 1) + cur + (i + 1) << endl;

    rep(j, 0, w) { cerr << seg.find(j, j + 1) << ' '; }
    cerr << '\\n';
  }
  return 0;
}