Cluster Features

5

1 votes
Advanced Data Structures, Data Structures, Fenwick tree, Line Sweep Technique, Medium, Segment Trees, offline
Problem

Given n 2-dimensional data objects or points in a cluster, we can define the centroid (x0,y0), radius R and diameter D of the cluster as follows.

x0=ni=1xin,y0=ni=1yin

R=ni=1(xix0)2+(yiy0)2n

D=ni=1nj=1(xixj)2+(yiyj)2n(n1)

where R is the average distance from member objects to the centroid, and D is the average pairwise distance within the cluster. Both R and D reflect the tightness of the cluster around the centroid.

Note: Take R=0 for n<1, and D=0 for n<2


Given p data objects or points, you are supposed to answer q queries. In the ith query, you consider only the points whose x-coordinate lies in [x1i,x2i] and y-coordinate lies in [y1i,y2i] as a single cluster. For each query you have to find the centroid, radius and diameter of the cluster. Since the values can be non-integer, you have to print the value of nx0+ny0+n2R2+n(n1)D2

Constraints

  • 1p,q15×104
  • 1xi,yi2×104
  • 1x1ix2i2×104
  • 1y1iy2i2×104

Input Format

The first line contains two integers p and q denoting the number of data objects or points and the number of queries/clusters respectively.

Next p lines contains two-space separated integers denoting xi and yi. The coordinates of ith data point.

Next q line contains four space-separated integer denoting x1i,x2i,y1i,and y2i respectively.

Output Format

Output q lines each containing a single integer. ith line denotes the value of nx0+ny0+n2R2+n(n1)D2 of the ith cluster.

Time Limit: 2
Memory Limit: 256
Source Limit:
Explanation

enter image description here

All the points inside yellow region(including blue) are in the cluster 1:

For cluster 1: data points are (2,4),(5,3),(5,5)

  • n=3 (number of points in the cluster)
  • (x0,y0)=(2+5+53,4+3+53)=(4,4)
  • R=83
  • D=243
  • nx0+ny0+n2R2+n(n1)D2=96

All the points inside blue region are in the cluster 2:

For cluster 2: data points are (5,3),(5,5)

  • n=2
  • (x0,y0)=(5+52,3+52)=(5,4)
  • R=1
  • D=2
  • nx0+ny0+n2R2+n(n1)D2=30
Editor Image

?