Language: C++

#include <iostream>
#include <algorithm>
#include <vector>
#include <string.h>
#include <cmath>
#include <queue>
using namespace std;
#define mod (long)(1e9 + 7)
#define all(x) (x).begin(), (x).end()
#define bitcount(n) __builtin_popcountl(long(n))
#define fcout cout << fixed << setprecision(15)
#define highest(x) (63 - __builtin_clzl(x))
template<class T> inline void YES(T condition){ if(condition) cout << "YES" << endl; else cout << "NO" << endl; }
template<class T> inline void Yes(T condition){ if(condition) cout << "Yes" << endl; else cout << "No" << endl; }
template<class T> inline void POSS(T condition){ if(condition) cout << "POSSIBLE" << endl; else cout << "IMPOSSIBLE" << endl; }
template<class T> inline void Poss(T condition){ if(condition) cout << "Possible" << endl; else cout << "Impossible" << endl; }
template<class T> inline void First(T condition){ if(condition) cout << "First" << endl; else cout << "Second" << endl; }
template<class T = string, class U = char>int character_count(T text, U character){ int ans = 0; for(U i: text){ ans += (i == character); } return ans; }
long power(long base, long exponent, long module){ if(exponent % 2){ return power(base, exponent - 1, module) * base % module; }else if(exponent){ long root_ans = power(base, exponent / 2, module); return root_ans * root_ans % module; }else{ return 1; }}
struct position{ int y, x; }; position mv[4] = {{-1, 0}, {0, 1}, {1, 0}, {0, -1}}; // double euclidean(position first, position second){ return sqrt((second.x - first.x) * (second.x - first.x) + (second.y - first.y) * (second.y - first.y)); }
template<class T, class U> string to_string(pair<T, U> x){ return to_string(x.first) + "," + to_string(x.second); } string to_string(string x){ return x; }
template<class itr> void array_output(itr start, itr goal){ string ans; for(auto i = start; i != goal; i++) ans += to_string(*i) + " "; if(!ans.empty()) ans.pop_back(); cout << ans << endl; }
template<class itr> void cins(itr start, itr goal){ for(auto i = start; i != goal; i++){ cin >> (*i); } }
template<class T> T gcd(T a, T b){ if(a && b){ return gcd(min(a, b), max(a, b) % min(a, b)); }else{ return a; }} template<class T> T lcm(T a, T b){ return a / gcd(a, b) * b; }
struct combination{ vector<long> fact, inv; combination(int sz) : fact(sz + 1), inv(sz + 1){ fact[0] = 1; for(int i = 1; i <= sz; i++){ fact[i] = fact[i - 1] * i % mod; } inv[sz] = power(fact[sz], mod - 2, mod); for(int i = sz - 1; i >= 0; i--){ inv[i] = inv[i + 1] * (i + 1) % mod; } } long C(int p, int q) const{ if(q < 0 || p < q) return 0; return (fact[p] * inv[q] % mod * inv[p - q] % mod); } };

template< typename Monoid, typename OperatorMonoid = Monoid >
struct LazySegmentTree
{
    using F = function< Monoid(Monoid, Monoid) >;
    using G = function< Monoid(Monoid, OperatorMonoid) >;
    using H = function< OperatorMonoid(OperatorMonoid, OperatorMonoid) >;
    using P = function< OperatorMonoid(OperatorMonoid, int) >;
    
    int sz;
    vector< Monoid > data;
    vector< OperatorMonoid > lazy;
    const F f;
    const G g;
    const H h;
    const P p;
    const Monoid M1;
    const OperatorMonoid OM0;
    
    
    LazySegmentTree(int n, const F f, const G g, const H h, const P p,
                    const Monoid &M1, const OperatorMonoid OM0)
    : f(f), g(g), h(h), p(p), M1(M1), OM0(OM0)
    {
        sz = 1;
        while(sz < n) sz <<= 1;
        data.assign(2 * sz, M1);
        lazy.assign(2 * sz, OM0);
    }
    
    void set(int k, const Monoid &x)
    {
        data[k + sz] = x;
    }
    
    void build()
    {
        for(int k = sz - 1; k > 0; k--) {
            data[k] = f(data[2 * k + 0], data[2 * k + 1]);
        }
    }
    
    void propagate(int k, int len)
    {
        if(lazy[k] != OM0) {
            if(k < sz) {
                lazy[2 * k + 0] = h(lazy[2 * k + 0], lazy[k]);
                lazy[2 * k + 1] = h(lazy[2 * k + 1], lazy[k]);
            }
            data[k] = g(data[k], p(lazy[k], len));
            lazy[k] = OM0;
        }
    }
    
    Monoid update(int a, int b, const OperatorMonoid &x, int k, int l, int r)
    {
        propagate(k, r - l);
        if(r <= a || b <= l) {
            return data[k];
        } else if(a <= l && r <= b) {
            lazy[k] = h(lazy[k], x);
            propagate(k, r - l);
            return data[k];
        } else {
            return data[k] = f(update(a, b, x, 2 * k + 0, l, (l + r) >> 1),
                               update(a, b, x, 2 * k + 1, (l + r) >> 1, r));
        }
    }
    
    Monoid update(int a, int b, const OperatorMonoid &x)
    {
        return update(a, b, x, 1, 0, sz);
    }
    
    
    Monoid query(int a, int b, int k, int l, int r)
    {
        propagate(k, r - l);
        if(r <= a || b <= l) {
            return M1;
        } else if(a <= l && r <= b) {
            return data[k];
        } else {
            return f(query(a, b, 2 * k + 0, l, (l + r) >> 1),
                     query(a, b, 2 * k + 1, (l + r) >> 1, r));
        }
    }
    
    Monoid query(int a, int b)
    {
        return query(a, b, 1, 0, sz);
    }
    
    Monoid operator[](const int &k)
    {
        return query(k, k + 1);
    }
};

int main(){
    int N, Q;
    cin >> N;
    int b[N];
    cins(b, b + N);
    cin >> Q;
    vector<int> rs[N];
    for(int i = 0; i < Q; i++){
        int l, r;
        cin >> l >> r;
        rs[l - 1].push_back(r);
    }
    auto f = [](int a, int b) { return min(a, b); };
    auto g = [](int a, int b) { return a + b; };
    auto h = [](int a, int b) { return a + b; };
    auto p = [](int a, int b) { return a; };
    LazySegmentTree<int> dp(N + 1, f, g, h, p, 1e9, 0);
    dp.set(0, 0);
    dp.build();
    for(int i = 0; i < N; i++){
        rs[i].push_back(N + 1);
        sort(all(rs[i]));
        for(int j = int(rs[i].size()) - 2; j >= 0; j--){
            int x = rs[i][j];
            dp.update(x, rs[i][j + 1], !b[i]);
            int num = min(0, dp.query(0, x) + !b[i] - dp[x]);
            dp.update(x, x + 1, num);
        }
        dp.update(i + 1, rs[i][0], !b[i]);
        dp.update(0, i + 1, b[i]);
    }
    cout << dp.query(0, N + 1) << endl;
}


Compiler output:
a.cc:30:15: error: 'function' does not name a type; did you mean 'union'?
   30 |     using F = function< Monoid(Monoid, Monoid) >;
      |               ^~~~~~~~
      |               union
a.cc:31:15: error: 'function' does not name a type; did you mean 'union'?
   31 |     using G = function< Monoid(Monoid, OperatorMonoid) >;
      |               ^~~~~~~~
      |               union
a.cc:32:15: error: 'function' does not name a type; did you mean 'union'?
   32 |     using H = function< OperatorMonoid(OperatorMonoid, OperatorMonoid) >;
      |               ^~~~~~~~
      |               union
a.cc:33:15: error: 'function' does not name a type; did you mean 'union'?
   33 |     using P = function< OperatorMonoid(OperatorMonoid, int) >;
      |               ^~~~~~~~
      |               union
a.cc:38:11: error: 'F' does not name a type
   38 |     const F f;
      |           ^
a.cc:39:11: error: 'G' does not name a type
   39 |     const G g;
      |           ^
a.cc:40:11: error: 'H' does not name a type
   40 |     const H h;
      |           ^
a.cc:41:11: error: 'P' does not name a type
   41 |     const P p;
      |           ^
a.cc:46:34: error: 'F' does not name a type
   46 |     LazySegmentTree(int n, const F f, const G g, const H h, const P p,
      |                                  ^
a.cc:46:45: error: 'G' does not name a type
   46 |     LazySegmentTree(int n, const F f, const G g, const H h, const P p,
      |                                             ^
a.cc:46:56: error: 'H' does not name a type
   46 |     LazySegmentTree(int n, const F f, const G g, const H h, const P p,
      |                                                        ^
a.cc:46:67: error: 'P' does not name a type
   46 |     LazySegmentTree(int n, const F f, const G g, const H h, const P p,
      |                                                                   ^
a.cc: In constructor 'LazySegmentTree<Monoid, OperatorMonoid>::LazySegmentTree(int, int, int, int, int, const Monoid&, OperatorMonoid)':
a.cc:48:7: error: class 'LazySegmentTree<Monoid, OperatorMonoid>' does not have any field named 'f'
   48 |     : f(f), g(g), h(h), p(p), M1(M1), OM0(OM0)
      |       ^
a.cc:48:13: error: class 'LazySegmentTree<Monoid, OperatorMonoid>' does not have any field named 'g'
   48 |     : f(f), g(g), h(h), p(p), M1(M1), OM0(OM0)
      |             ^
a.cc:48:19: error: class 'LazySegmentTree<Monoid, OperatorMonoid>' does not have any field named 'h'
   48 |     : f(f), g(g), h(h), p(p), M1(M1), OM0(OM0)
      |                   ^
a.cc:48:25: error: class 'LazySegmentTree<Monoid, OperatorMonoid>' does not have any field named 'p'
   48 |     : f(f), g(g), h(h), p(p), M1(M1), OM0(OM0)
      |                         ^
a.cc: In function 'int main()':
a.cc:141:36: error: invalid user-defined conversion from 'main()::<lambda(int, int)>' to 'int' [-fpermissive]
  141 |     LazySegmentTree<int> dp(N + 1, f, g, h, p, 1e9, 0);
      |                                    ^
a.cc:137:14: note: candidate is: 'constexpr main()::<lambda(int, int)>::operator int (*)(int, int)() const' (near match)
  137 |     auto f = [](int a, int b) { return min(a, b); };
      |              ^
a.cc:137:14: note:   no known conversion from 'int (*)(int, int)' to 'int'
a.cc:46:36: note:   initializing argument 2 of 'LazySegmentTree<Monoid, OperatorMonoid>::LazySegmentTree(int, int, int, int, int, const Monoid&, OperatorMonoid) [with Monoid = int; OperatorMonoid = int]'
   46 |     LazySegmentTree(int n, const F f, const G g, const H h, const P p,
      |                            ~~~~~~~~^
a.cc:141:39: error: invalid user-defined conversion from 'main()::<lambda(int, int)>' to 'int' [-fpermissive]
  141 |     LazySegmentTree<int> dp(N + 1, f, g, h, p, 1e9, 0);
      |                                       ^
a.cc:138:14: note: candidate is: 'constexpr main()::<lambda(int, int)>::operator int (*)(int, int)() const' (near match)
  138 |     auto g = [](int a, int b) { return a + b; };
      |              ^
a.cc:138:14: note:   no known conversion from 'int (*)(int, int)' to 'int'
a.cc:46:47: note:   initializing argument 3 of 'LazySegmentTree<Monoid, OperatorMonoid>::LazySegmentTree(int, int, int, int, int, const Monoid&, OperatorMonoid) [with Monoid = int; OperatorMonoid = int]'
   46 |     LazySegmentTree(int n, const F f, const G g, const H h, const P p,
      |                                       ~~~~~~~~^
a.cc:141:42: error: invalid user-defined conversion from 'main()::<lambda(int, int)>' to 'int' [-fpermissive]
  141 |     LazySegmentTree<int> dp(N + 1, f, g, h, p, 1e9, 0);
      |                                          ^
a.cc:139:14: note: candidate is: 'constexpr main()::<lambda(int, int)>::operator int (*)(int, int)() const' (near match)
  139 |     auto h = [](int a, int b) { return a + b; };
      |              ^
a.cc:139:14: note:   no known conversion from 'int (*)(int, int)' to 'int'
a.cc:46:58: note:   initializing argument 4 of 'LazySegmentTree<Monoid, OperatorMonoid>::LazySegmentTree(int, int, int, int, int, const Monoid&, OperatorMonoid) [with Monoid = int; OperatorMonoid = int]'
   46 |     LazySegmentTree(int n, const F f, const G g, const H h, const P p,
      |                                                  ~~~~~~~~^
a.cc:141:45: error: invalid user-defined conversion from 'main()::<lambda(int, int)>' to 'int' [-fpermissive]
  141 |     LazySegmentTree<int> dp(N + 1, f, g, h, p, 1e9, 0);
      |                                             ^
a.cc:140:14: note: candidate is: 'constexpr main()::<lambda(int, int)>::operator int (*)(int, int)() const' (near match)
  140 |     auto p = [](int a, int b) { return a; };
      |              ^
a.cc:140:14: note:   no known conversion from 'int (*)(int, int)' to 'int'
a.cc:46:69: note:   initializing argument 5 of 'LazySegmentTree<Monoid, OperatorMonoid>::LazySegmentTree(int, int, int, int, int, const Monoid&, OperatorMonoid) [with Monoid = int; OperatorMonoid = int]'
   46 |     LazySegmentTree(int n, const F f, const G g, const H h, const P p,
      |                                                             ~~~~~~~~^
a.cc: In instantiation of 'void LazySegmentTree<Monoid, OperatorMonoid>::build() [with Monoid = int; OperatorMonoid = int]':
a.cc:143:13:   required from here
  143 |     dp.build();
      |     ~~~~~~~~^~
a.cc:64:24: error: 'f' was not declared in this scope
   64 |             data[k] = f(data[2 * k + 0], data[2 * k + 1]);
      |                       ~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc: In instantiation of 'Monoid LazySegmentTree<Monoid, OperatorMonoid>::update(int, int, const OperatorMonoid&, int, int, int) [with Monoid = int; OperatorMonoid = int]':
a.cc:97:22:   required from 'Monoid LazySegmentTree<Monoid, OperatorMonoid>::update(int, int, const OperatorMonoid&) [with Monoid = int; OperatorMonoid = int]'
   97 |         return update(a, b, x, 1, 0, sz);
      |                ~~~~~~^~~~~~~~~~~~~~~~~~~
a.cc:149:22:   required from here
  149 |             dp.update(x, rs[i][j + 1], !b[i]);
      |             ~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
a.cc:86:24: error: 'h' was not declared in this scope
   86 |             lazy[k] = h(lazy[k], x);
      |                       ~^~~~~~~~~~~~
a.cc:90:31: error: 'f' was not declared in this scope
   90 |             return data[k] = f(update(a, b, x, 2 * k + 0, l, (l + r) >> 1),
      |                              ~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
   91 |                                update(a, b, x, 2 * k + 1, (l + r) >> 1, r));
      |                                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc: In instantiation of 'Monoid LazySegmentTree<Monoid, OperatorMonoid>::query(int, int, int, int, int) [with Monoid = int; OperatorMonoid = int]':
a.cc:116:21:   required from 'Monoid LazySegmentTree<Monoid, OperatorMonoid>::query(int, int) [with Monoid = int; OperatorMonoid = int]'
  116 |         return query(a, b, 1, 0, sz);
      |                ~~~~~^~~~~~~~~~~~~~~~
a.cc:150:38:   required from here
  150 |             int num = min(0, dp.query(0, x) + !b[i] - dp[x]);
      |                              ~~~~~~~~^~~~~~
a.cc:109:21: error: 'f' was not declared in this scope
  109 |             return f(query(a, b, 2 * k + 0, l, (l + r) >> 1),
      |                    ~^~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
  110 |                      query(a, b, 2 * k + 1, (l + r) >> 1, r));
      |                      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc: In instantiation of 'void LazySegmentTree<Monoid, OperatorMonoid>::propagate(int, int) [with Monoid = int; OperatorMonoid = int]':
a.cc:82:9:   required from 'Monoid LazySegmentTree<Monoid, OperatorMonoid>::update(int, int, const OperatorMonoid&, int, int, int) [with Monoid = int; OperatorMonoid = int]'
   82 |         propagate(k, r - l);
      |         ^~~~~~~~~
a.cc:97:22:   required from 'Monoid LazySegmentTree<Monoid, OperatorMonoid>::update(int, int, const OperatorMonoid&) [with Monoid = int; OperatorMonoid = int]'
   97 |         return update(a, b, x, 1, 0, sz);
      |                ~~~~~~^~~~~~~~~~~~~~~~~~~
a.cc:149:22:   required from here
  149 |             dp.update(x, rs[i][j + 1], !b[i]);
      |             ~~~~~~~~~^~~~~~~~~~~~~~~~~~~~~~~~
a.cc:72:36: error: 'h' was not declared in this scope
   72 |                 lazy[2 * k + 0] = h(lazy[2 * k + 0], lazy[k]);
      |                                   ~^~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:73:36: error: 'h' was not declared in this scope, and no declarations were found by argument-dependent lookup at the point of instantiation
   73 |                 lazy[2 * k + 1] = h(lazy[2 * k + 1], lazy[k]);
      |                                   ~^~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:72:36: note: 'h' declared here, later in the translation unit
   72 |                 lazy[2 * k + 0] = h(lazy[2 * k + 0], lazy[k]);
      |                                   ~^~~~~~~~~~~~~~~~~~~~~~~~~~
a.cc:75:35: error: 'p' was not declared in this scope
   75 |             data[k] = g(data[k], p(lazy[k], len));
      |                                  ~^~~~~~~~~~~~~~
a.cc:75:24: error: 'g' was not declared in this scope
   75 |             data