|
| 1 | +```java |
| 2 | + |
| 3 | +import java.util.*; |
| 4 | +import java.io.*; |
| 5 | + |
| 6 | +class SegTree{ |
| 7 | + int[] tree; |
| 8 | + SegTree(int size){ |
| 9 | + tree = new int[size*4 + 1]; |
| 10 | + } |
| 11 | + void upt(int s, int e, int i, int n) { |
| 12 | + if(s == e) { |
| 13 | + tree[n]++; |
| 14 | + return; |
| 15 | + } |
| 16 | + int m=(s+e)>>1; |
| 17 | + if(i<=m) upt(s,m,i,n*2); |
| 18 | + else upt(m+1,e,i,n*2+1); |
| 19 | + tree[n] = tree[n*2] + tree[n*2+1]; |
| 20 | + } |
| 21 | + int 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, pos; |
| 45 | + static boolean[] vis; |
| 46 | + static SegTree seg; |
| 47 | + |
| 48 | + public static void main(String[] args) throws Exception { |
| 49 | + |
| 50 | + ready(); |
| 51 | + solve(); |
| 52 | + |
| 53 | + bwEnd(); |
| 54 | + |
| 55 | + } |
| 56 | + |
| 57 | + static void ready() throws Exception{ |
| 58 | + |
| 59 | + N = Integer.parseInt(br.readLine()); |
| 60 | + A = new int[N+1]; |
| 61 | + pos = new int[N+1]; |
| 62 | + vis = new boolean[N+1]; |
| 63 | + seg = new SegTree(N); |
| 64 | + |
| 65 | + nextLine(); |
| 66 | + for(int i=1;i<=N;i++) { |
| 67 | + int a = nextInt(); |
| 68 | + A[i] = a; |
| 69 | + pos[a] = i; |
| 70 | + } |
| 71 | + |
| 72 | + } |
| 73 | + |
| 74 | + static void solve() throws Exception{ |
| 75 | + |
| 76 | + int s = 1, e = 2; |
| 77 | + nextLine(); |
| 78 | + for(int i=1;i<=N-2;i++) { |
| 79 | + int b = nextInt(); |
| 80 | + int p = pos[b] - seg.find(1, N, 1, pos[b], 1); |
| 81 | + vis[b] = true; |
| 82 | + while(vis[A[s]]) s++; |
| 83 | + while(vis[A[e]] || s >= e) e++; |
| 84 | + seg.upt(1, N, pos[b], 1); |
| 85 | + if(p%2 != 0) continue; |
| 86 | + int temp = pos[A[s]]; |
| 87 | + pos[A[s]] = pos[A[e]]; |
| 88 | + pos[A[e]] = temp; |
| 89 | + temp = A[s]; |
| 90 | + A[s] = A[e]; |
| 91 | + A[e] = temp; |
| 92 | + } |
| 93 | + |
| 94 | + int p = nextInt(), q = nextInt(); |
| 95 | + if(p == A[s] && q == A[e]) bw.write("Possible"); |
| 96 | + else bw.write("Impossible"); |
| 97 | + |
| 98 | + } |
| 99 | + |
| 100 | +} |
| 101 | + |
| 102 | +``` |
0 commit comments