-
Notifications
You must be signed in to change notification settings - Fork 0
/
11012BOJ_egg.cpp
137 lines (123 loc) · 3.09 KB
/
11012BOJ_egg.cpp
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
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
#include <iostream>
#include <vector>
#include <bits/stdc++.h>
#define fastio ios_base::sync_with_stdio(0); cin.tie(0); cout.tie(0);
#define all(v) v.begin(), v.end()
#define INF 100005
using namespace std;
using ll = long long;
struct Node
{
int lo, hi;
Node *l, *r;
ll cnt;
Node(int lo, int hi) : lo(lo), hi(hi){
l = r = nullptr;
cnt = 0;
}
~Node(){
if (l) delete l;
if (r) delete r;
}
};
struct PST
{
vector<Node*> roots;
void init(){
roots.clear();
roots.resize(INF);
roots[0] = init(0, INF);
}
void update(int x){
roots[x] = roots[x-1];
}
void update(int x, int y){
if (!roots[x]) roots[x] = new Node(0, INF);
update(roots[x-1], roots[x], y);
}
ll query(int u, int d, int l, int r){
return query(roots[u-1], roots[d], l, r);
}
void print(int i){
print(roots[i]);
cout << '\n';
}
private:
Node* init(int lo, int hi){
Node *x = new Node(lo, hi);
if (lo == hi) return x;
int mid = lo + hi >> 1;
x->l = init(lo, mid);
x->r = init(mid+1, hi);
return x;
}
void update(Node *p, Node *x, int y){
if (x->lo == x->hi) {
if (!x->cnt) x->cnt = p->cnt+1;
else x->cnt ++;
return;
}
int mid = x->lo + x->hi >> 1;
if (y<=mid) {
if (!x->l || x->l == p->l) x->l = new Node(x->lo, mid);
if (!x->r) x->r = p->r;
update(p->l, x->l, y);
} else {
if (!x->l) x->l = p->l;
if (!x->r || x->r == p->r) x->r = new Node(mid+1, x->hi);
update(p->r, x->r, y);
}
x->cnt = x->l->cnt + x->r->cnt;
}
ll query(Node *up, Node *dn, int l, int r){
if (dn->lo == l && dn->hi == r) return dn->cnt - up->cnt;
int mid = dn->lo + dn->hi >> 1;
if (r <= mid) return query(up->l, dn->l, l, r);
else if (l > mid) return query(up->r, dn->r, l, r);
else return query(up->l, dn->l, l, mid) + query(up->r, dn->r, mid+1, r);
}
void print(Node *x){
if (!x->l && !x->r) {
cout << x->cnt << ' ';
return;
}
print(x->l);
print(x->r);
}
} pst;
struct p{
int x, y;
bool operator<(const p &t) const{
if (x != t.x) return x<t.x;
return y<t.y;
}
};
int main()
{
fastio
int T; cin >> T;
while (T--){
int n,m; cin >> n >> m;
pst.init();
vector<p> pt;
for (int i=0; i<n; ++i){
int x,y; cin >> x >> y;
pt.push_back(p{++x,++y});
}
sort(all(pt));
for (int i=1, j=0; i<INF; ++i){
bool c = 0;
while (j<n && pt[j].x == i){
pst.update(i, pt[j].y);
j++; c = 1;
}
if (!c) pst.update(i);
}
ll ret = 0;
while (m--){
int u,d,l,r; cin >> u >> d >> l >> r;
ret += pst.query(++u, ++d, ++l, ++r);
}
cout << ret << '\n';
}
} // namespace