|
| 1 | +```java |
| 2 | + |
| 3 | +import java.util.*; |
| 4 | +import java.io.*; |
| 5 | + |
| 6 | +class SegTree{ |
| 7 | + long[] tree; |
| 8 | + SegTree(int size){ |
| 9 | + tree = new long[size*4]; |
| 10 | + } |
| 11 | + void upt(int s, int e, int i, int v, int n) { |
| 12 | + if(s == e) { |
| 13 | + tree[n] = v; |
| 14 | + return; |
| 15 | + } |
| 16 | + int m=(s+e)>>1; |
| 17 | + if(i <= m) upt(s,m,i,v,n*2); |
| 18 | + else upt(m+1,e,i,v,n*2+1); |
| 19 | + tree[n] = tree[n*2] + tree[n*2+1]; |
| 20 | + } |
| 21 | + long find(int s, int e, int l, int r, int n) { |
| 22 | + if(l>r || l>e || r<s) return 0; |
| 23 | + if(l<=s && e<=r) return tree[n]; |
| 24 | + int m=(s+e)>>1; |
| 25 | + return find(s,m,l,r,n*2) + find(m+1,e,l,r,n*2+1); |
| 26 | + } |
| 27 | +} |
| 28 | + |
| 29 | +class Main { |
| 30 | + |
| 31 | + // IO field |
| 32 | + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 33 | + static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 34 | + static StringTokenizer st; |
| 35 | + |
| 36 | + static void nextLine() throws Exception {st = new StringTokenizer(br.readLine());} |
| 37 | + static int nextInt() {return Integer.parseInt(st.nextToken());} |
| 38 | + static long nextLong() {return Long.parseLong(st.nextToken());} |
| 39 | + static void bwEnd() throws Exception {bw.flush();bw.close();} |
| 40 | + |
| 41 | + // Additional field |
| 42 | + |
| 43 | + static int N; |
| 44 | + static int[] A, B, P, Q; |
| 45 | + static SegTree seg; |
| 46 | + |
| 47 | + public static void main(String[] args) throws Exception { |
| 48 | + |
| 49 | + ready(); |
| 50 | + solve(); |
| 51 | + |
| 52 | + bwEnd(); |
| 53 | + |
| 54 | + } |
| 55 | + |
| 56 | + static void ready() throws Exception{ |
| 57 | + |
| 58 | + N = Integer.parseInt(br.readLine()); |
| 59 | + A = new int[N+1]; |
| 60 | + B = new int[N+1]; |
| 61 | + |
| 62 | + for(int i=1;i<=N;i++) A[i] = Integer.parseInt(br.readLine()); |
| 63 | + for(int i=1;i<=N;i++) B[i] = Integer.parseInt(br.readLine()); |
| 64 | + |
| 65 | + } |
| 66 | + |
| 67 | + static void solve() throws Exception{ |
| 68 | + |
| 69 | + long ans = Math.min(result(A,B), result(B,A)); |
| 70 | + |
| 71 | + bw.write(ans + "\n"); |
| 72 | + |
| 73 | + } |
| 74 | + |
| 75 | + static long result(int[] a, int[] b) { |
| 76 | + |
| 77 | + P = new int[N+1]; |
| 78 | + Q = new int[N+1]; |
| 79 | + for(int i=1;i<=N;i++) { |
| 80 | + int x = a[i]; |
| 81 | + P[x] = i; |
| 82 | + } |
| 83 | + |
| 84 | + seg = new SegTree(N+1); |
| 85 | + long res = 0; |
| 86 | + |
| 87 | + for(int i=1;i<=N;i++) { |
| 88 | + int x = b[i]; |
| 89 | + Q[i] = P[x]; |
| 90 | + res += seg.find(1, N, P[x], N, 1); |
| 91 | + seg.upt(1, N, P[x], 1, 1); |
| 92 | + } |
| 93 | + |
| 94 | + long ans = res; |
| 95 | + for(int i=N;i>=1;i--) { |
| 96 | + res = (res - (N-Q[i]) + Q[i]-1); |
| 97 | + ans = Math.min(ans, res); |
| 98 | + } |
| 99 | + |
| 100 | + return ans; |
| 101 | + |
| 102 | + } |
| 103 | + |
| 104 | +} |
| 105 | + |
| 106 | +``` |
0 commit comments