#include using namespace std; template inline bool chmax(T& a, T b) { if (a < b) { a = b; return 1; } return 0; } template inline bool chmin(T& a, T b) { if (a > b) { a = b; return 1; } return 0; } using pint = pair; using pll = pair; // Segment Tree template struct SegTree { using Func = function; // core member int SIZE; Func F; Monoid IDENTITY; // data int offset; vector dat; // constructor SegTree() {} SegTree(int n, const Func &f, const Monoid &identity) : SIZE(n), F(f), IDENTITY(identity) { offset = 1; while (offset < n) offset *= 2; dat.assign(offset * 2, IDENTITY); } void init(int n, const Func &f, const Monoid &identity) { SIZE = n; F = f; IDENTITY = identity; offset = 1; while (offset < n) offset *= 2; dat.assign(offset * 2, IDENTITY); } int size() const { return SIZE; } // set, a is 0-indexed // // build(): O(N) void set(int a, const Monoid &v) { dat[a + offset] = v; } void build() { for (int k = offset - 1; k > 0; --k) dat[k] = F(dat[k*2], dat[k*2+1]); } void build(const vector &vec) { for (int a = 0; a < vec.size() && a + offset < dat.size(); ++a) set(a, vec[a]); build(); } // update a, a is 0-indexed, O(log N) void update(int a, const Monoid &v) { int k = a + offset; dat[k] = v; while (k >>= 1) dat[k] = F(dat[k*2], dat[k*2+1]); } // get [a, b), a and b are 0-indexed, O(log N) Monoid get(int a, int b) { Monoid vleft = IDENTITY, vright = IDENTITY; for (int left = a + offset, right = b + offset; left < right; left >>= 1, right >>= 1) { if (left & 1) vleft = F(vleft, dat[left++]); if (right & 1) vright = F(dat[--right], vright); } return F(vleft, vright); } Monoid get_all() { return dat[1]; } Monoid operator [] (int a) const { return dat[a + offset]; } // get max r that f(get(l, r)) = True (0-indexed), O(log N) // f(IDENTITY) need to be True int max_right(const function f, int l = 0) { if (l == SIZE) return SIZE; l += offset; Monoid sum = IDENTITY; do { while (l % 2 == 0) l >>= 1; if (!f(F(sum, dat[l]))) { while (l < offset) { l = l * 2; if (f(F(sum, dat[l]))) { sum = F(sum, dat[l]); ++l; } } return l - offset; } sum = F(sum, dat[l]); ++l; } while ((l & -l) != l); // stop if l = 2^e return SIZE; } // get min l that f(get(l, r)) = True (0-indexed), O(log N) // f(IDENTITY) need to be True int min_left(const function f, int r = -1) { if (r == 0) return 0; if (r == -1) r = SIZE; r += offset; Monoid sum = IDENTITY; do { --r; while (r > 1 && (r % 2)) r >>= 1; if (!f(F(dat[r], sum))) { while (r < offset) { r = r * 2 + 1; if (f(F(dat[r], sum))) { sum = F(dat[r], sum); --r; } } return r + 1 - offset; } sum = F(dat[r], sum); } while ((r & -r) != r); return 0; } // debug friend ostream& operator << (ostream &s, const SegTree &seg) { for (int i = 0; i < seg.size(); ++i) { s << seg[i]; if (i != seg.size()-1) s << " "; } return s; } }; #define REP(i, n) for (long long i = 0; i < (long long)(n); ++i) #define REP2(i, a, b) for (long long i = a; i < (long long)(b); ++i) #define COUT(x) cout << #x << " = " << (x) << " (L" << __LINE__ << ")" << endl template ostream& operator << (ostream &s, pair P) { return s << '<' << P.first << ", " << P.second << '>'; } template ostream& operator << (ostream &s, vector P) { for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; } template ostream& operator << (ostream &s, deque P) { for (int i = 0; i < P.size(); ++i) { if (i > 0) { s << " "; } s << P[i]; } return s; } template ostream& operator << (ostream &s, vector > P) { for (int i = 0; i < P.size(); ++i) { s << endl << P[i]; } return s << endl; } template ostream& operator << (ostream &s, set P) { for(auto it : P) { s << "<" << it << "> "; } return s; } template ostream& operator << (ostream &s, multiset P) { for(auto it : P) { s << "<" << it << "> "; } return s; } template ostream& operator << (ostream &s, map P) { for(auto it : P) { s << "<" << it.first << "->" << it.second << "> "; } return s; } const long long INF = 1LL<<60; int main() { int N, M; cin >> N >> M; vector items(N); // {A[i], B[i]} vector gen, mma; for (int i = 0; i < N; ++i) cin >> items[i].first >> items[i].second; for (int i = 0; i < M; ++i) { long long T, C; cin >> T >> C; if (T == 0) gen.push_back(C); else mma.push_back(C); } sort(items.begin(), items.end()); sort(gen.begin(), gen.end()); sort(mma.begin(), mma.end()); // 特定区間の B の最大値・最小値を求めるためのセグ木 using Node = pair; // {最大値 or 最小値, index} SegTree seg_max(N, [&](Node a, Node b){ return max(a, b); }, Node(-INF, -1)); SegTree seg_min(N, [&](Node a, Node b){ return min(a, b); }, Node(INF, -1)); for (int i = 0; i < N; ++i) { seg_max.set(i, pll(items[i].second, i)); seg_min.set(i, pll(items[i].second, i)); } seg_max.build(), seg_min.build(); // gen -> mma のそれぞれ所持金が小さい順に見ていく int match = 0; for (auto v : gen) { // まだ購入されていない商品のうち、A[i] <= v である範囲内で B[i] が最大の i を選ぶ int right = upper_bound(items.begin(), items.end(), pll(v, INF)) - items.begin(); auto [max_cost, index] = seg_max.get(0, right); // 購入済みにする if (index == -1) continue; ++match; seg_max.update(index, pll(-INF, -1)); seg_min.update(index, pll(INF, -1)); } //COUT(match); for (auto v : mma) { // まだ購入されていない商品のうち、B[i] <= v であるような i を選ぶ (ここでは最小をとる) auto [min_cost, index] = seg_min.get(0, N); // 購入済みにする if (index == -1 || v < min_cost) continue; ++match; seg_max.update(index, pll(-INF, -1)); seg_min.update(index, pll(INF, -1)); } //COUT(match); cout << N - match << endl; }