Language: C++

#include <iostream>
#include <vector>
using namespace std;
template<class T> inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; }

template<class Monoid> struct RMQ {
    const Monoid INF;
    int SIZE_R;
    vector<pair<Monoid,int> > dat;
    
    RMQ(int n, const Monoid &inf): INF(inf) { init(n); }
    void init(int n) {
        SIZE_R = 1;
        while (SIZE_R < n) SIZE_R *= 2;
        dat.assign(SIZE_R * 2, pair<Monoid,int>(INF, -1));
    }
    
    /* set, a is 0-indexed */
    void set(int a, const Monoid &v) { dat[a + SIZE_R] = make_pair(v, a); }
    void build() {
        for (int k = SIZE_R - 1; k > 0; --k) {
            dat[k] = min(dat[k*2], dat[k*2+1]);
        }
    }
    
    /* update, a is 0-indexed */
    void update(int a, const Monoid &v) {
        int k = a + SIZE_R;
        dat[k] = make_pair(v, a);
        while (k >>= 1) dat[k] = min(dat[k*2], dat[k*2+1]);
    }

    void chmin(int a, const Monoid &v) {
        long long pv = get(a, a+1).first;
        if (pv <= v) return;
        update(a, v);
    }
    
    /* get {min-value, min-index}, a and b are 0-indexed */
    pair<Monoid,int> get(int a, int b) {
        pair<Monoid,int> vleft = make_pair(INF, -1), vright = make_pair(INF, -1);
        for (int left = a + SIZE_R, right = b + SIZE_R; left < right; left >>= 1, right >>= 1) {
            if (left & 1) vleft = min(vleft, dat[left++]);
            if (right & 1) vright = min(dat[--right], vright);
        }
        return min(vleft, vright);
    }
    inline Monoid operator [] (int a) { return dat[a + SIZE_R].first; }
    
    /* debug */
    void print() {
        for (int i = 0; i < SIZE_R; ++i) {
            Monoid val = (*this)[i];
            if (val < INF) cout << val;
            else cout << "INF";
            if (i != SIZE_R-1) cout << ",";
        }
        cout << endl;
    }
};

const long long INF = 1LL<<60;
using pint = pair<int,int>;

int N, Q;
vector<int> b;
vector<vector<int>> rs;

long long f(int i) {
    if (b[i] == 0) return -1;
    else return 1;
}

long long solve() {
    long long offset = 0;
    for (int i = 0; i < N; ++i) if (b[i] == 0) ++offset;

    RMQ<long long> dp(N+2, INF);
    dp.update(0, 0);
    for (auto r : rs[0]) dp.update(r, 0);
    for (int i = 1; i <= N; ++i) {
        for (auto r : rs[i]) {
            long long mi = dp.get(i, r+1).first;
            dp.chmin(r, mi);
            dp.chmin(r, dp.get(i-1, i).first + f(i-1));
        }
        dp.chmin(i, dp.get(i-1, i).first + f(i-1));
    }
    return dp.get(N, N+1).first + offset;
}

int main() {
    while (cin >> N) {
        b.resize(N);
        for (int i = 0; i < N; ++i) cin >> b[i];
        rs.assign(N+1, vector<int>());
        cin >> Q;
        for (int i = 0; i < Q; ++i) {
            int l, r; cin >> l >> r; --l;
            rs[l].push_back(r);
        }
        for (int l = 0; l <= N; ++l)
            sort(rs[l].begin(), rs[l].end(), greater<int>());
        cout << solve() << endl;
    }
}


Compiler output:
a.cc: In function 'int main()':
a.cc:103:13: error: 'sort' was not declared in this scope; did you mean 'short'?
  103 |             sort(rs[l].begin(), rs[l].end(), greater<int>());
      |             ^~~~
      |             short