#P11262. Similarity of Sequences
Similarity of Sequences
Similarity of Sequences
We are given a sequence . Two sequences of equal length, ( [a_1,a_2,\ldots,a_k] ) and ( [b_1,b_2,\ldots,b_k] ), are defined to be almost equal if and only if there exists exactly one index ( 1\le p\le k ) such that ( a_p\neq b_p ). They are defined to be similar if one can rearrange the elements of each sequence so that they become almost equal.
Given ( m ) queries. Each query provides four integers ( l_1, r_1, l_2, r_2 ) representing two subarrays ( [a_{l_1},a_{l_1+1},\ldots,a_{r_1}] ) and ( [a_{l_2},a_{l_2+1},\ldots,a_{r_2}] ) (it is guaranteed that ( r_1-l_1=r_2-l_2 ), i.e. they have equal lengths). Your task is to determine whether these two subarrays are similar.
Explanation: Let the two subarrays be denoted as ( A ) and ( B ) of length ( k ). They are similar if the multisets of the two subarrays differ in exactly two elements: there exists one element ( x ) that appears one more time in ( A ) than in ( B ) and one element ( y ) (with ( x\neq y )) that appears one less time in ( A ) than in ( B ), while all other elements appear the same number of times. In other words, if we define ( d(z)=#{z\ \textrm{in }A}-#{z\ \textrm{in }B} ) for each integer ( z ), then we must have ( d(x)=1 ), ( d(y)=-1 ) and for all other ( z;,; d(z)=0 ).
inputFormat
The first line contains two integers ( n ) and ( m ) (( 1\le n,m\le 200000 )). The next line contains ( n ) integers ( a_1,a_2,\ldots,a_n ) representing the sequence. Each of the following ( m ) lines contains four integers ( l_1, r_1, l_2, r_2 ) (with ( 1\le l_1\le r_1\le n ) and ( 1\le l_2\le r_2\le n )) describing a query. It is guaranteed that ( r_1-l_1=r_2-l_2 ).
outputFormat
For each query, output a single line containing YES
if the two subarrays are similar according to the definition above, or NO
otherwise.
sample
5 3
1 2 3 2 1
1 3 3 5
2 4 1 3
1 5 1 5
NO
YES
NO
</p>