C - Bib
Source: atcoder
Difficulty: 300

Problem Statement
There are
N
people numbered from
1
to
N
.
Person
i
is wearing a bib with the number
Q_i
and is staring at person
P_i
.
For each
i = 1,2,\ldots,N
, find the number written on the bib of the person that the person wearing the bib with number
i
is staring at.
Constraints
2 \leq N \leq 3\times 10^5
1 \leq P_i \leq N
The values of
P_i
are distinct.
1 \leq Q_i \leq N
The values of
Q_i
are distinct.
All input values are integers.
Input
The input is given from Standard Input in the following format:
N
P_1
P_2
\dots
P_N
Q_1
Q_2
\dots
Q_N
Output
Let
S_i
be the number written on the bib of the person that the person wearing the bib with number
i
is staring at.
Print
S_1, S_2, \ldots, S_N
in this order, separated by a single space.
Sample Input 1
4
4 3 2 1
2 3 1 4
Sample Output 1
3 4 1 2
Person
3
is wearing the bib with the number
1
, and the person that person
3
is staring at, person
2
, is wearing the bib with the number
3
.
Thus, the answer for
i = 1
is
3
.
Sample Input 2
10
2 6 4 3 7 8 9 10 1 5
1 4 8 2 10 5 7 3 9 6
Sample Output 2
4 8 6 5 3 10 9 2 1 7

C++ solution:
#include <bits/stdc++.h>
using namespace std;
int main(){
	ios::sync_with_stdio(false);
	cin.tie(nullptr);
	int N;
	cin>>N;
	vector<int> P(N+1),Q(N+1);
	for (int i=1;i<=N;i++) {
		cin>>P[i];
	}
	for (int i=1;i<=N;i++) {
		cin>>Q[i];
	}
	vector<int> person(N+1);
	for (int i=1;i<=N;i++) {
		person[Q[i]]=i;
	}
	vector<int> S(N+1);
	for(int k=1;k<=N;k++) {
		int i=person[k]; 
		int j=P[i];      
		S[k]=Q[j];
	}
	for (int k=1;k<=N;k++) {
		cout<<S[k]<<(k==N?'\n':' ');
	}
	return 0;
}

Go solution:
package main

import (
	"bufio"
	"fmt"
	"math"
	"os"
	"sort"
	"strconv"
	"strings"
)

var sc = bufio.NewScanner(os.Stdin)
var wr = bufio.NewWriter(os.Stdout)

func main() {
	defer wr.Flush()
	sc.Split(bufio.ScanWords)
	sc.Buffer([]byte{}, math.MaxInt32)

	N := readInt()
	P := readInts(N)
	Q := readInts(N)

	ans := make([]int, N)
	for i := 0; i < N; i++ {
		ans[Q[i]-1] = Q[P[i]-1]
	}
	for i := 0; i < N; i++ {
		out(ans[i])
	}

}

func out(x ...interface{}) {
	fmt.Fprintln(wr, x...)
}

func outStrings(x []string) {
	out(strings.Join(x, " "))
}

func outInts(x []int) {
	y := make([]string, len(x))
	for i := 0; i < len(x); i++ {
		y[i] = strconv.Itoa(x[i])
	}
	outStrings(y)
}

func readInt() int {
	sc.Scan()
	i, e := strconv.Atoi(sc.Text())
	if e != nil {
		panic(e)
	}
	return i
}

func readInt64() int64 {
	sc.Scan()
	i, e := strconv.ParseInt(sc.Text(), 0, 64)
	if e != nil {
		panic(e)
	}
	return i
}

func readFloat() float64 {
	sc.Scan()
	i, e := strconv.ParseFloat(sc.Text(), 64)
	if e != nil {
		panic(e)
	}
	return i
}

func readInts(N int) []int {
	ret := make([]int, N)
	for i := 0; i < N; i++ {
		ret[i] = readInt()
	}
	return ret
}

func readFloats(N int) []float64 {
	ret := make([]float64, N)
	for i := 0; i < N; i++ {
		ret[i] = readFloat()
	}
	return ret
}

func readText() string {
	sc.Scan()
	return sc.Text()
}

func readRune() []rune {
	sc.Scan()
	return []rune(sc.Text())
}

// min, max, aSub, absなど基本関数
func max(a, b int) int {
	if a > b {
		return a
	}
	return b
}

func min(a, b int) int {
	if a < b {
		return a
	}
	return b
}

// min for n entry
func nMin(a ...int) int {
	ret := a[0]
	for _, e := range a {
		ret = min(ret, e)
	}
	return ret
}

// max for n entry
func nMax(a ...int) int {
	ret := a[0]
	for _, e := range a {
		ret = max(ret, e)
	}
	return ret
}

func chMin(a *int, b int) bool {
	if *a < b {
		return false
	}
	*a = b
	return true
}

func chMax(a *int, b int) bool {
	if *a > b {
		return false
	}
	*a = b
	return true
}

func aSub(a, b int) int {
	if a > b {
		return a - b
	}
	return b - a
}

func abs(a int) int {
	if a >= 0 {
		return a
	}
	return -a
}

func lowerBound(a []int, x int) int {
	idx := sort.Search(len(a), func(i int) bool {
		return a[i] >= x
	})
	return idx
}

func upperBound(a []int, x int) int {
	idx := sort.Search(len(a), func(i int) bool {
		return a[i] > x
	})
	return idx
}

func swap(a, b *int) {
	*a, *b = *b, *a
}

func swapArrElm(a []int, i, j int) {
	a[i], a[j] = a[j], a[i]
}

Java solution:
import java.io.BufferedReader;
import java.io.InputStreamReader;
import java.util.*;

public class Main {
	public static void main(String args[]) throws Exception {

		// 受け取り
		BufferedReader br = new BufferedReader(new InputStreamReader(System.in));
		int n = Integer.parseInt(br.readLine());

		String[] lookArray = new String[n];
		String[] zekkenArray = new String[n];
		lookArray = br.readLine().split(" ");
		zekkenArray = br.readLine().split(" ");
		
		String[] sArray = new String[n];
		for (int i = 1; i < n+1; i++) {
			int sindex = Integer.parseInt(zekkenArray[i-1]); 
			int lookIndex = Integer.parseInt(lookArray[i-1]);
			sArray[sindex-1] = zekkenArray[lookIndex-1];
		}
		
		String ans = String.join(" ", sArray);
		System.out.println(ans);
	}
}

JavaScript solution:
/** @param {String} stdin */
function main(stdin){
  let [[N],P,Q]=stdin.trim().split("\n").map(v=>v.split(" ").map(e=>e-1))
  N++
  const revQ=Array(N).fill(0)
  for(let i=0;i<N;i++){
    revQ[Q[i]]=i
  }
  const ans=[]
  for(let i=0;i<N;i++){
    ans.push(Q[P[revQ[i]]]+1)
  }
  console.log(ans.join(" "))
}

main(require("fs").readFileSync("/dev/stdin","utf8"))

Python solution:
n = int(input())
p = list(map(int, input().split()))
q = list(map(int, input().split()))

ans = [-1 for _ in range(n)]
for i in range(n):
    ans[q[i]-1] = q[p[i]-1]
print(' '.join(map(str, ans)))

Ruby solution:
N = gets.to_i
P=gets.split.map(&:to_i)
Q=gets.split.map(&:to_i)

ans = Array.new(N+1,0)
for i in (0...N)
  pos = P[i]-1
  ans[Q[i]] = Q[pos]
end
ans.shift
puts ans.join(" ")