P18120 [ICPC 2026 Seoul R] Boss Raid Log
Description
You are a wizard participating in a boss raid. There are $k$ different types of spells, numbered from $1$ to $k$. To defeat the boss, $m$ spells must be cast in a specific order. Let $P = (P_1, P_2, \ldots, P_m)$ denote this required spell sequence, where each $P_i$ denotes the type of the $i$-th spell in the sequence. The spells in the required sequence do not need to be cast by the same wizard, as different spells may be cast by different wizards. The same spell type may appear more than once in $P$.
During the raid, the wizards cast spells, and the raid log records their spell types in chronological order, but not the identities of the wizards who cast them. We assume that no two spells are cast at the same time. Suppose that spells are recorded at $n$ different times during the battle. The resulting raid log is $S = (S_1, S_2, \ldots, S_n)$, where each $S_i$ is a log entry representing the spell type recorded at the $i$-th time. For a successful attack, consecutive spells in the required sequence must be sufficiently separated in the raid log. Let $d$ denote the minimum distance between the recorded times of two consecutive spells in the required sequence.
We say that the raid log contains a successful attack if there exist $m$ recorded times $1 \le t_1 < t_2 < \cdots < t_m \le n$ satisfying both of the following conditions:
1. $S_{t_j} = P_j$ for every $1 \le j \le m$.
2. $t_{j+1} - t_j \ge d$ for every $1 \le j < m$.
A log entry $S_i$ is *covered* if it is included in at least one successful attack; that is, if $i = t_j$ for some $1 \le j \le m$ in some successful attack. Finally, the raid log $S$ is called *consistent* if every log entry of $S$ is covered.
For example, suppose $d = 2$, $P = (1, 3, 2)$ and $S = (1, 1, 3, 3, 3, 2, 2, 2)$. Then the following are successful attacks: (a) recorded times $1, 3, 6$, (b) recorded times $2, 5, 7$, and (c) recorded times $2, 4, 8$. Every log entry of $S$ is covered by at least one of these successful attacks, and hence $S$ is consistent. On the other hand, suppose $d = 2$, $P = (1, 3, 2)$ and $S = (1, 3, 3, 3, 2, 2)$. The second log entry $S_2$ is not covered because it cannot be included in any successful attack. Therefore, $S$ is not consistent.
Given $k$, $d$, the required spell sequence $P$, and the raid log $S$, write a program to determine whether $S$ is consistent.
Input Format
Your program is to read from standard input. The input starts with a line containing four integers $k$, $m$, $n$, and $d$ ($1 \le k \le 200{,}000$; $1 \le m \le n \le 200{,}000$; $1 \le d \le n$), which denote the number of spell types, the length of the required spell sequence, the number of entries in the raid log, and the minimum required distance between consecutive spells in a successful attack, respectively.
The next line contains $m$ integers $P_1, P_2, \ldots, P_m$ ($1 \le P_j \le k$), which denote the required spell sequence.
The following line contains $n$ integers $S_1, S_2, \ldots, S_n$ ($1 \le S_i \le k$), which denote the raid log.
Output Format
Your program is to write to standard output. Print exactly one line. Print `YES` if the raid log $S$ is consistent and print `NO` otherwise.