import std; void main () { int N, M; readln.read(N, M); auto A = readln.split.to!(int[]); auto B = readln.split.to!(int[]); int[] mma, non_mma; foreach (i; 0..M) { int T, C; readln.read(T, C); if (T == 0) non_mma ~= C; if (T == 1) mma ~= C; } solve(N, M, A, B, mma, non_mma); } void solve (int N, int M, int[] A, int[] B, int[] mma, int[] non_mma) { auto items = new Tuple!(int, int, int)[](N); foreach (i; 0..N) { items[i] = tuple(i, A[i], B[i]); } items.sort!"a[1] < b[1]"; non_mma.sort; auto used = new bool[](N); { BinaryHeap!(Tuple!(int, int)[], "a[1] < b[1]") PQ = []; // rangeだけ使う auto r = items; foreach (v; non_mma) { while (!r.empty && r.front[1] <= v) { PQ.insert(tuple(r.front[0], r.front[2])); r.popFront; } if (PQ.empty) continue; used[PQ.front[0]] = true; PQ.removeFront; } } mma.sort; BinaryHeap!(Tuple!(int, int)[], "b[1] < a[1]") PQ = []; foreach (i; 0..N) { if (used[i]) continue; PQ.insert(tuple(items[i][0], items[i][2])); } foreach (v; mma) { if (PQ.empty) break; if (v < PQ.front[1]) continue; used[PQ.front[0]] = true; PQ.removeFront; } int ans = 0; foreach (i; 0..N) if (!used[i]) ans++; writeln(ans); } void read (T...) (string S, ref T args) { import std.conv : to; import std.array : split; auto buf = S.split; foreach (i, ref arg; args) { arg = buf[i].to!(typeof(arg)); } }