Problem:


Score : 1000 points

Problem Statement

You are given a sequence a = \{a_1, ..., a_N\} with all zeros, and a sequence b = \{b_1, ..., b_N\} consisting of 0 and 1. The length of both is N.

You can perform Q kinds of operations. The i-th operation is as follows:

Replace each of a_{l_i}, a_{l_i + 1}, ..., a_{r_i} with 1.

Minimize the hamming distance between a and b, that is, the number of i such that a_i \neq b_i, by performing some of the Q operations.

Constraints

1 \leq N \leq 200,000

b consists of 0 and 1.

1 \leq Q \leq 200,000

1 \leq l_i \leq r_i \leq N

If i \neq j, either l_i \neq l_j or r_i \neq r_j.

Input

Input is given from Standard Input in the following format:

N
b_1 b_2 ... b_N
Q
l_1 r_1
l_2 r_2
:
l_Q r_Q

Output

Print the minimum possible hamming distance.

Sample Input 1

3
1 0 1
1
1 3

Sample Output 1

1

If you choose to perform the operation, a will become \{1, 1, 1\}, for a hamming distance of 1.

Sample Input 2

3
1 0 1
2
1 1
3 3

Sample Output 2

0

If both operations are performed, a will become \{1, 0, 1\}, for a hamming distance of 0.

Sample Input 3

3
1 0 1
2
1 1
2 3

Sample Output 3

1

Sample Input 4

5
0 1 0 1 0
1
1 5

Sample Output 4

2

It may be optimal to perform no operation.

Sample Input 5

9
0 1 0 1 1 1 0 1 0
3
1 4
5 8
6 7

Sample Output 5

3

Sample Input 6

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

Sample Output 6

5

Sample Input 7

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

Sample Output 7

1



Sample input:
3
1 0 1
1
1 3


Sample output:
1

Submission status: Accepted

Solution code:
import sys
read = sys.stdin.buffer.read
readline = sys.stdin.buffer.readline
readlines = sys.stdin.buffer.readlines

N = int(readline())
B = list(map(int,readline().split()))
Q = int(readline())
m = map(int,read().split())
LR = sorted(zip(m,m))

class MaxSegTree():
    def __init__(self,N):
        self.Nelem = N
        self.size = 1<<(N.bit_length()) # 葉の要素数
        self.data = [0] * (2*self.size)
        
    def build(self,raw_data):
        # raw_data は 0-indexed
        for i,x in enumerate(raw_data):
            self.data[self.size+i] = x
        for i in range(self.size-1,0,-1):
            x = self.data[i+i]; y = self.data[i+i+1]
            self.data[i] = x if x>y else y
    
    def update(self,i,x):
        i += self.size
        self.data[i] = x
        i >>= 1
        while i:
            x = self.data[i+i]; y = self.data[i+i+1]
            self.data[i] = x if x>y else y
            i >>= 1
    
    def get_data(self,i):
        return self.data[i+self.size]

    def get_max(self,L,R):
        # [L,R] に対する値を返す
        L += self.size
        R += self.size + 1
        # [L,R) に変更
        x = 0
        while L < R:
            if L&1:
                y = self.data[L]
                if x < y: x = y
                L += 1
                
            if R&1:
                R -= 1
                y = self.data[R]
                if x < y: x = y
            L >>= 1; R >>= 1
        return x

"""
・ある場所まで確定して、そこから先を全て1で埋めた場合
・ある場所まで確定して、そこから先を全て0で埋めた場合
の加算スコア
"""
add0 = [0] * (N+1)
add1 = [0] * (N+1)
x = sum(B)
add1[0] = x
add0[0] = N-x
for i,x in enumerate(B,1):
    if x == 0:
        add0[i]=add0[i-1]-1; add1[i] = add1[i-1]
    else:
        add1[i]=add1[i-1]-1; add0[i] = add0[i-1]

"""
ある場所を右端としてとった時点での
・残りをすべて0で埋めたときのスコア
・残りをすべて1で埋めたときのスコア
dp0, dp1 をseg木で管理
"""

dp0 = MaxSegTree(N+1)
dp0.build([add0[0]] + [0] * N)
dp1 = MaxSegTree(N+1)
dp1.build([add1[0]] + [0] * N)

for L,R in LR:
    # dp1[R] を計算したい。[L,inf) が1で埋まったときのスコア
    x = dp1.get_data(R)
    y = dp0.get_max(0,L-1) + add1[L-1] - add0[L-1] # 0埋め部分を1埋めに修正してdp1[R]に遷移
    z = dp1.get_max(L,R-1) # そのままdp1[R]に遷移
    if y < z: y = z
    if x < y:
        dp1.update(R,y)
        dp0.update(R,y - add1[R] + add0[R])

# 一致を数えていたので、距離に修正
answer = N - dp0.data[1]
print(answer)

Submission status: Runtime Error

Solution code:
import sys
sys.setrecursionlimit(3 * 10**5)

n = int(input())
b = list(map(int, input().split()))
op = [[] for i in range(n)]
q = int(input())
for i in range(q):
    l, r = map(int, input().split())
    op[l - 1].append(r)
nula = [0 for i in range(n + 1)]
for i in range(n):
    nula[i + 1] = nula[i] + (b[i] == 0)

memo = [-1 for i in range(n + 1)]
def dp(i):
    if memo[i] != -1:
        return memo[i]
    if i == n:
        memo[i] = 0
        return 0
    best = dp(i + 1) + (b[i] == 1)
    ii = i
    while True:
        hi = 0
        for r in op[ii]:
            pot = dp(r) + nula[r] - nula[i]
            if pot < best:
                best = pot
                hi = r
        if hi == 0:
            break
        ii += 1
    memo[i] = best
    return best

print(dp(0))