Pre-order and In-order

AtCoder
IDabc255_f
Time2000ms
Memory256MB
Difficulty
Consider a **binary tree** with $N$ vertices numbered $1, 2, \ldots, N$. Here, a binary tree is a rooted tree where each vertex has at most two children. Specifically, each vertex in a binary tree has at most one **left child** and at most one **right child**. Determine whether there exists a binary tree rooted at Vertex $1$ satisfying the conditions below, and present such a tree if it exists. * The depth-first traversal of the tree in [pre-order](https://en.wikipedia.org/wiki/Tree_traversal#Pre-order,_NLR) is $(P_1, P_2, \ldots, P_N)$. * The depth-first traversal of the tree in [in-order](https://en.wikipedia.org/wiki/Tree_traversal#In-order,_LNR) is $(I_1, I_2, \ldots, I_N)$. ## Constraints * $2 \leq N \leq 2 \times 10^5$ * $N$ is an integer. * $(P_1, P_2, \ldots, P_N)$ is a permutation of $(1, 2, \ldots, N)$. * $(I_1, I_2, \ldots, I_N)$ is a permutation of $(1, 2, \ldots, N)$. ## Input Input is given from Standard Input in the following format: $N$ $P_1$ $P_2$ $\ldots$ $P_N$ $I_1$ $I_2$ $\ldots$ $I_N$ [samples]
Samples
Input #1
6
1 3 5 6 4 2
3 5 1 4 6 2
Output #1
3 6
0 0
0 5
0 0
0 0
4 2

The binary tree rooted at Vertex $1$ shown in the following image satisfies the conditions.
![image](https://img.atcoder.jp/abc255/b51399e8953ae1723d1d9e83617f9be9.png)
Input #2
2
2 1
1 2
Output #2
\-1

No binary tree rooted at Vertex $1$ satisfies the conditions, so $-1$ should be printed.
API Response (JSON)
{
  "problem": {
    "name": "Pre-order and In-order",
    "description": {
      "content": "Consider a **binary tree** with $N$ vertices numbered $1, 2, \\ldots, N$. Here, a binary tree is a rooted tree where each vertex has at most two children. Specifically, each vertex in a binary tree has",
      "description_type": "Markdown"
    },
    "platform": "AtCoder",
    "limit": {
      "time_limit": 2000,
      "memory_limit": 262144
    },
    "difficulty": "None",
    "is_remote": true,
    "is_sync": true,
    "sync_url": null,
    "sign": "abc255_f"
  },
  "statements": [
    {
      "statement_type": "Markdown",
      "content": "Consider a **binary tree** with $N$ vertices numbered $1, 2, \\ldots, N$. Here, a binary tree is a rooted tree where each vertex has at most two children. Specifically, each vertex in a binary tree has...",
      "is_translate": false,
      "language": "English"
    }
  ]
}
Full JSON Raw Segments