Problem p03554 (C++)

Buggy submission (status: Time Limit Exceeded):
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

#define pb push_back
#define mp make_pair
#define fs first
#define sc second
#define sz(a) ((int)(a).size())
#define eprintf(...) fprintf(stderr, __VA_ARGS__)

using namespace std;

typedef long long int64;
typedef long double ldb;

const long double eps = 1e-9;
const int inf = (1 << 30) - 1;
const long long inf64 = ((long long)1 << 62) - 1;
const long double pi = acos(-1);

template <class T> T sqr(T x) { return x * x; }
template <class T> T abs(T x) { return x < 0 ? -x : x; }

vector<int> tree;

int Get(int v, int vleft, int vright, int pos) {
  if (vleft + 1 == vright) {
    return tree[v];
  }

  int vmid = (vleft + vright) / 2;
  if (pos < vmid) {
    return min(Get(2 * v + 1, vleft, vmid, pos), tree[v]);
  } else {
    return min(Get(2 * v + 2, vmid, vright, pos), tree[v]);
  }
}

void Set(int v, int vleft, int vright, int pos, int val) {
  if (vleft + 1 == vright) {
    tree[v] = val;
    return;
  }

  int vmid = (vleft + vright) / 2;
  if (pos < vmid) {
    tree[2 * v + 2] = min(tree[v], tree[2 * v + 2]);
    Set(2 * v + 1, vleft, vmid, pos, val);
  } else {
    tree[2 * v + 1] = min(tree[v], tree[2 * v + 1]);
    Set(2 * v + 2, vmid, vright, pos, val);
  }
  tree[v] = inf;
}

void Umn(int v, int vleft, int vright, int left, int right, int val) {
  if (vleft + 1 == vright) {
    tree[v] = min(tree[v], val);
    return;
  }

  int vmid = (vleft + vright) / 2;

  if (left < vmid) {
    Umn(2 * v + 1, vleft, vmid, left, min(right, vmid), val);
  }
  if (right > vmid) {
    Umn(2 * v + 2, vmid, vright, max(left, vmid), right, val);
  }
}

int main() {
  //  ios_base::sync_with_stdio(0);
  //	freopen("input.txt", "rt", stdin);
  //	freopen("output.txt", "wt", stdout);

  int n;
  scanf("%d", &n);

  vector<int> b(n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &b[i]);
  }

  int m;
  scanf("%d", &m);
  vector<vector<int>> seg(n);
  for (int i = 0; i < m; ++i) {
    int left, right;
    scanf("%d%d", &left, &right);
    --left;
    seg[left].pb(right);
  }

  int num_0 = 0;
  for (int i = 0; i < n; ++i) {
    num_0 += (b[i] == 0);
  }

  tree.resize(4 * (n + 1), inf);

  Set(0, 0, n + 1, n, 0);
  for (int i = n - 1; i >= 0; --i) {
    Set(0, 0, n + 1, i, Get(0, 0, n + 1, i + 1) + (b[i] == 1 ? 1 : -1));
    for (int j = 0; j < sz(seg[i]); ++j) {
      Umn(0, 0, n + 1, i, seg[i][j], Get(0, 0, n + 1, seg[i][j]));
    }
  }

  printf("%d\n", num_0 + Get(0, 0, n + 1, 0));

  return 0;
}


Fixed submission:
#include <algorithm>
#include <bitset>
#include <cassert>
#include <cmath>
#include <cstdio>
#include <cstring>
#include <ctime>
#include <iostream>
#include <map>
#include <queue>
#include <set>
#include <sstream>
#include <string>
#include <vector>

#define pb push_back
#define mp make_pair
#define fs first
#define sc second
#define sz(a) ((int)(a).size())
#define eprintf(...) fprintf(stderr, __VA_ARGS__)

using namespace std;

typedef long long int64;
typedef long double ldb;

const long double eps = 1e-9;
const int inf = (1 << 30) - 1;
const long long inf64 = ((long long)1 << 62) - 1;
const long double pi = acos(-1);

template <class T> T sqr(T x) { return x * x; }
template <class T> T abs(T x) { return x < 0 ? -x : x; }

vector<int> tree;

int Get(int v, int vleft, int vright, int pos) {
  if (vleft + 1 == vright) {
    return tree[v];
  }

  int vmid = (vleft + vright) / 2;
  if (pos < vmid) {
    return min(Get(2 * v + 1, vleft, vmid, pos), tree[v]);
  } else {
    return min(Get(2 * v + 2, vmid, vright, pos), tree[v]);
  }
}

void Set(int v, int vleft, int vright, int pos, int val) {
  if (vleft + 1 == vright) {
    tree[v] = val;
    return;
  }

  int vmid = (vleft + vright) / 2;
  if (pos < vmid) {
    tree[2 * v + 2] = min(tree[v], tree[2 * v + 2]);
    Set(2 * v + 1, vleft, vmid, pos, val);
  } else {
    tree[2 * v + 1] = min(tree[v], tree[2 * v + 1]);
    Set(2 * v + 2, vmid, vright, pos, val);
  }
  tree[v] = inf;
}

void Umn(int v, int vleft, int vright, int left, int right, int val) {
  if (left == vleft && right == vright) {
    tree[v] = min(tree[v], val);
    return;
  }

  int vmid = (vleft + vright) / 2;

  if (left < vmid) {
    Umn(2 * v + 1, vleft, vmid, left, min(right, vmid), val);
  }
  if (right > vmid) {
    Umn(2 * v + 2, vmid, vright, max(left, vmid), right, val);
  }
}

int main() {
  //  ios_base::sync_with_stdio(0);
  //	freopen("input.txt", "rt", stdin);
  //	freopen("output.txt", "wt", stdout);

  int n;
  scanf("%d", &n);

  vector<int> b(n);
  for (int i = 0; i < n; ++i) {
    scanf("%d", &b[i]);
  }

  int m;
  scanf("%d", &m);
  vector<vector<int>> seg(n);
  for (int i = 0; i < m; ++i) {
    int left, right;
    scanf("%d%d", &left, &right);
    --left;
    seg[left].pb(right);
  }

  int num_0 = 0;
  for (int i = 0; i < n; ++i) {
    num_0 += (b[i] == 0);
  }

  tree.resize(4 * (n + 1), inf);

  Set(0, 0, n + 1, n, 0);
  for (int i = n - 1; i >= 0; --i) {
    Set(0, 0, n + 1, i, Get(0, 0, n + 1, i + 1) + (b[i] == 1 ? 1 : -1));
    for (int j = 0; j < sz(seg[i]); ++j) {
      Umn(0, 0, n + 1, i, seg[i][j], Get(0, 0, n + 1, seg[i][j]));
    }
  }

  printf("%d\n", num_0 + Get(0, 0, n + 1, 0));

  return 0;
}