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

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

#define int long long
#define pb push_back
#define S second
#define F first
#define f(i, n) for (int i = 0; i < n; i++)
#define fast                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define vi vector<int>
#define pii pair<int, int>

const int N = 2e5 + 10;
const int inf = 1e14;

int seg[4 * N];
int lazy[4 * N];

void build(int node, int ss, int se) {
  seg[node] = 0;
  lazy[node] = -1;

  if (ss == se)
    return;

  int mid = (ss + se) / 2;

  build(node * 2 + 1, ss, mid);
  build(node * 2 + 2, mid + 1, se);
}

void push(int node, int ss, int se) {
  if (lazy[node] == -1)
    return;

  seg[node] = lazy[node];

  if (ss != se) {
    int mid = (ss + se) / 2;

    lazy[node * 2 + 1] = lazy[node];
    lazy[node * 2 + 2] = lazy[node] + (mid - ss + 1);
  }

  lazy[node] = -1;
}

void update(int node, int ss, int se, int l, int r, int a) {
  push(node, ss, se);

  if (l > se || r < ss)
    return;

  if (l <= ss && se <= r) {
    lazy[node] = ss - l + a;
    push(node, ss, se);
    return;
  }

  int mid = (ss + se) / 2;

  update(node * 2 + 1, ss, mid, l, r, a);
  update(node * 2 + 2, mid + 1, se, l, r, a);

  seg[node] = min(seg[node * 2 + 1], seg[node * 2 + 2]);
}

int query(int node, int ss, int se, int l, int r) {
  push(node, ss, se);

  if (l > se || r < ss)
    return inf;

  if (l <= ss && se <= r)
    return seg[node];

  int mid = (ss + se) / 2;

  return min(query(node * 2 + 1, ss, mid, l, r),
             query(node * 2 + 2, mid + 1, se, l, r));
}

signed main() {
  fast;

  int n, m;
  cin >> n >> m;

  pii a[n];
  f(i, n) cin >> a[i].F >> a[i].S;
  f(i, n) a[i].F--, a[i].S--;

  build(0, 0, m - 1);

  f(i, n) {
    int val = query(0, 0, m - 1, a[i].F - 1, a[i].F - 1);

    update(0, 0, m - 1, a[i].F, a[i].S, val + 1);

    int ans = seg[0] + i + 1;

    if (ans >= inf)
      ans = -1;

    cout << ans << '\\n';
  }
}

Error: TLE

Fixed submission:

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

#define int long long
#define pb push_back
#define S second
#define F first
#define f(i, n) for (int i = 0; i < n; i++)
#define fast                                                                   \\
  ios_base::sync_with_stdio(0);                                                \\
  cin.tie(0);                                                                  \\
  cout.tie(0)
#define vi vector<int>
#define pii pair<int, int>

const int N = 2e5 + 10;
const int inf = 1e14;

int seg[4 * N];
int lazy[4 * N];

void build(int node, int ss, int se) {
  seg[node] = 0;
  lazy[node] = -1;

  if (ss == se)
    return;

  int mid = (ss + se) / 2;
  build(node * 2 + 1, ss, mid);
  build(node * 2 + 2, mid + 1, se);
}

void push(int node, int ss, int se) {
  if (lazy[node] == -1)
    return;

  seg[node] = lazy[node];

  if (ss != se) {
    int mid = (ss + se) / 2;

    lazy[node * 2 + 1] = lazy[node];
    lazy[node * 2 + 2] = lazy[node] + (mid - ss + 1);
  }

  lazy[node] = -1;
}

void update(int node, int ss, int se, int l, int r, int a) {
  if (ss > se)
    return;

  push(node, ss, se);

  if (l > se || r < ss)
    return;

  if (l <= ss && se <= r) {
    lazy[node] = a;
    push(node, ss, se);
    return;
  }

  int mid = (ss + se) / 2;

  update(node * 2 + 1, ss, mid, l, r, a);
  update(node * 2 + 2, mid + 1, se, l, r, a + mid - l + 1);

  seg[node] = min(seg[node * 2 + 1], seg[node * 2 + 2]);
}

int query(int node, int ss, int se, int l, int r) {
  if (ss > se)
    return inf;

  push(node, ss, se);

  if (l > se || r < ss)
    return inf;

  if (l <= ss && se <= r)
    return seg[node];

  int mid = (ss + se) / 2;

  return min(query(node * 2 + 1, ss, mid, l, r),
             query(node * 2 + 2, mid + 1, se, l, r));
}

int arr[N];

signed main() {
  fast;

  int n, m;
  cin >> n >> m;

  pii a[n];
  f(i, n) cin >> a[i].F >> a[i].S;
  f(i, n) a[i].F--, a[i].S--;
  f(i, n) arr[i] = 0;

  f(i, n) {
    int val = inf;

    if (a[i].F > 0)
      val = arr[a[i].F - 1];

    for (int j = a[i].F; j <= a[i].S; j++)
      arr[j] = ++val;

    int mn = arr[0];

    f(j, n) mn = min(mn, arr[j]);

    int ans = mn + i + 1;

    if (ans >= inf)
      ans = -1;

    cout << ans << '\\n';
  }
}