CF1194E. Count The Rectangles(树状数组)

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

  • segments h1h1 and h2h2 are horizontal;
  • segments v1v1 and v2v2 are vertical;
  • segment h1h1 intersects with segment v1v1;
  • segment h2h2 intersects with segment v1v1;
  • segment h1h1 intersects with segment v2v2;
  • segment h2h2 intersects with segment v2v2.

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

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

output

Copy

7

input

Copy

5
1 5 1 0
0 1 5 1
5 4 0 4
4 2 4 0
4 3 4 5

output

Copy

0

这题数据比较大,java版的线段树竟然过不了。所以第一次写了树状数组(BIT),快了不少,用到单点修改,区间查询。树状数组的区间修改以后再看看。这题主要是水平线排序,垂直线,双指针,存垂直线的x坐标。再枚举每两条水平线,区间查询两端的区间。

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 l1=new ArrayList<>();
		ArrayList l2=new ArrayList<>();
		for(int i=0;i() {
			public int compare(int[] A1,int[] A2) {
				return A1[1]-A2[1];
			}
		});
		Collections.sort(l2,new Comparator() {
			public int compare(int[] A1,int[] A2) {
				return A2[3]-A1[3];//上端点
			}
		});
		long ret=0;
		for(int i=0;ii;j--) {
				int s=Math.max(l1.get(i)[0], l1.get(j)[0]);
				int e=Math.min(l1.get(i)[2], l1.get(j)[2]);
				if(s>=e)continue;
				while(k=l1.get(j)[1]) {
					if(l2.get(k)[1]<=l1.get(i)[1]) {
						update(l2.get(k)[0],1);
					}
					k++;
				}
				long v=query(s,e);
				ret+=v*(v-1)/2;
			}
		}
		out.println(ret);
	}
	
	private long query(int s, int e) {
		return query(e)-query(s-1);
	}
	
	long query(int x) {
		long ret=0;
		for(;x>0;x-=lowbit(x)) {
			ret+=A[x];
		}
		return ret;
	}
	
	int lowbit(int x){
		return x&-x;
	}
	
	private void update(int x, int v) {
		for(;x<10003;x+=lowbit(x)) {
			A[x]+=v;
		}
	}


	//input
	private ArrayList[] ng(int n, int m) {
		ArrayList[] graph=(ArrayList[])new ArrayList[n];
		for(int i=0;i();
		}
		for(int i=1;i<=m;i++) {
			int s=in.nextInt()-1,e=in.nextInt()-1;
			graph[s].add(e);
			graph[e].add(s);
		}
		return graph;
	}
	
	private ArrayList[] ngw(int n, int m) {
		ArrayList[] graph=(ArrayList[])new ArrayList[n];
		for(int i=0;i();
		}
		for(int i=1;i<=m;i++) {
			long s=in.nextLong()-1,e=in.nextLong()-1,w=in.nextLong();
			graph[(int)s].add(new long[] {e,w});
			graph[(int)e].add(new long[] {s,w});
		}
		return graph;
	}

	private int ni() {
		return in.nextInt();
	}

	private long nl() {
		return in.nextLong();
	}

	private long[] na(int n) {
		long[] A=new long[n];
		for(int i=0;i

 

你可能感兴趣的:(Codeforces)