-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtriangles.txt
159 lines (150 loc) · 5.82 KB
/
triangles.txt
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
import java.io.*;
import java.util.*;
public class triangles {
public static void main(String[] args) throws IOException {
BufferedReader f = new BufferedReader(new FileReader("triangles.in"));
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("triangles.out")),true);
int pointnum = Integer.parseInt(f.readLine());
long mod = 1000000000+7;
Points[] points = new Points[pointnum];
for(int i=0;i<pointnum;i++){
StringTokenizer st = new StringTokenizer(f.readLine());
long x = Long.parseLong(st.nextToken())+10000;
long y = Long.parseLong(st.nextToken())+10000;
points[i]=new Points(x,y);
}
Points[] sorty = new Points[pointnum];
Points[] sortx = new Points[pointnum];
for(int i=0; i<pointnum;i++){
sortx[i]=points[i];
sorty[i]=points[i];
}
Arrays.sort(sortx, new Comparator<Points>() {
@Override
public int compare(Points points, Points t1) {
if(points.x != t1.x){
return Long.compare(points.x,t1.x);
}else {
return Long.compare(points.y,t1.y);
}
}
});
Arrays.sort(sorty, new Comparator<Points>() {
@Override
public int compare(Points points, Points t1) {
if(points.y != t1.y){
return Long.compare(points.y,t1.y);
}else {
return Long.compare(points.x,t1.x);
}
}
});
ArrayList<ArrayList<Points>> groupX = new ArrayList<>();
ArrayList<ArrayList<Points>> groupY = new ArrayList<>();
long CurrentX = sortx[0].x;
long CurrentY = sorty[0].y;
int indexX = 0;
int indexY=0;
groupX.add(new ArrayList<>());
groupY.add(new ArrayList<>());
for(int i=0; i<pointnum; i++){
if(sortx[i].x==CurrentX){
groupX.get(indexX).add(sortx[i]);
}else{
groupX.add(new ArrayList<>());
indexX++;
groupX.get(indexX).add(sortx[i]);
CurrentX=sortx[i].x;
}
if(sorty[i].y==CurrentY){
groupY.get(indexY).add(sorty[i]);
}else{
groupY.add(new ArrayList<>());
indexY++;
groupY.get(indexY).add(sorty[i]);
CurrentY=sorty[i].y;
}
}
HashMap<Points,int[]> mapX =new HashMap<>();
HashMap<Points,int[]> mapY = new HashMap<>();
for(int i=0; i< groupX.size(); i++){
for(int j=0; j<groupX.get(i).size();j++){
mapX.put(groupX.get(i).get(j),new int[]{i,j});
}
}
for(int i=0; i< groupY.size(); i++){
for(int j=0; j<groupY.get(i).size(); j++){
mapY.put(groupY.get(i).get(j),new int[]{i,j});
}
}
ArrayList<long[][]> XSUM = new ArrayList<>();
ArrayList<long[][]> YSUM = new ArrayList<>();
for(int i=0; i<groupX.size(); i++){
long a = 0;
long b=0;
long[] differences = new long[groupX.get(i).size()-1];
for(int j=1; j<groupX.get(i).size();j++){
differences[j-1]=groupX.get(i).get(j).y-groupX.get(i).get(j-1).y;
}
for(int j=0; j<groupX.get(i).size()-1;j++){
a+=differences[j]*((groupX.get(i).size()-1)-j);
}
for(int j=differences.length-1; j>-1; j--){
b+=differences[j]*(j+1);
}
long[] ans1 = new long[groupX.get(i).size()];
long[] ans2 = new long[groupX.get(i).size()];
ans1[0]=a;
ans2[ans2.length-1]=b;
for(int j=1; j< ans1.length; j++){
ans1[j]=ans1[j-1]-(ans1.length-j)*differences[j-1];
}
for(int j=ans2.length-2; j>-1; j--){
ans2[j]=ans2[j+1]-(j+1)*differences[j];
}
XSUM.add(new long[][]{ans1,ans2});
}
for(int i=0; i<groupY.size(); i++){
long a = 0;
long b=0;
long[] differences = new long[groupY.get(i).size()-1];
for(int j=1; j<groupY.get(i).size();j++){
differences[j-1]=groupY.get(i).get(j).x-groupY.get(i).get(j-1).x;
}
for(int j=0; j<groupY.get(i).size()-1;j++){
a+=differences[j]*((groupY.get(i).size()-1)-j);
}
for(int j=differences.length-1; j>-1; j--){
b+=differences[j]*(j+1);
}
long[] ans1 = new long[groupY.get(i).size()];
long[] ans2 = new long[groupY.get(i).size()];
ans1[0]=a;
ans2[ans2.length-1]=b;
for(int j=1; j< ans1.length; j++){
ans1[j]=ans1[j-1]-(ans1.length-j)*differences[j-1];
}
for(int j=ans2.length-2; j>-1; j--){
ans2[j]=ans2[j+1]-(j+1)*differences[j];
}
YSUM.add(new long[][]{ans1,ans2});
}
long ans = 0;
for(Points point : sortx){
ans+=((XSUM.get(mapX.get(point)[0])[0][mapX.get(point)[1]]+XSUM.get(mapX.get(point)[0])[1][mapX.get(point)[1]])*(YSUM.get(mapY.get(point)[0])[0][mapY.get(point)[1]]+YSUM.get(mapY.get(point)[0])[1][mapY.get(point)[1]]))%mod;
}
out.println(ans%mod);
out.close();
}
}
class Points{
public long x;
public long y;
public Points(long x, long y){
this.x=x;
this.y=y;
}
public String toString(){
return "Point x: "+ x + " Point y: "+y;
}
}