CF1569D. Inconvenient Pairs(思维)

原题链接
题意中说所有的点肯定会落在某条横线或纵线上,或者落在横线和纵线交点上,这是个很重要的性质。
如果点在横线和纵线交点上的话,他和其他点的曼哈顿距离就是最短距离。
如果某个点落在某条横线上,并且在两条纵线之间,那么在两条纵线之间并且不在同一条横线的点都可以跟他有贡献。
用map标记一下,计算贡献就好了。
参考

// 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;
typedef long long ll;
typedef unsigned long long ull;
typedef pair<ll, ll>PLL;
typedef pair<int, int>PII;
typedef pair<double, double>PDD;
typedef pair<string,string>PSS;
#define I_int ll
inline ll read(){ll x = 0, f = 1;char ch = getchar();while(ch < '0' || ch > '9'){if(ch == '-')f = -1;ch = getchar();}while(ch >= '0' && ch <= '9'){x = x * 10 + ch - '0';ch = getchar();}return x * f;}
 
inline void write(ll x){if (x < 0) x = ~x + 1, putchar('-');if (x > 9) write(x / 10);putchar(x % 10 + '0');}
 
#define read read()
#define closeSync ios::sync_with_stdio(0);cin.tie(0);cout.tie(0)
#define multiCase int T;cin>>T;for(int t=1;t<=T;t++)
#define rep(i,a,b) for(int i=(a);i<=(b);i++)
#define repp(i,a,b) for(int i=(a);i<(b);i++)
#define per(i,a,b) for(int i=(a);i>=(b);i--)
#define inf 0x3f3f3f3f

ll ksm(ll a, ll b,ll mod){ll res = 1;while(b){if(b&1)res=res*a%mod;a=a*a%mod;b>>=1;}return res;}
 

int n,m,k;
vector<int>vx,vy;

int main(){
    int _=read;
    while(_--){
        n=read,m=read,k=read;
        rep(i,1,n){
            vx.push_back(read);
        }
        rep(i,1,m) vy.push_back(read);
        map<int,int>mpx,mpy;
        map<PII,int>mpxy,mpyx;
        ll ans=0;
        while(k--){
            int x=read,y=read;
            int px=lower_bound(vx.begin(),vx.end(),x)-vx.begin();//二分落在哪个纵线上
            int py=lower_bound(vy.begin(),vy.end(),y)-vy.begin();//二分落在哪个横线上
            if(vx[px]==x&&vy[py]==y) continue;//在交点处 无贡献
            if(vx[px]==x){//在纵线上
                ans=ans+mpy[py]-mpxy[{px,py}];//横线之间的点和减去在两线之间并且在纵线上的点
                mpy[py]++;mpxy[{px,py}]++;//更新
            }
            else{
                ans=ans+mpx[px]-mpyx[{py,px}];
                mpx[px]++;
                mpyx[{py,px}]++;
            }
        }
        printf("%lld
",ans);
        vx.clear();vy.clear();
    }
    return 0;    
}

原文地址:https://www.cnblogs.com/OvOq/p/15245840.html