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

Solution in C++14 (GCC 5.4.1) (judge verdict: Accepted)

#include <bits/stdc++.h>
#include <ext/pb_ds/assoc_container.hpp>
#include <ext/pb_ds/tree_policy.hpp>
 
using namespace std;
using namespace __gnu_pbds;
 
#define fi first
#define se second
#define mp make_pair
#define pb push_back
#define fbo find_by_order
#define ook order_of_key
 
typedef long long ll;
typedef pair<int,int> ii;
typedef vector<int> vi;
typedef long double ld; 
typedef tree<int, null_type, less<int>, rb_tree_tag, tree_order_statistics_node_update> pbds;
typedef set<int>::iterator sit;
typedef map<int,int>::iterator mit;
typedef vector<int>::iterator vit;

int suf[200011];
int a[200011];
vector<pair<int,int> > adj[200011];
int dp[200011];
vi st[800011];
vector<ii> val[800011];

int sum(int l, int r)
{
	if(l==0) return dp[r];
	else return dp[r]-dp[l-1];
}

vector<ii> interval;

void modify(int id, int p, int value) {  // set value at position p
	int n=st[id].size()/2;
  for (st[id][p += n] = value; p > 1; p >>= 1) st[id][p>>1] = min(st[id][p], st[id][p^1]);
}

int ask(int id, int l, int r) {  // sum on interval [l, r)
	if(l>=r) return int(1e9);
  int res = int(1e9);
  int n=st[id].size()/2;
  for (l += n, r += n; l < r; l >>= 1, r >>= 1) {
    if (l&1) res = min(res, st[id][l++]);
    if (r&1) res = min(res, st[id][--r]);
  }
  return res;
}

int build(int id, int l, int r)
{
	if(r-l<2)
	{
		////cerr<<"BUILD : "<<id<<' '<<sum<<'\n';
		val[id].pb(mp(interval[l].se,l));
		st[id].assign(2,int(1e9));
		return 1;
	}
	int mid=(l+r)>>1;
	int sum = build(id*2,l,mid) + build(id*2+1,mid,r);
	for(int j=0;j<val[id*2].size();j++) val[id].pb(val[id*2][j]);
	for(int j=0;j<val[id*2+1].size();j++) val[id].pb(val[id*2+1][j]);
	sort(val[id].begin(),val[id].end());
	st[id].assign(2*sum,int(1e9));
	assert(val[id].size()==sum);
	////cerr<<"BUILD : "<<id<<' '<<sum<<'\n';
	return sum;
}

void update(int id, int l, int r, int pos, int v)
{
	if(pos>=r||pos<l) return ;
	int R = interval[pos].se;
	int poss=lower_bound(val[id].begin(),val[id].end(),mp(R,pos))-val[id].begin();
	////cerr<<id<<' '<<l<<' '<<r<<' '<<val[id].size()<<'\n';
	assert(poss>=0&&poss<int(val[id].size()));
	modify(id,poss,v);
	if(r-l<2) return ;
	int mid=(l+r)>>1;
	update(id*2,l,mid,pos,v); update(id*2+1,mid,r,pos,v);
}

int query(int id, int l, int r, int ql, int qr, int v)
{
	if(l>=qr||ql>=r) return int(1e9);
	if(ql<=l&&r<=qr)
	{
		int poss=lower_bound(val[id].begin(),val[id].end(),mp(v+1,-1))-val[id].begin();
		return ask(id,poss,int(st[id].size())/2);
	}
	int mid=(l+r)>>1;
	return min(query(id*2,l,mid,ql,qr,v),query(id*2+1,mid,r,ql,qr,v));
}

int main()
{
	ios_base::sync_with_stdio(0); cin.tie(0);
	int n; cin>>n;
	for(int i=0;i<n;i++)
	{
		cin>>a[i];
	}
	dp[0]=(a[0]==0);
	for(int i=1;i<n;i++)
	{
		dp[i]=dp[i-1]+(a[i]==0);
	}
	int q; cin>>q;
	for(int i=0;i<q;i++)
	{
		int l,r; cin>>l>>r;
		l--; r--;
		interval.pb(mp(l,r));
	}
	suf[n]=0;
	sort(interval.begin(),interval.end());
	for(int i=q-1;i>=0;i--)
	{
		adj[interval[i].fi].pb(mp(interval[i].se,i));
	}
	for(int i=0;i<n;i++)
	{
		sort(adj[i].rbegin(),adj[i].rend());
	}
	build(1,0,q);
	for(int i=n-1;i>=0;i--)
	{
		suf[i]=suf[i+1]+(a[i]==1);
		for(int j=0;j<adj[i].size();j++)
		{
			int r=adj[i][j].fi;
			int add=(i-1>=0?-dp[i-1]:0);
			int z=lower_bound(interval.begin(),interval.end(),mp(r+1,0))-interval.begin();
			z--;
			//test from interval
			int cur = add + query(1,0,q,adj[i][j].se+1,z+1,r); //must be more than r
			cur=min(cur,suf[r+1]+sum(i,r));
			suf[i]=min(suf[i],cur);
			//cerr<<i<<' '<<r<<' '<<cur<<"  "<<adj[i][j].se<<'\n';
			//cerr<<"UPDATE : "<<adj[i][j].se<<' '<<cur+add<<'\n';
			update(1,0,q,adj[i][j].se,cur-add);
		}
		//cerr<<"SUF "<<i<<" : "<<suf[i]<<'\n';
	}
	cout<<suf[0]<<'\n';
}