E. Count The Rectangles
time limit per test
2 seconds
memory limit per test
256 megabytes
input
standard input
output
standard output
There are nn segments drawn on a plane; the ii-th segment connects two points (xi,1xi,1, yi,1yi,1) and (xi,2xi,2, yi,2yi,2). Each segment is non-degenerate, and is either horizontal or vertical — formally, for every i∈[1,n]i∈[1,n] either xi,1=xi,2xi,1=xi,2 or yi,1=yi,2yi,1=yi,2 (but only one of these conditions holds). Only segments of different types may intersect: no pair of horizontal segments shares any common points, and no pair of vertical segments shares any common points.
We say that four segments having indices h1h1, h2h2, v1v1 and v2v2 such that h1 Please calculate the number of ways to choose four segments so they form a rectangle. Note that the conditions h1 Input The first line contains one integer nn (1≤n≤50001≤n≤5000) — the number of segments. Then nn lines follow. The ii-th line contains four integers xi,1xi,1, yi,1yi,1, xi,2xi,2 and yi,2yi,2 denoting the endpoints of the ii-th segment. All coordinates of the endpoints are in the range [−5000,5000][−5000,5000]. It is guaranteed that each segment is non-degenerate and is either horizontal or vertical. Furthermore, if two segments share a common point, one of these segments is horizontal, and another one is vertical. Output Print one integer — the number of ways to choose four segments so they form a rectangle. Examples input Copy output Copy input Copy output Copy 这题数据比较大,java版的线段树竟然过不了。所以第一次写了树状数组(BIT),快了不少,用到单点修改,区间查询。树状数组的区间修改以后再看看。这题主要是水平线排序,垂直线,双指针,存垂直线的x坐标。再枚举每两条水平线,区间查询两端的区间。
7
-1 4 -1 -2
6 -1 -2 -1
-2 3 6 3
2 -2 2 4
4 -1 4 3
5 3 5 1
5 2 1 2
7
5
1 5 1 0
0 1 5 1
5 4 0 4
4 2 4 0
4 3 4 5
0
import java.util.*;
import java.io.*;
public class Main {
public static void main(String args[]) {new Main().run();}
FastReader in = new FastReader();
PrintWriter out = new PrintWriter(System.out);
void run(){
work();
out.flush();
}
long mod=1000000007;
long gcd(long a,long b) {
return a==0?b:gcd(b%a,a);
}
int[] A;
int maxn=5001;
void work() {
int n=ni();
ArrayList