-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathcirclecross.java
102 lines (80 loc) · 2.45 KB
/
circlecross.java
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
import java.io.*;
import java.lang.reflect.Array;
import java.util.*;
public class circlecross {
static private void update(int[] arr, int pos, int val)
{
int len = arr.length;
for (; pos < len; pos |= (pos + 1))
arr[pos] += val;
}
static private int query(int[] arr, int pos)
{
int sum = 0;
for (; pos >= 0; pos = (pos & (pos + 1)) - 1)
sum += arr[pos];
return sum;
}
public static void main(String[] args) throws IOException {
BufferedReader f = new BufferedReader(new FileReader("circlecross.in"));
// input file name goes above
PrintWriter out = new PrintWriter(new BufferedWriter(new FileWriter("circlecross.out")));
int n = Integer.parseInt(f.readLine());
pair[] pairs = new pair[n];
for(int i = 0; i<2*n;i++){
int ind = Integer.parseInt(f.readLine());
if(pairs[ind-1]!=null){
pairs[ind-1].y= i;
}
else {
pairs[ind-1] = new pair();
pairs[ind-1].x = i;
}
}
Arrays.sort(pairs);
//FAST METHOD
int count = 0;
int[] bit = new int[2*n+1];
for(int i =0;i<n;i++){
int start = pairs[i].x;
int end = pairs[i].y;
count+= query(bit,end)-query(bit,start);
update(bit,end,1);
}
out.println(count);
System.out.println(count);
/*
SLOW METHOD
int count = 0;
for(int i =0; i<n;i++){
for(int j =i+1; j<n;j++){
int startfirst = pairs[i][0];
int endfirst = pairs[i][1];
int startsecond = pairs[j][0];
int endsecond = pairs[j][1];
if(startfirst>startsecond){
if(endfirst>endsecond&&startfirst<endsecond){
count++;
}
}
else if (startfirst<startsecond){
if(endfirst<endsecond&&startsecond<endfirst){
count++;
}
}
}
}
out.println(count);
System.out.println(count);
*/
out.close();
}
static class pair implements Comparable<pair> {
int x;
int y;
@Override
public int compareTo(pair pair) {
return this.x-pair.x;
}
}
}