1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
  | // 더 많은 정보는 42jerrykim.github.io 에서 확인하세요.
#include <bits/stdc++.h>
using namespace std;
struct Fenwick {
    int n;
    vector<int> bit;
    Fenwick(int n = 0) { init(n); }
    void init(int n_) { n = n_; bit.assign(n + 1, 0); }
    void add(int idx, int delta) {
        for (; idx <= n; idx += idx & -idx) bit[idx] += delta;
    }
    long long sum(int idx) const {
        long long s = 0;
        for (; idx > 0; idx -= idx & -idx) s += bit[idx];
        return s;
    }
};
struct Event {
    int x, b, t, coeff; // coeff = +1 for r, -1 for l-1
    bool operator<(const Event& other) const { return x < other.x; }
};
int main() {
    ios::sync_with_stdio(false);
    cin.tie(nullptr);
    int T;
    if (!(cin >> T)) return 0;
    while (T--) {
        int n, m;
        cin >> n >> m;
        vector<pair<int,int>> points(n);
        vector<int> y_vals;
        y_vals.reserve(n);
        for (int i = 0; i < n; ++i) {
            int x, y; cin >> x >> y;
            points[i] = {x, y};
            y_vals.push_back(y);
        }
        vector<Event> events;
        events.reserve(2 * m);
        for (int i = 0; i < m; ++i) {
            int l, r, b, t; 
            cin >> l >> r >> b >> t;
            events.push_back({r, b, t, +1});
            events.push_back({l - 1, b, t, -1});
        }
        sort(points.begin(), points.end()); // by x
        sort(events.begin(), events.end()); // by x
        sort(y_vals.begin(), y_vals.end());
        y_vals.erase(unique(y_vals.begin(), y_vals.end()), y_vals.end());
        auto y_index_upper = [&](int y) -> int {
            // number of unique point-ys <= y
            return int(upper_bound(y_vals.begin(), y_vals.end(), y) - y_vals.begin());
        };
        Fenwick fw((int)y_vals.size());
        long long answer = 0;
        int p = 0; // pointer over points
        for (const auto& ev : events) {
            while (p < n && points[p].first <= ev.x) {
                int y = points[p].second;
                int idx = y_index_upper(y); // 1..N
                if (idx > 0) fw.add(idx, 1);
                ++p;
            }
            int up_t = y_index_upper(ev.t);
            int up_bm1 = y_index_upper(ev.b - 1);
            long long cnt = fw.sum(up_t) - fw.sum(up_bm1);
            answer += (long long)ev.coeff * cnt;
        }
        cout << answer << '\n';
    }
    return 0;
}
  |