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

#include <algorithm>
#include <assert.h>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
using namespace std;
#define rep(i, a, n) for (int i = a; i < n; i++)
#define per(i, a, n) for (int i = n - 1; i >= a; i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define INF 0x3f3f3f3f3f3f3f3f
typedef vector<int> VI;
typedef long long ll;
typedef pair<int, int> PII;
typedef double db;
const ll mod = 1000000007;
const double PI = acos(-1.0);
const double epsilon = PI / 180.0; // 角度转弧度
ll powmod(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
const int N = 2e5 + 10, M = 1e6 + 10;
int h, w;
int a[N], b[N];
struct SegTree {
  struct node {
    int l, r;
    ll mn, add;
  } tr[N * 4];
  void build(int u, int l, int r) {
    if (l == r) {
      tr[u] = {l, r, 0, 0};
    } else {
      int mid = l + r >> 1;
      tr[u] = {l, r};
      build(u << 1, l, mid);
      build(u << 1 | 1, mid + 1, r);
    }
  }
  void pushup(int u) { tr[u].mn = min(tr[u << 1].mn, tr[u << 1 | 1].mn); }
  void pushdown(int u) {
    if (tr[u].add) {
      tr[u << 1].mn += tr[u].add;
      tr[u << 1 | 1].mn += tr[u].add;
      tr[u << 1].add += tr[u].add;
      tr[u << 1 | 1].add += tr[u].add;
      tr[u].add = 0;
    }
  }
  void modify(int u, int l, int r, ll d) {
    if (tr[u].l >= l && tr[u].r <= r) {
      tr[u].mn += d;
      tr[u].add += d;
    } else {
      int mid = tr[u].l + tr[u].r >> 1;
      pushdown(u);
      if (l <= mid)
        modify(u << 1, l, r, d);
      if (r > mid)
        modify(u << 1 | 1, l, r, d);
      pushup(u);
    }
  }
  ll query(int u, int l, int r) {
    if (tr[u].l >= l && tr[u].r <= r) {
      return tr[u].mn;
    } else {
      int mid = tr[u].l + tr[u].r >> 1;
      pushdown(u);
      ll ans = INF;
      if (l <= mid)
        ans = min(ans, query(u << 1, l, r));
      if (r > mid)
        ans = min(ans, query(u << 1 | 1, l, r));
      return ans;
    }
  }
} x, y;
int main() {
  cin >> h >> w;
  for (int i = 1; i <= h; i++) {
    scanf("%d %d", &a[i], &b[i]);
  }
  x.build(1, 1, w), y.build(1, 1, w);
  for (int i = 1; i <= w; i++) {
    y.modify(1, i, i, -i);
  }
  int f1 = 0;
  for (int i = 1; i <= h; i++) {
    if (f1) {
      cout << "-1\\n";
      continue;
    }
    if (b[i] + 1 <= w)
      x.modify(1, b[i] + 1, b[i] + 1,
               min(1ll * (b[i] + 1 + y.query(1, 1, b[i])) -
                       x.query(1, b[i] + 1, b[i] + 1),
                   0ll));
    if (b[i] + 1 <= w)
      y.modify(1, b[i] + 1, b[i] + 1,
               min(y.query(1, 1, b[i]) - y.query(1, b[i] + 1, b[i] + 1), 0ll));
    x.modify(1, a[i], b[i], 1e9);
    y.modify(1, a[i], b[i], 1e9);
    x.modify(1, 1, w, 1);
    y.modify(1, 1, w, 1);
    int ans = x.query(1, 1, w);
    // cout<<y.query(1, 1, w)<<"\\n";
    if (ans >= 1e9) {
      f1 = 1;
      cout << "-1\\n";
    } else {
      cout << ans << "\\n";
    }
  }
  return 0;
}


Error: TLE

Fixed submission:

#include <algorithm>
#include <assert.h>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <stack>
using namespace std;
#define rep(i, a, n) for (int i = a; i < n; i++)
#define per(i, a, n) for (int i = n - 1; i >= a; i--)
#define pb push_back
#define mp make_pair
#define all(x) (x).begin(), (x).end()
#define fi first
#define se second
#define SZ(x) ((int)(x).size())
#define INF 0x3f3f3f3f
typedef vector<int> VI;
typedef long long ll;
typedef pair<int, int> PII;
typedef double db;
const ll mod = 1000000007;
const double PI = acos(-1.0);
const double epsilon = PI / 180.0; // 角度转弧度
ll powmod(ll a, ll b) {
  ll res = 1;
  a %= mod;
  assert(b >= 0);
  for (; b; b >>= 1) {
    if (b & 1)
      res = res * a % mod;
    a = a * a % mod;
  }
  return res;
}
ll gcd(ll a, ll b) { return b ? gcd(b, a % b) : a; }
const int N = 2e5 + 10, M = 1e6 + 10;
int h, w;
int a[N], b[N];
struct node {
  int l, r;
  int mn, add1, x, add2;
} tr[N * 4];
void build(int u, int l, int r) {
  if (l == r) {
    tr[u] = {l, r, 0};
  } else {
    int mid = l + r >> 1;
    tr[u] = {l, r};
    build(u << 1, l, mid);
    build(u << 1 | 1, mid + 1, r);
  }
}
void pushup(int u) { tr[u].mn = min(tr[u << 1].mn, tr[u << 1 | 1].mn); }
void pushdown1(int u) {
  if (tr[u].add1) {
    tr[u << 1].mn += tr[u].add1;
    tr[u << 1 | 1].mn += tr[u].add1;
    tr[u << 1].add1 += tr[u].add1;
    tr[u << 1 | 1].add1 += tr[u].add1;
    tr[u].add1 = 0;
  }
}
void pushdown2(int u) {
  if (tr[u].x) {
    tr[u << 1].mn = abs(tr[u].x - tr[u << 1].l) + tr[u].add2;
    tr[u << 1 | 1].mn = abs(tr[u << 1 | 1].l - tr[u].x) + tr[u].add2;
    tr[u << 1].x = tr[u].x;
    tr[u << 1].add2 = tr[u].add2;
    tr[u << 1 | 1].x = tr[u].x;
    tr[u << 1 | 1].add2 = tr[u].add2;
    tr[u].x = 0;
    tr[u].add2 = 0;
  }
}
void modify1(int u, int l, int r) {
  if (tr[u].l >= l && tr[u].r <= r) {
    tr[u].mn += 1;
    tr[u].add1 += 1;
  } else {
    int mid = tr[u].l + tr[u].r >> 1;
    // pushdown2(u);
    pushdown1(u);
    if (l <= mid)
      modify1(u << 1, l, r);
    if (r > mid)
      modify1(u << 1 | 1, l, r);
    pushup(u);
  }
}
void modify2(int u, int x, int d) {
  if (tr[u].l == x && tr[u].r == x) {
    tr[u].mn = d;
  } else {
    int mid = tr[u].l + tr[u].r >> 1;
    // pushdown2(u);
    pushdown1(u);
    if (x <= mid)
      modify2(u << 1, x, d);
    if (x > mid)
      modify2(u << 1 | 1, x, d);
    pushup(u);
  }
}
int query(int u, int l, int r) {
  if (tr[u].l >= l && tr[u].r <= r) {
    return tr[u].mn;
  } else {
    int mid = tr[u].l + tr[u].r >> 1;
    // pushdown2(u);
    pushdown1(u);
    int ans = INF;
    if (l <= mid)
      ans = min(ans, query(u << 1, l, r));
    if (r > mid)
      ans = min(ans, query(u << 1 | 1, l, r));
    return ans;
  }
}
int main() {
  cin >> h >> w;
  for (int i = 1; i <= h; i++) {
    scanf("%d %d", &a[i], &b[i]);
  }
  build(1, 1, w);
  int st = 1, f1 = 0;
  for (int i = 1; i <= h; i++) {
    if (f1) {
      cout << "-1\\n";
      continue;
    }
    int res = INF, tmp = st;
    if (st <= a[i] - 1) {
      res = min(res, query(1, st, a[i] - 1));
    } else {
      tmp = max(st, b[i] + 1);
    }
    if (st <= b[i] && b[i] + 1 <= w) {
      res = min(res, query(1, b[i] + 1, w));
    } else if (st > b[i] && b[i] + 1 <= w && st <= w) {
      res = min(res, query(1, st, w));
    }
    if (res == INF) {
      cout << "-1\\n";
      f1 = 1;
    } else {
      cout << res + 1 << "\\n";
    }
    if (st <= a[i] - 1) {
      modify1(1, st, a[i] - 1);
      for (int k = a[i]; k <= b[i]; k++)
        modify2(1, k, k - a[i] + 1 + query(1, a[i] - 1, a[i] - 1));
      if (b[i] + 1 <= w)
        modify1(1, b[i] + 1, w);
    } else {
      if (b[i] + 1 <= w)
        modify1(1, st, w);
    }
    st = tmp;
  }
  return 0;
}