题目连接:
http://acm.sgu.ru/problem.php?contest=0&problem=532
Description
A new office building is to appear in Berland soon. Its construction has just been started, and the first problem builders are facing is to lay the foundation.
The ground at construction site area has already been hardened along n segments. Each segment is given by integer coordinates of its endpoints in the site area coordinate system. Every segment has a positive length and is parallel to either Ox axis or Oy axis. It's important to note that the ground hardening was done in such a way that only perpendicular segments could possibly have common points.
The decision has been made for the foundation to have a rectangular form. The rectangle must have the following properties:
it should have a positive area,
its sides should be parallel to one of the coordinate axes,
its sides should be situated on the hardened ground, i.e. each point of its perimeter should belong to at least one segment out of the n hardened ones.
You are to help estimating the difficulty of choosing such a rectangle. Write a program that finds the number of rectangles that can possibly be used as a foundation.
Input
The first line contains integer n (1 ≤ n ≤ 600) — the number of hardened segments. Each of the following n lines contains four space-separated integers x1, y1, x2, y2 (-109 ≤ x1,y1,x2,y2 ≤ 109) — coordinates of the segments' endpoints. Each segment has positive length and is parallel to either Ox axis or Oy axis. No two horizontal segments have a common point. No two vertical segments have a common point.
Output
Print a single integer — the number of rectangles that can possibly be used as a foundation.
Sample Input
4
0 0 1 0
0 0 0 1
1 1 1 -1
1 1 0 1
Sample Output
1
Hint
题意
在平面上给你n条平行于坐标轴的线段,然后问你能够组成多少个矩形
题解:
直接暴力枚举两条边,然后统计有多少条边同时经过这两条边就好了
然后贡献就是n*(n-1)/2,n表示经过这两条边的边数
代码
#include<bits/stdc++.h>
using namespace std;
#define maxn 1005
int x1[maxn],x2[maxn],yy1[maxn],y2[maxn];
int cross(int k1,int k2)
{
if(x1[k1]==x2[k1]&&x1[k2]==x2[k2])
return 0;
if(yy1[k1]==y2[k1]&&y2[k2]==yy1[k2])
return 0;
if(yy1[k1]==y2[k1])
swap(k1,k2);
if(x1[k1]<=x2[k2]&&x1[k1]>=x1[k2] && yy1[k1]<=yy1[k2]&&y2[k1]>=yy1[k2])
return 1;
return 0;
}
bitset<maxn> s[maxn];
int main()
{
int n;
scanf("%d",&n);
for(int i=0;i<n;i++)
{
scanf("%d%d%d%d",&x1[i],&yy1[i],&x2[i],&y2[i]);
if(x1[i]>x2[i])swap(x1[i],x2[i]);
if(yy1[i]>y2[i])swap(yy1[i],y2[i]);
}
for(int i=0;i<n;i++)
for(int j=i+1;j<n;j++)
if(cross(i,j))
s[i][j]=1,s[j][i]=1;
long long ans = 0;
for(int i=0;i<n;i++)
for(int j=i+1;j<n;j++)
{
bitset<maxn> t = s[i]&s[j];
int p = t.count();
ans+=p*(p-1)/2;
}
cout<<ans/2<<endl;
}