TOJ Right Triangles II

本文介绍了一种算法,用于计算平面上N个点中能构成直角三角形的组合数量,这些三角形的两条直角边需平行于坐标轴。文章提供了完整的C++代码实现,并解释了输入输出格式。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

Right Triangles II 分享至QQ空间 去爱问答提问或回答

Time Limit(Common/Java):1000MS/3000MS     Memory Limit:65536KByte
Total Submit: 53            Accepted: 14

Description

N points are placed in the coordinate plane.
Write a program that calculates how many ways we can choose three points so that they form a right triangle with legs parallel to the coordinate axes.
A right triangle has one 90-degree internal angle. The legs of a right triangle are its two shorter sides.

Input

The first line of input contains the integer N (3 ≤ N ≤ 100 000), the number of points.
Each of the following N lines contains two integers X and Y (1 ≤ X, Y ≤ 100 000), the coordinates of one point.
No pair of points will share the same pair of coordinates.

Output

Output the number of triangles.

Sample Input

3
4 2
2 1
1 3

Sample Output

0

Uploader

crq

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
#include <vector>
using namespace std;

const int maxn = 100008;

vector<__int64> x[maxn];
__int64 y[maxn];

int n;

void work() {
    __int64 i, j;
    __int64 cnt = 0;
    for(i = 1; i < maxn; i++) {//maxn --> n !!!
        __int64 Size = x[i].size();
        if(Size <= 1) continue;
        for(j = 0; j < Size; j++) {
            __int64 id = x[i][j];
            if(y[id] > 0)
                cnt += (Size - 1) * (y[id] - 1);
        }
    }
    printf("%I64d\n", cnt);
}

void init() {
    int i;
    for(i = 0; i < maxn; i++) {
        x[i].clear();
        y[i] = 0;
    }
}

int main()
{
    int i;
    int dx, dy;
    while(scanf("%d", &n) != EOF) {
        init();
        for(i = 0; i < n; i++) {
            scanf("%d%d", &dx, &dy);
            x[dx].push_back(dy);
            y[dy]++;
        }
        work();
    }
    return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值