Language: C++

#include <iostream>
#include <cstdio>
#include <cstring>
#include <queue>
#include <math.h>
#include <algorithm>
#include <map>
#include <set>
#include <vector>
 
#include <bits/stdc++.h>
// #include <ext/pb_ds/assoc_container.hpp>
// using namespace __gnu_pbds;
using namespace std;
// typedef pair<int,int> pii;
// tree <pii,null_type,greater<pii>,rb_tree_tag,tree_order_statistics_node_update> tr;
// cin.tie(0);
// std::ios::sync_with_stdio(false);
typedef long long LL;
const int N = 200005;
const int M = 205;
const int INF = 0x3f3f3f3f;
const double Pi = acos(-1);
const int MOD = 1e9+7;
const double eps = 1e-8;
int n,m;
 
class SegmentTree {
  public:
    SegmentTree(int size, int value):
        m_size(size),
        m_data(size * 2, value) {}
 
    void update(int position, int value) {
        for (position += m_size; position; position /= 2)
            m_data[position] = min(m_data[position], value);
    }
 
    int query(int from, int to) {
        int answer = numeric_limits<int>::max();
        for (from += m_size, to += m_size; from < to; from /= 2, to /= 2) {
            if (from & 1)
                answer = min(answer, m_data[from++]);
            if (to & 1)
                answer = min(answer, m_data[--to]);
        }
        return answer;
    }
 
  private:
    int m_size;
    vector<int> m_data;
};
 
int main() 
    cin.tie(0);
    ios::sync_with_stdio(false);
    int N; 
    cin >> N;
    vector<int> B(N + 1, 0);
    for (int i = 1; i <= N; ++i)
        cin >> B[i];
 
    vector<int> partial(N + 1, 0);
    for (int i = 1; i <= N; ++i)
        partial[i] = partial[i - 1] + B[i];
 
    int Q; cin >> Q;
    vector< vector<int> > rights(N + 1);
    for (int i = 0; i < Q; ++i) {
        int x, y; cin >> x >> y;
        rights[x].emplace_back(y);
    }
 
    SegmentTree S(N + 1, N);
    vector<int> dp(N + 1, N);
    dp[0] = 0;
    for (int i = 1; i <= N; ++i) {
        dp[i] = min(dp[i], dp[i - 1] + B[i]);
        S.update(i - 1, dp[i - 1] - (i - 1) + partial[i - 1]);
        // dp[i] = min(dp[j] + (i - j) - (partial[i] - partial[j]))
        //       = i - partial[i] + min(dp[j] - j + partial[j])
        for (auto &right : rights[i]) {
            int best = right - partial[right] + S.query(i - 1, right);
            dp[right] = min(dp[right], best);
            S.update(right, best - right + partial[right]);
        }
    }
 
    cout << dp[N] << "\n";
}
 
/*
 
 
3
1 0 1
2
1 1
2 3
 
5
0 1 0 1 0
1
1 5
 
9
0 1 0 1 1 1 0 1 0
3
1 4
5 8
6 7
 
15
1 1 0 0 0 0 0 0 1 0 1 1 1 0 0
9
4 10
13 14
1 7
4 14
9 11
2 6
7 8
3 12
7 13
 
10
0 0 0 1 0 0 1 1 1 0
7
1 4
2 5
1 3
6 7
9 9
1 5
7 9
 
*/

Compiler output:
a.cc:56:5: error: expected initializer before 'cin'
   56 |     cin.tie(0);
      |     ^~~
a.cc:57:5: error: specializing member 'std::basic_ios<char>::sync_with_stdio' requires 'template<>' syntax
   57 |     ios::sync_with_stdio(false);
      |     ^~~
a.cc:58:9: error: conflicting declaration 'int N'
   58 |     int N;
      |         ^
a.cc:20:11: note: previous declaration as 'const int N'
   20 | const int N = 200005;
      |           ^
a.cc:59:5: error: 'cin' does not name a type
   59 |     cin >> N;
      |     ^~~
a.cc:61:5: error: expected unqualified-id before 'for'
   61 |     for (int i = 1; i <= N; ++i)
      |     ^~~
a.cc:61:21: error: 'i' does not name a type
   61 |     for (int i = 1; i <= N; ++i)
      |                     ^
a.cc:61:29: error: expected unqualified-id before '++' token
   61 |     for (int i = 1; i <= N; ++i)
      |                             ^~
a.cc:65:5: error: expected unqualified-id before 'for'
   65 |     for (int i = 1; i <= N; ++i)
      |     ^~~
a.cc:65:21: error: 'i' does not name a type
   65 |     for (int i = 1; i <= N; ++i)
      |                     ^
a.cc:65:29: error: expected unqualified-id before '++' token
   65 |     for (int i = 1; i <= N; ++i)
      |                             ^~
a.cc:68:12: error: 'cin' does not name a type
   68 |     int Q; cin >> Q;
      |            ^~~
a.cc:70:5: error: expected unqualified-id before 'for'
   70 |     for (int i = 0; i < Q; ++i) {
      |     ^~~
a.cc:70:21: error: 'i' does not name a type
   70 |     for (int i = 0; i < Q; ++i) {
      |                     ^
a.cc:70:28: error: expected unqualified-id before '++' token
   70 |     for (int i = 0; i < Q; ++i) {
      |                            ^~
a.cc:77:5: error: 'dp' does not name a type
   77 |     dp[0] = 0;
      |     ^~
a.cc:78:5: error: expected unqualified-id before 'for'
   78 |     for (int i = 1; i <= N; ++i) {
      |     ^~~
a.cc:78:21: error: 'i' does not name a type
   78 |     for (int i = 1; i <= N; ++i) {
      |                     ^
a.cc:78:29: error: expected unqualified-id before '++' token
   78 |     for (int i = 1; i <= N; ++i) {
      |                             ^~
a.cc:90:5: error: 'cout' does not name a type
   90 |     cout << dp[N] << "\n";
      |     ^~~~
a.cc:91:1: error: expected declaration before '}' token
   91 | }
      | ^