diff --git "a/khj20006/202507/28 BOJ P4 \354\206\214\352\260\200 \352\270\270\354\235\204 \352\261\264\353\204\210\352\260\204 \354\235\264\354\234\240 9.md" "b/khj20006/202507/28 BOJ P4 \354\206\214\352\260\200 \352\270\270\354\235\204 \352\261\264\353\204\210\352\260\204 \354\235\264\354\234\240 9.md" new file mode 100644 index 00000000..85a27bf4 --- /dev/null +++ "b/khj20006/202507/28 BOJ P4 \354\206\214\352\260\200 \352\270\270\354\235\204 \352\261\264\353\204\210\352\260\204 \354\235\264\354\234\240 9.md" @@ -0,0 +1,124 @@ +```java +import java.util.*; +import java.io.*; + +class IOController { + BufferedReader br; + BufferedWriter bw; + StringTokenizer st; + + public IOController() { + br = new BufferedReader(new InputStreamReader(System.in)); + bw = new BufferedWriter(new OutputStreamWriter(System.out)); + st = new StringTokenizer(""); + } + + String nextLine() throws Exception { + String line = br.readLine(); + st = new StringTokenizer(line); + return line; + } + + String nextToken() throws Exception { + while (!st.hasMoreTokens()) nextLine(); + return st.nextToken(); + } + + int nextInt() throws Exception { + return Integer.parseInt(nextToken()); + } + + long nextLong() throws Exception { + return Long.parseLong(nextToken()); + } + + double nextDouble() throws Exception { + return Double.parseDouble(nextToken()); + } + + void close() throws Exception { + bw.flush(); + bw.close(); + } + + void write(String content) throws Exception { + bw.write(content); + } + +} + +class SegTree { + int[] tree; + SegTree(int size) { + tree = new int[size*4]; + } + + void update(int s, int e, int i, int n) { + if(s == e) { + tree[n]++; + return; + } + int m = (s+e)>>1; + if(i <= m) update(s,m,i,n*2); + else update(m+1,e,i,n*2+1); + tree[n] = tree[n*2] + tree[n*2+1]; + } + + int find(int s, int e, int l, int r, int n) { + if(l>r || l>e || r>1; + return find(s,m,l,r,n*2) + find(m+1,e,l,r,n*2+1); + } +} + +public class Main { + + static IOController io; + + // + + static int N; + static int[] a, p; + static boolean[] v; + static SegTree seg; + + public static void main(String[] args) throws Exception { + + io = new IOController(); + + init(); + solve(); + + io.close(); + + } + + static void init() throws Exception { + + N = io.nextInt(); + a = new int[2*N+1]; + p = new int[N+1]; + v = new boolean[N+1]; + for(int i=1;i<=2*N;i++) { + a[i] = io.nextInt(); + p[a[i]] = i; + } + seg = new SegTree(N*2); + + } + + static void solve() throws Exception { + + long ans = 0; + for(int i=1;i<=2*N;i++) if(!v[a[i]]) { + v[a[i]] = true; + ans += seg.find(1,N*2,i,p[a[i]],1); + seg.update(1, N*2, p[a[i]],1); + } + io.write(ans + "\n"); + + } + +} +```