题目链接:https://codeforces.com/contest/1569/problem/D
做完C题就剩十几分钟了,没时间看了,还是比较经典的一道题。
思路:
考虑0 ~ y1之间的绿色点,假设有x个,则ans+=C2x,再减掉同一条线段上的点C2y,即可求出0 ~ y1之间有多少符合条件的解。同理求出y1 ~ y2,y2 ~ y3,…,0 ~ x0,x1 ~ x2,…
ac代码:
// Problem: D. Inconvenient Pairs
// Contest: Codeforces - Educational Codeforces Round 113 (Rated for Div. 2)
// URL: https://codeforces.com/contest/1569/problem/D
// Memory Limit: 256 MB
// Time Limit: 2000 ms
//
// Powered by CP Editor (https://cpeditor.org)
#include <bits/stdc++.h>
using namespace std;
const int N = 1e6+6;
typedef long long LL;
int t;
int n,m,k;
typedef pair<int,int>pii;
int x[N],y[N];
bool cntx[N],cnty[N];
vector<int>sumx[N],sumy[N];
int mnx[N],mny[N];
int cnt[N];
int main(){
ios::sync_with_stdio(false);
cin >> t;
while(t--){
cin >> n >> m >> k;//3e5
memset(cntx,0,sizeof cntx);
memset(cnty,0,sizeof cnty);
memset(mnx,0,sizeof mnx);
memset(mny,0,sizeof mny);
memset(cnt,0,sizeof cnt);
for(int i = 0;i <= m + 1;i++)sumy[i].clear();
for(int i = 0;i <= n + 1;i++)sumx[i].clear();
vector<pii>p(k+1);
for(int i = 0;i < n;i++)
cin >> x[i],cntx[x[i]] = 1;
for(int j = 0;j < m;j++)
cin >> y[j],cnty[y[j]] = 1;
for(int i = 1;i <= k;i++)
cin >> p[i].first >> p[i].second;
for(int i = 1;i <= k;i++){
if(cntx[p[i].first]&&cnty[p[i].second])continue;
if(cntx[p[i].first])
sumy[lower_bound(y,y+m,p[i].second) - y].push_back(i);
else
sumx[lower_bound(x,x+n,p[i].first) - x].push_back(i);
}
LL ans = 0;
for(int i = 0;i <= m + 1;i++){
int sz = sumy[i].size();
ans+=(LL)sz*(LL)(sz - 1)/2;
for(int j = 0;j < sz;j++)cnt[p[sumy[i][j]].first]++;//注意这里不要写成cnt[x[sumy[i][j]]].
for(int j = 0;j < sz;j++)
if(cnt[p[sumy[i][j]].first] > 1)
ans-=(LL)cnt[p[sumy[i][j]].first]*(LL)(cnt[p[sumy[i][j]].first] - 1)/2,cnt[p[sumy[i][j]].first] = 0;
else if(cnt[p[sumy[i][j]].first]==1)cnt[p[sumy[i][j]].first] = 0;
}
for(int i = 0;i <= n + 1;i++){
int sz = sumx[i].size();
ans+=(LL)sz*(LL)(sz - 1)/2;
for(int j = 0;j < sz;j++)cnt[p[sumx[i][j]].second]++;
for(int j = 0;j < sz;j++)
if(cnt[p[sumx[i][j]].second] > 1)
ans-=(LL)cnt[p[sumx[i][j]].second]*(LL)(cnt[p[sumx[i][j]].second] - 1)/2,cnt[p[sumx[i][j]].second] = 0;
else if(cnt[p[sumx[i][j]].second] == 1)cnt[p[sumx[i][j]].second] = 0;
}
cout << ans << endl;
}
return 0;
}