|
| 1 | +```java |
| 2 | +import java.io.*; |
| 3 | +import java.util.*; |
| 4 | + |
| 5 | +public class BOJ13711 { |
| 6 | + static class SegmentTree { |
| 7 | + int[] tree; |
| 8 | + SegmentTree(int size) { |
| 9 | + tree = new int[size*4]; |
| 10 | + } |
| 11 | + |
| 12 | + void update(int s, int e, int i, int v, int n) { |
| 13 | + if(s == e) { |
| 14 | + tree[n] = v; |
| 15 | + return; |
| 16 | + } |
| 17 | + int m = (s+e)>>1; |
| 18 | + if(i <= m) update(s, m, i, v, n*2); |
| 19 | + else update(m+1, e, i, v, n*2+1); |
| 20 | + tree[n] = Math.max(tree[n*2], tree[n*2+1]); |
| 21 | + } |
| 22 | + |
| 23 | + int find(int s, int e, int l, int r, int n) { |
| 24 | + if(l>r || l>e || r<s) return 0; |
| 25 | + if(l<=s && e<=r) return tree[n]; |
| 26 | + int m = (s+e)>>1; |
| 27 | + return Math.max(find(s, m, l, r, n*2), find(m+1, e, l, r, n*2+1)); |
| 28 | + } |
| 29 | + } |
| 30 | + |
| 31 | + static BufferedReader br = new BufferedReader(new InputStreamReader(System.in)); |
| 32 | + static BufferedWriter bw = new BufferedWriter(new OutputStreamWriter(System.out)); |
| 33 | + static StringTokenizer st; |
| 34 | + |
| 35 | + static int N; |
| 36 | + static int[] index; |
| 37 | + static SegmentTree seg; |
| 38 | + |
| 39 | + public static void main(String[] args) throws Exception { |
| 40 | + N = Integer.parseInt(br.readLine()); |
| 41 | + |
| 42 | + index = new int[N+1]; |
| 43 | + st = new StringTokenizer(br.readLine()); |
| 44 | + for(int i=1;i<=N;i++) { |
| 45 | + index[Integer.parseInt(st.nextToken())] = i; |
| 46 | + } |
| 47 | + |
| 48 | + seg = new SegmentTree(N); |
| 49 | + st = new StringTokenizer(br.readLine()); |
| 50 | + for(int i=1;i<=N;i++) { |
| 51 | + int a = Integer.parseInt(st.nextToken()); |
| 52 | + int val = seg.find(1, N, 1, index[a], 1) + 1; |
| 53 | + seg.update(1, N, index[a], val, 1); |
| 54 | + } |
| 55 | + |
| 56 | + bw.write(seg.tree[1] + "\n"); |
| 57 | + bw.close(); |
| 58 | + } |
| 59 | +} |
| 60 | +``` |
0 commit comments