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<h2h1<h2 and v1<v2v1<v2 form a rectangle if the following conditions hold:

  • 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<h2h1<h2 and v1<v2v1<v2should hold.

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<int[]> l1=new ArrayList<>();
		ArrayList<int[]> l2=new ArrayList<>();
		for(int i=0;i<n;i++) {
			int[] A=nia(4);
			if(A[0]==A[2]) {
				l2.add(new int[] {A[0],Math.min(A[1], A[3]),A[2],Math.max(A[1], A[3])});
			}else {
				l1.add(new int[] {Math.min(A[0], A[2]),A[1],Math.max(A[0], A[2]),A[3]});
			}
		}
		Collections.sort(l1,new Comparator<int[]>() {
			public int compare(int[] A1,int[] A2) {
				return A1[1]-A2[1];
			}
		});
		Collections.sort(l2,new Comparator<int[]>() {
			public int compare(int[] A1,int[] A2) {
				return A2[3]-A1[3];//上端点
			}
		});
		long ret=0;
		for(int i=0;i<l1.size();i++) {
			A=new int[10003];
			for(int j=l1.size()-1,k=0;j>i;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<l2.size()&&l2.get(k)[3]>=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<Integer>[] ng(int n, int m) {
		ArrayList<Integer>[] graph=(ArrayList<Integer>[])new ArrayList[n];
		for(int i=0;i<n;i++) {
			graph[i]=new ArrayList<>();
		}
		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<long[]>[] ngw(int n, int m) {
		ArrayList<long[]>[] graph=(ArrayList<long[]>[])new ArrayList[n];
		for(int i=0;i<n;i++) {
			graph[i]=new ArrayList<>();
		}
		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<n;i++) {
			A[i]=in.nextLong();
		}
		return A;
	}
	private int[] nia(int n) {
		int[] A=new int[n];
		for(int i=0;i<n;i++) {
			A[i]=in.nextInt()+maxn;
		}
		return A;
	}
}	

class FastReader
{
	BufferedReader br;
	StringTokenizer st;

	public FastReader()
	{
		br=new BufferedReader(new InputStreamReader(System.in));
	}


	public String next() 
	{
		while(st==null || !st.hasMoreElements())//回车,空行情况
		{
			try {
				st = new StringTokenizer(br.readLine());
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return st.nextToken();
	}

	public int nextInt() 
	{
		return Integer.parseInt(next());
	}

	public long nextLong()
	{
		return Long.parseLong(next());
	}
}

 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值