[2019CCPC Qinhuangdao: A] Angle Beats Category Discussion (unordered_map plus hash)

The meaning of problems: a given point n, q an interrogation points, given a query each coordinate A, Q n is selected from the two points B, C, there are many kinds of programs is such that the right triangle ABC.

Ideas: right triangle can think of those few, enumerated edge, vertex enumeration, this title will do, write enumeration of vertices, A point two cases, one is a right angle, two non-right angles. Preventing errors represents the slope of a fraction, then the map <pair <int, int> int> t found, and found that the change unordered_map unordered_map only a mapping, i.e. unordered_map <ll, int>, the hash used to obtain it, the dimensional point into a hash number.

Said that under those two cases,

 

 

#include<bits/stdc++.h>
typedef long long ll;
using namespace std;
int gcd(int a, int b) { return b == 0 ? a : gcd(b, a%b); }

int ans[2200];
unordered_map<ll,int>mp;
struct node{
    int x, y;
}p[2200], q[2200];

ll Hash(node v){  //hash
    ll A = 2333, B = 5279, C = 998244353;
    return A*v.x + B*v.y + C;
}

int main(){
    int n,w; cin >> n >> w;
    for(int i = 1;i <= n;i++) scanf("%d%d",&p[i].x, &p[i].y);
    for(int i = 1;i <= w;i++) scanf("%d%d",&q[i].x,&q[i].y);
    //第一种,A为直角顶点
    for(int i = 1;i <= w;i++){
        mp.clear();
        for(int j = 1;j <= n;j++){
            int x = q[i].x - p[j].x;
            int y = q[i].y - p[j].y;
            int g = gcd(x,y);
            x /= g;
            y /= g;
            mp[Hash((node){x, y})]++;
        }
        for(int j = 1; j <= n; j++){
            int nowx=q[i].x - p[j].x;
            int nowy=q[i].y - p[j].y;
            int g=gcd(nowx,nowy);
            nowx /= g;
            nowy /= g;
            swap(nowx, nowy);
            ans[i] += (mp[Hash((node){-nowx, nowy})] + mp[Hash((node){nowx, -nowy})]);
        }
        ans[i] /= 2;
    }
    //第二种,A为非直角顶点
    for(int i = 1; i <= n; i++){
        mp.clear();
        for(int j = 1; j <= n; j++){
            if(i == j) continue;
            int x = p[i].x -p [i] .x;
            int y = p [i] .y - p [i] .y;
            int g = gcd (x, y); 
            x / = g; 
            y / = g; 
            mp [Hash ((node) {x, y})] ++ ; 
        } 
        For ( int j = 1 ; j <= w; j ++ ) {
             int nowx = p [i] .x - q [i] .x;
            int new = p [i] .y - q [i] .y;
            int g = gcd (nowx, new); 
            nowx / = g; 
            new / = g;
            swap(nowx, nowy);
            ans[j] += (mp[Hash((node){-nowx, nowy})] + mp[Hash((node){nowx, -nowy})]);
        }
    }
    
    for(int i = 1; i <= w; i++) printf("%d\n",ans[i]);
    return 0;
}

 

Guess you like

Origin www.cnblogs.com/philo-zhou/p/11671124.html