-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathD.cpp
192 lines (178 loc) · 4.28 KB
/
D.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
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
#include<bits/stdc++.h>
using namespace std;
#define fo(i,n) for(i=0;i<n;i++)
#define fo2(i,start,end) for(i=start;i<=end;i++)
#define rfo(i,n) for(i=n-1;i>=0;i--)
#define ll long long int
#define deb(x) cout<<#x<<"="<<x<<endl
#define deb2(x,y) cout<<#x<<"="<<x<<","<<#y<<"="<<y<<endl
#define pb push_back
#define mp make_pair
#define F first
#define S second
#define all(x) x.begin(), x.end()
#define sortall(x) sort(all(x))
#define tr(it,a) for(auto it=a.begin(); it!=a.end();it++)
#define nl '\n'
#define PI 3.1415926535897932384626
#define mod 1000000007
typedef pair<int, int> pii;
typedef pair<ll, ll> pl;
typedef vector<int> vi;
typedef vector<ll> vl;
typedef vector<pii> vpii;
typedef vector<pl> vpl;
const int N=1e6+10;
void solve(){
int i,j;
int n,m,k;
cin>>n>>m>>k;
int x[n],y[m];
set<int> s1,s2,m1,m2;
fo(i,n){
cin>>x[i];
s1.insert(x[i]);
}
fo(i,m){
cin>>y[i];
s2.insert(y[i]);
}
pii p[k];
vi v1,v2;
map<int,vi> mp1,mp2;
fo(i,k){
cin>>p[i].F>>p[i].S;
// deb2(p[i].F,p[i].S);
if(s1.find(p[i].F)!=s1.end() && s2.find(p[i].S)!=s2.end()){
continue;
}
if(s1.find(p[i].F)!=s1.end()){
v1.pb(p[i].S);
mp1[p[i].F].pb(p[i].S);
m1.insert(p[i].F);
}
else{
v2.pb(p[i].F);
mp2[p[i].S].pb(p[i].F);
m2.insert(p[i].S);
}
}
// v1-> ||
sort(all(v1));
sort(all(v2));
ll count=0,curr=-1,temp=0,ans=0,minus=0;
fo(i,v1.size()){
if(v1[i]>curr){
temp+=(count*(count-1))/2;
count=1;
if(s2.upper_bound(v1[i])!=s2.end()){
curr=*s2.upper_bound(v1[i]);
}
else{
curr=1e6+10;
}
}
else if(v1[i]<curr){
count++;
}
}
temp+=(count*(count-1))/2;
ans+=temp;
// deb(ans);
count=0;curr=-1;temp=0;
fo(i,v2.size()){
// deb(v2[i]);
if(v2[i]>curr){
temp+=(count*(count-1))/2;
count=1;
if(s1.upper_bound(v2[i])!=s1.end()){
curr=*s1.upper_bound(v2[i]);
}
else{
curr=1e6+10;
}
}
else if(v2[i]<curr){
count++;
}
}
temp+=(count*(count-1))/2;
ans+=temp;
// deb(temp);
for(auto it : m1){
v1.clear();
for(auto itr : mp1[it]){
v1.pb(itr);
// deb(itr);
}
// deb(v1.size());
// fo(i,v1.size()){
// cout<<v1[i]<<" ";
// }cout<<nl;
sort(all(v1));
count=0;curr=-1;temp=0;
fo(i,v1.size()){
// deb(v1[i]);
if(v1[i]>curr){
temp+=(count*(count-1))/2;
count=1;
if(s2.upper_bound(v1[i])!=s2.end()){
curr=*s2.upper_bound(v1[i]);
}
else{
curr=1e6+10;
}
}
else if(v1[i]<curr){
count++;
}
// deb2(curr,temp);
}
temp+=(count*(count-1))/2;
minus+=temp;
// deb2(it,temp);
}
for(auto it : m2){
v2.clear();
for(auto itr : mp2[it]){
v2.pb(itr);
}
// deb(v2.size());
sort(all(v2));
// fo(i,v2.size()){
// cout<<v2[i]<<" ";
// }cout<<nl;
count=0;curr=-1;temp=0;
fo(i,v2.size()){
// deb(v2[i]);
if(v2[i]>curr){
temp+=(count*(count-1))/2;
count=1;
if(s1.upper_bound(v2[i])!=s1.end()){
curr=*s1.upper_bound(v2[i]);
}
else{
curr=1e6+10;
}
}
else if(v2[i]<curr){
count++;
}
}
temp+=(count*(count-1))/2;
minus+=temp;
// deb2(it,temp);
}
ans-=minus;
cout<<ans<<nl;
}
int main(){
// freopen("input.txt", "r", stdin);
// freopen("output.txt", "w", stdout);
ios_base::sync_with_stdio(0), cin.tie(0), cout.tie(0);
int t=1;
cin>>t;
while(t--){
solve();
}
}