Skip to content

Commit f67ce10

Browse files
authored
Merge pull request #561 from AlgorithmWithGod/khj20006
[20250728] BOJ / P4 / 소가 길을 건너간 이유 9 / 권혁준
2 parents 63ace9d + d3fe18b commit f67ce10

File tree

1 file changed

+124
-0
lines changed

1 file changed

+124
-0
lines changed
Lines changed: 124 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,124 @@
1+
```java
2+
import java.util.*;
3+
import java.io.*;
4+
5+
class IOController {
6+
BufferedReader br;
7+
BufferedWriter bw;
8+
StringTokenizer st;
9+
10+
public IOController() {
11+
br = new BufferedReader(new InputStreamReader(System.in));
12+
bw = new BufferedWriter(new OutputStreamWriter(System.out));
13+
st = new StringTokenizer("");
14+
}
15+
16+
String nextLine() throws Exception {
17+
String line = br.readLine();
18+
st = new StringTokenizer(line);
19+
return line;
20+
}
21+
22+
String nextToken() throws Exception {
23+
while (!st.hasMoreTokens()) nextLine();
24+
return st.nextToken();
25+
}
26+
27+
int nextInt() throws Exception {
28+
return Integer.parseInt(nextToken());
29+
}
30+
31+
long nextLong() throws Exception {
32+
return Long.parseLong(nextToken());
33+
}
34+
35+
double nextDouble() throws Exception {
36+
return Double.parseDouble(nextToken());
37+
}
38+
39+
void close() throws Exception {
40+
bw.flush();
41+
bw.close();
42+
}
43+
44+
void write(String content) throws Exception {
45+
bw.write(content);
46+
}
47+
48+
}
49+
50+
class SegTree {
51+
int[] tree;
52+
SegTree(int size) {
53+
tree = new int[size*4];
54+
}
55+
56+
void update(int s, int e, int i, int n) {
57+
if(s == e) {
58+
tree[n]++;
59+
return;
60+
}
61+
int m = (s+e)>>1;
62+
if(i <= m) update(s,m,i,n*2);
63+
else update(m+1,e,i,n*2+1);
64+
tree[n] = tree[n*2] + tree[n*2+1];
65+
}
66+
67+
int find(int s, int e, int l, int r, int n) {
68+
if(l>r || l>e || r<s) return 0;
69+
if(l<=s && e<=r) return tree[n];
70+
int m = (s+e)>>1;
71+
return find(s,m,l,r,n*2) + find(m+1,e,l,r,n*2+1);
72+
}
73+
}
74+
75+
public class Main {
76+
77+
static IOController io;
78+
79+
//
80+
81+
static int N;
82+
static int[] a, p;
83+
static boolean[] v;
84+
static SegTree seg;
85+
86+
public static void main(String[] args) throws Exception {
87+
88+
io = new IOController();
89+
90+
init();
91+
solve();
92+
93+
io.close();
94+
95+
}
96+
97+
static void init() throws Exception {
98+
99+
N = io.nextInt();
100+
a = new int[2*N+1];
101+
p = new int[N+1];
102+
v = new boolean[N+1];
103+
for(int i=1;i<=2*N;i++) {
104+
a[i] = io.nextInt();
105+
p[a[i]] = i;
106+
}
107+
seg = new SegTree(N*2);
108+
109+
}
110+
111+
static void solve() throws Exception {
112+
113+
long ans = 0;
114+
for(int i=1;i<=2*N;i++) if(!v[a[i]]) {
115+
v[a[i]] = true;
116+
ans += seg.find(1,N*2,i,p[a[i]],1);
117+
seg.update(1, N*2, p[a[i]],1);
118+
}
119+
io.write(ans + "\n");
120+
121+
}
122+
123+
}
124+
```

0 commit comments

Comments
 (0)