From 25107708149c4e7105c2c97df602c7d35d6cae53 Mon Sep 17 00:00:00 2001 From: oncsr Date: Tue, 18 Mar 2025 17:08:03 +0900 Subject: [PATCH] =?UTF-8?q?[20250318]=20BOJ=20/=20P5=20/=20=EB=B9=8C?= =?UTF-8?q?=EB=B3=B4=EC=9D=98=20=EC=83=9D=EC=9D=BC=20/=20=EA=B6=8C?= =?UTF-8?q?=ED=98=81=EC=A4=80?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- ...4\354\235\230 \354\203\235\354\235\274.md" | 87 +++++++++++++++++++ 1 file changed, 87 insertions(+) create mode 100644 "khj20006/202503/18 BOJ P5 \353\271\214\353\263\264\354\235\230 \354\203\235\354\235\274.md" diff --git "a/khj20006/202503/18 BOJ P5 \353\271\214\353\263\264\354\235\230 \354\203\235\354\235\274.md" "b/khj20006/202503/18 BOJ P5 \353\271\214\353\263\264\354\235\230 \354\203\235\354\235\274.md" new file mode 100644 index 00000000..ddd8bcd2 --- /dev/null +++ "b/khj20006/202503/18 BOJ P5 \353\271\214\353\263\264\354\235\230 \354\203\235\354\235\274.md" @@ -0,0 +1,87 @@ +```java + +import java.util.*; +import java.io.*; + +class SegTree{ + int[] tree; + SegTree(int size){ + tree = new int[size*4+1]; + } + + void upt(int s, int e, int i, int n) { + if(s==e) { + tree[n] = 1; + return; + } + int m=(s+e)>>1; + if(i<=m) upt(s,m,i,n*2); + else upt(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); + } +} + +class Main { + + // IO field + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); + static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); + static StringTokenizer st = new StringTokenizer(""); + + static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());} + static String nextToken() throws Exception { + if(!st.hasMoreTokens()) nextLine(); + return st.nextToken(); + } + static int nextInt() throws Exception { return Integer.parseInt(nextToken()); } + static long nextLong() throws Exception { return Long.parseLong(nextToken()); } + static void bwEnd() throws Exception {bw.flush();bw.close();} + + // Additional field + + static int N; + static SegTree seg; + static TreeMap T; + + public static void main(String[] args) throws Exception { + + for(N = nextInt();N != 0;N = nextInt()) { + + ready(); + solve(); + } + + bwEnd(); + + } + + static void ready() throws Exception{ + + T = new TreeMap<>(); + seg = new SegTree(N); + for(int i=1;i<=N;i++) T.put(nextToken(), i); + + } + + static void solve() throws Exception{ + + long ans = 0; + for(int i=1;i<=N;i++) { + int x = T.get(nextToken()); + ans += seg.find(1, N, x, N, 1); + seg.upt(1,N,x,1); + } + bw.write(ans + "\n"); + + } + +} + +```