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

#include <bits/stdc++.h>

#define si(a) scanf("%d", &a)
#define sl(a) scanf("%lld", &a)
#define sd(a) scanf("%lf", &a)
#define sc(a) scahf("%c", &a);
#define ss(a) scanf("%s", a)
#define pi(a) printf("%d\\n", a)
#define pl(a) printf("%lld\\n", a)
#define pc(a) putchar(a)
#define ms(a) memset(a, 0, sizeof(a))
#define repi(i, a, b) for (register int i = a; i <= b; ++i)
#define repd(i, a, b) for (register int i = a; i >= b; --i)
#define reps(s) for (register int i = head[s]; i; i = Next[i])
#define ll long long
#define ull unsigned long long
#define vi vector<int>
#define pii pair<int, int>
#define mii unordered_map<int, int>
#define msi unordered_map<string, int>
#define lowbit(x) ((x) & (-(x)))
#define ce(i, r) i == r ? '\\n' : ' '
#define pb push_back
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define INF 0x3f3f3f3f
#define pr(x) cout << #x << ": " << x << endl
using namespace std;

inline int qr() {
  int f = 0, fu = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      fu = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    f = (f << 3) + (f << 1) + c - 48;
    c = getchar();
  }
  return f * fu;
}

const int N = 2e5 + 10;

struct Seg_Tree {
  struct T {
    int l, r, ans, add;
  } t[N * 4];

  void build(int p, int l, int r) {
    t[p].l = l, t[p].r = r, t[p].ans = t[p].add = 0;
    if (l == r)
      return;
    int mid = (l + r) >> 1;
    build(p << 1, l, mid);
    build(p << 1 | 1, mid + 1, r);
  }

  void spread(int p) {
    if (t[p].add) {
      t[p << 1].ans = t[p].l - t[p].add;
      t[p << 1].add = t[p].add;
      t[p << 1 | 1].ans = ((t[p].l + t[p].r) >> 1) + 1 - t[p].add;
      t[p << 1 | 1].add = t[p].add;
      t[p].add = 0;
    }
  }

  void change(int p, int l, int r, int c) {
    if (l <= t[p].l && t[p].r <= r) {
      t[p].ans = t[p].l - c, t[p].add = c;
      return;
    }
    spread(p);
    int mid = (t[p].l + t[p].r) >> 1;
    if (l <= mid)
      change(p << 1, l, r, c);
    if (r > mid)
      change(p << 1 | 1, l, r, c);
    t[p].ans = min(t[p << 1].ans, t[p << 1 | 1].ans);
  }

  int ask(int p, int x) {
    if (t[p].l == t[p].r)
      return x - t[p].ans;
    spread(p);
    int mid = (t[p].l + t[p].r) >> 1;
    return x <= mid ? ask(p << 1, x) : ask(p << 1 | 1, x);
  }
} tr;

int n, m, now = 1;

int main() {
  n = qr(), m = qr();
  tr.build(1, 1, m);
  repi(i, 1, n) {
    int l = qr(), r = qr();
    if (now >= l && now <= r)
      tr.change(1, now, r, -INF), now = r + 1;
    else if (now < l)
      tr.change(1, l, r, tr.ask(1, l - 1));
    if (now > m) {
      repi(j, i, n) puts("-1");
      return 0;
    }
    pi(i + tr.t[1].ans);
  }
  return 0;
}

Error: TLE

Fixed submission:

#include <bits/stdc++.h>

#define si(a) scanf("%d", &a)
#define sl(a) scanf("%lld", &a)
#define sd(a) scanf("%lf", &a)
#define sc(a) scahf("%c", &a);
#define ss(a) scanf("%s", a)
#define pi(a) printf("%d\\n", a)
#define pl(a) printf("%lld\\n", a)
#define pc(a) putchar(a)
#define ms(a) memset(a, 0, sizeof(a))
#define repi(i, a, b) for (register int i = a; i <= b; ++i)
#define repd(i, a, b) for (register int i = a; i >= b; --i)
#define reps(s) for (register int i = head[s]; i; i = Next[i])
#define ll long long
#define ull unsigned long long
#define vi vector<int>
#define pii pair<int, int>
#define mii unordered_map<int, int>
#define msi unordered_map<string, int>
#define lowbit(x) ((x) & (-(x)))
#define ce(i, r) i == r ? '\\n' : ' '
#define pb push_back
#define fi first
#define se second
#define all(x) x.begin(), x.end()
#define INF 0x3f3f3f3f
#define pr(x) cout << #x << ": " << x << endl
using namespace std;

inline int qr() {
  int f = 0, fu = 1;
  char c = getchar();
  while (c < '0' || c > '9') {
    if (c == '-')
      fu = -1;
    c = getchar();
  }
  while (c >= '0' && c <= '9') {
    f = (f << 3) + (f << 1) + c - 48;
    c = getchar();
  }
  return f * fu;
}

const int N = 4e5 + 10;

struct Seg_Tree {
  struct T {
    int l, r, ans, add;
  } t[N * 4];

  void build(int p, int l, int r) {
    t[p].l = l, t[p].r = r, t[p].ans = t[p].add = 0;
    if (l == r)
      return;
    int mid = (l + r) >> 1;
    build(p << 1, l, mid);
    build(p << 1 | 1, mid + 1, r);
  }

  void spread(int p) {
    if (t[p].add) {
      t[p << 1].ans = t[p].l - t[p].add;
      t[p << 1].add = t[p].add;
      t[p << 1 | 1].ans = ((t[p].l + t[p].r) >> 1) + 1 - t[p].add;
      t[p << 1 | 1].add = t[p].add;
      t[p].add = 0;
    }
  }

  void change(int p, int l, int r, int c) {
    // if (t[p].r < l || t[p].l > r)return;
    if (l <= t[p].l && t[p].r <= r) {
      t[p].ans = l - c, t[p].add = c;
      return;
    }
    spread(p);
    int mid = (t[p].l + t[p].r) >> 1;
    if (l <= mid)
      change(p << 1, l, r, c);
    if (r > mid)
      change(p << 1 | 1, l, r, c);
    t[p].ans = min(t[p << 1].ans, t[p << 1 | 1].ans);
  }

  int ask(int p, int x) {
    if (t[p].l == t[p].r)
      return x - t[p].ans;
    spread(p);
    int mid = (t[p].l + t[p].r) >> 1;
    return x <= mid ? ask(p >> 1, x) : ask(p << 1 | 1, x);
  }
} tr;

int n, m, now = 1;

int main() {
  n = qr(), m = qr();
  tr.build(1, 1, m);
  repi(i, 1, n) {
    int l = qr(), r = qr();
    if (now >= l && now <= r)
      tr.change(1, now, r, -INF), now = r + 1;
    else if (now < l)
      tr.change(1, l, r, tr.ask(1, l - 1));
    if (now > m) {
      repi(j, i, n) puts("-1");
      return 0;
    }
    pi(i + tr.t[1].ans);
  }
  return 0;
}