Problem p03686 (C++)

Buggy submission (status: Runtime Error):
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// #pragma GCC optimize("unroll-loops")

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <deque>
#include <iostream>
#include <map>
#include <memory.h>
#include <queue>
#include <random>
#include <set>
#include <stdlib.h>
#include <time.h>
#include <unordered_map>
#include <vector>

#define uint unsigned int
#define ll long long
#define ull unsigned long long
#define ld long double
#define rep(i, l, r) for (int i = l; i < r; i++)
#define repb(i, r, l) for (int i = r; i > l; i--)
#define sz(a) (int)a.size()
#define fi first
#define se second
#define mp(a, b) make_pair(a, b)
#define rank qwertyuio
#define next dfghjk

using namespace std;

inline bool setmin(int &x, int y) { return (y < x) ? x = y, 1 : 0; }
inline bool setmax(int &x, int y) { return (y > x) ? x = y, 1 : 0; }
inline bool setmin(ll &x, ll y) { return (y < x) ? x = y, 1 : 0; }
inline bool setmax(ll &x, ll y) { return (y > x) ? x = y, 1 : 0; }

const int N = 10000;
const int inf = (int)1e9 + 1;
const ll big = (ll)1e18 + 1;
const int P = 239;
const int P1 = 31;
const int P2 = 57;
const int MOD = (int)1e9 + 7;
const int MOD1 = (int)1e9 + 9;
const int MOD2 = 998244353;
const double eps = 1e-9;
const double pi = atan2(0, -1);
const int ABC = 26;

int cnt_v = 1;
ll tree[N * 4], add[N * 4];

void build(int n, int *val) {
  cnt_v = 1;
  while (cnt_v < n)
    cnt_v <<= 1;
  fill(tree, tree + cnt_v * 2 - 1, 0);
  fill(add, add + cnt_v * 2 - 1, 0);
  rep(i, 0, n) {
    tree[cnt_v - 1 + i] = val[i];
    if (i > 0) {
      tree[cnt_v - 1 + i] += tree[cnt_v - 1 + i - 1];
    }
  }
  repb(i, cnt_v - 2, -1) { tree[i] = max(tree[i * 2 + 1], tree[i * 2 + 2]); }
}

void push(int x) {
  tree[(x << 1) + 1] += add[x];
  add[(x << 1) + 1] += add[x];
  tree[(x << 1) + 2] += add[x];
  add[(x << 1) + 2] += add[x];
  add[x] = 0;
}

void upd(int x, int lx, int rx, int l, int r, ll v) {
  if (lx >= r || l >= rx)
    return;
  else if (lx >= l && rx <= r) {
    tree[x] += v;
    add[x] += v;
  } else {
    push(x);
    upd((x << 1) + 1, lx, (lx + rx) >> 1, l, r, v);
    upd((x << 1) + 2, (lx + rx) >> 1, rx, l, r, v);
    tree[x] = max(tree[(x << 1) + 1], tree[(x << 1) + 2]);
  }
}

ll get(int x, int lx, int rx, int l, int r) {
  if (lx >= r || l >= rx)
    return -big;
  else if (lx >= l && rx <= r)
    return tree[x];
  else {
    push(x);
    return max(get(x * 2 + 1, lx, (lx + rx) / 2, l, r),
               get(x * 2 + 2, (lx + rx) / 2, rx, l, r));
  }
}

int val[N];

vector<pair<int, int>> e1[N + 1];
vector<pair<int, int>> e2[N + 1];

int main() {
  // freopen("a.in", "r", stdin);
  // freopen("a.out", "w", stdout);
  // ios_base::sync_with_stdio(0);
  // cin.tie(0);
  // cout.precision(20);
  // ll TL = 10.95 * CLOCKS_PER_SEC;
  // clock_t time = clock();
  int T;
  // scanf("%d", &T);
  T = 1;
  while (T--) {
    int n, m;
    scanf("%d %d", &m, &n);
    rep(i, 0, n) {
      // scanf("%d", &val[i]);
      val[i] = 1;
    }
    rep(i, 0, n) {
      e1[i].clear();
      e2[i].clear();
    }
    rep(i, 0, m) {
      int x, y;
      cin >> x >> y;
      y = n - y + 1;
      if (x + y >= n) {
        continue;
      }
      y = n - y - 1;

      // cout << x << " " << y << endl;

      e1[x].push_back({x, y});
      e2[y + 1].push_back({x, y});
    }
    build(n, val);
    ll ans = m;
    rep(i, 0, n) {
      for (auto &it : e1[i]) {
        int x = it.fi, y = it.se;
        upd(0, 0, cnt_v, x, y + 1, 1);
      }
      for (auto &it : e2[i]) {
        int x = it.fi, y = it.se;
        upd(0, 0, cnt_v, x, y + 1, -1);
      }
      /*rep(j, 0, n) {
          cout << get(0, 0, cnt_v, j, j + 1) << " ";
      }
      cout << endl;
      cout << tree[0] << endl;*/
      setmax(ans, tree[0]);
      upd(0, 0, cnt_v, i, n, -val[i]);
    }
    ll sum = 0;
    rep(i, 0, n) { sum += val[i]; }
    sum = min(0LL, sum - ans);
    cout << m - (m + sum) << "\n";
  }
  return 0;
}


Fixed submission:
// #pragma comment(linker, "/stack:200000000")
// #pragma GCC optimize("Ofast")
// #pragma GCC target("sse,sse2,sse3,ssse3,sse4,popcnt,abm,mmx,avx,tune=native")
// #pragma GCC optimize("unroll-loops")

#include <algorithm>
#include <assert.h>
#include <bitset>
#include <cmath>
#include <deque>
#include <iostream>
#include <map>
#include <memory.h>
#include <queue>
#include <random>
#include <set>
#include <stdlib.h>
#include <time.h>
#include <unordered_map>
#include <vector>

#define uint unsigned int
#define ll long long
#define ull unsigned long long
#define ld long double
#define rep(i, l, r) for (int i = l; i < r; i++)
#define repb(i, r, l) for (int i = r; i > l; i--)
#define sz(a) (int)a.size()
#define fi first
#define se second
#define mp(a, b) make_pair(a, b)
#define rank qwertyuio
#define next dfghjk

using namespace std;

inline bool setmin(int &x, int y) { return (y < x) ? x = y, 1 : 0; }
inline bool setmax(int &x, int y) { return (y > x) ? x = y, 1 : 0; }
inline bool setmin(ll &x, ll y) { return (y < x) ? x = y, 1 : 0; }
inline bool setmax(ll &x, ll y) { return (y > x) ? x = y, 1 : 0; }

const int N = 200000;
const int inf = (int)1e9 + 1;
const ll big = (ll)1e18 + 1;
const int P = 239;
const int P1 = 31;
const int P2 = 57;
const int MOD = (int)1e9 + 7;
const int MOD1 = (int)1e9 + 9;
const int MOD2 = 998244353;
const double eps = 1e-9;
const double pi = atan2(0, -1);
const int ABC = 26;

int cnt_v = 1;
ll tree[N * 4], add[N * 4];

void build(int n, int *val) {
  cnt_v = 1;
  while (cnt_v < n)
    cnt_v <<= 1;
  fill(tree, tree + cnt_v * 2 - 1, 0);
  fill(add, add + cnt_v * 2 - 1, 0);
  rep(i, 0, n) {
    tree[cnt_v - 1 + i] = val[i];
    if (i > 0) {
      tree[cnt_v - 1 + i] += tree[cnt_v - 1 + i - 1];
    }
  }
  repb(i, cnt_v - 2, -1) { tree[i] = max(tree[i * 2 + 1], tree[i * 2 + 2]); }
}

void push(int x) {
  tree[(x << 1) + 1] += add[x];
  add[(x << 1) + 1] += add[x];
  tree[(x << 1) + 2] += add[x];
  add[(x << 1) + 2] += add[x];
  add[x] = 0;
}

void upd(int x, int lx, int rx, int l, int r, ll v) {
  if (lx >= r || l >= rx)
    return;
  else if (lx >= l && rx <= r) {
    tree[x] += v;
    add[x] += v;
  } else {
    push(x);
    upd((x << 1) + 1, lx, (lx + rx) >> 1, l, r, v);
    upd((x << 1) + 2, (lx + rx) >> 1, rx, l, r, v);
    tree[x] = max(tree[(x << 1) + 1], tree[(x << 1) + 2]);
  }
}

ll get(int x, int lx, int rx, int l, int r) {
  if (lx >= r || l >= rx)
    return -big;
  else if (lx >= l && rx <= r)
    return tree[x];
  else {
    push(x);
    return max(get(x * 2 + 1, lx, (lx + rx) / 2, l, r),
               get(x * 2 + 2, (lx + rx) / 2, rx, l, r));
  }
}

int val[N];

vector<pair<int, int>> e1[N + 1];
vector<pair<int, int>> e2[N + 1];

int main() {
  // freopen("a.in", "r", stdin);
  // freopen("a.out", "w", stdout);
  // ios_base::sync_with_stdio(0);
  // cin.tie(0);
  // cout.precision(20);
  // ll TL = 10.95 * CLOCKS_PER_SEC;
  // clock_t time = clock();
  int T;
  // scanf("%d", &T);
  T = 1;
  while (T--) {
    int n, m;
    scanf("%d %d", &m, &n);
    rep(i, 0, n) {
      // scanf("%d", &val[i]);
      val[i] = 1;
    }
    rep(i, 0, n) {
      e1[i].clear();
      e2[i].clear();
    }
    rep(i, 0, m) {
      int x, y;
      cin >> x >> y;
      y = n - y + 1;
      if (x + y >= n) {
        continue;
      }
      y = n - y - 1;

      // cout << x << " " << y << endl;

      e1[x].push_back({x, y});
      e2[y + 1].push_back({x, y});
    }
    build(n, val);
    ll ans = m;
    rep(i, 0, n) {
      for (auto &it : e1[i]) {
        int x = it.fi, y = it.se;
        upd(0, 0, cnt_v, x, y + 1, 1);
      }
      for (auto &it : e2[i]) {
        int x = it.fi, y = it.se;
        upd(0, 0, cnt_v, x, y + 1, -1);
      }
      /*rep(j, 0, n) {
          cout << get(0, 0, cnt_v, j, j + 1) << " ";
      }
      cout << endl;
      cout << tree[0] << endl;*/
      setmax(ans, tree[0]);
      upd(0, 0, cnt_v, i, n, -val[i]);
    }
    ll sum = 0;
    rep(i, 0, n) { sum += val[i]; }
    sum = min(0LL, sum - ans);
    cout << m - (m + sum) << "\n";
  }
  return 0;
}