# CF EDU 113 D - Inconvenient Pairs

2022-05-16 22:03:17  阅读：37  来源： 互联网

# D - Inconvenient Pairs

### 思维

#include <iostream>
#include <cstring>
#include <algorithm>
#include <vector>
#include <cmath>
#include <map>

using namespace std;
typedef long long ll;
typedef pair<int, int> PII;
const int N = 3e5 + 10;
int n, m, k;
int row[N], col[N];
map<int, int> xcnt, ycnt;
map<int, bool> xban, yban;
struct Point
{
int x, y;
}p[N];

bool cmp1(Point A, Point B)
{
if (A.x == B.x)
return A.y < B.y;
return A.x < B.x;
}

bool cmp2(Point A, Point B)
{
if (A.y == B.y)
return A.x < B.x;
return A.y < B.y;
}

void init()
{
xcnt.clear();
ycnt.clear();
xban.clear();
yban.clear();
}
int main()
{
ios::sync_with_stdio(false), cin.tie(0), cout.tie(0);
int T;
cin >> T;
while(T--)
{
cin >> n >> m >> k;
init();
for (int i = 1; i <= n; i++)
{
cin >> col[i];
xban[col[i]] = true;
}

for (int i = 1; i <= m; i++)
{
cin >> row[i];
yban[row[i]] = true;
}

for (int i = 1; i <= k; i++)
cin >> p[i].x >> p[i].y;
sort(p + 1, p + k + 1, cmp2);
int id = 2;
ll ans = 0;
for (int i = 1; i <= k; i++)
{
int x = p[i].x, y = p[i].y;
if (yban[y])
continue;
if (y > row[id])
{
ll add = 0, sum = 0;
for (auto t : xcnt)
{
sum += t.second;
add -= t.second * (t.second - 1) / 2;
}
add += sum * (sum - 1) / 2;
xcnt.clear();
while(y > row[id]) id++;
}
xcnt[x]++;
}
if (xcnt.size())
{

ll add = 0, sum = 0;
for (auto t : xcnt)
{
sum += t.second;
add -= t.second * (t.second - 1) / 2;
}
add += sum * (sum - 1) / 2;
xcnt.clear();
}
sort(p + 1, p + k + 1, cmp1);
id = 2;
for (int i = 1; i <= k; i++)
{
int x = p[i].x, y = p[i].y;
if (xban[x])
continue;
if (x > col[id])
{
ll add = 0, sum = 0;
for (auto t : ycnt)
{
sum += t.second;
add -= t.second * (t.second - 1) / 2;
}
add += sum * (sum - 1) / 2;
ycnt.clear();
while(x > col[id]) id++;
}
ycnt[y]++;
}
if (ycnt.size())
{
ll add = 0, sum = 0;
for (auto t : ycnt)
{
sum += t.second;
add -= t.second * (t.second - 1) / 2;
}
add += sum * (sum - 1) / 2;
ycnt.clear();
}
cout << ans << endl;
}
return 0;
}