Logo Matching

Time limit2sMemory limit128 MB

Summary
Given a permutation pattern of length n and a sequence of m distinct heights, find all starting positions where a length-n window matches the relative order pattern.
Level

Hard8 of 10

Topics
String matching, Array, Sorting
Solved
No attempts yet

Problem

As part of a new advertising campaign, a large company wants to place its logo somewhere in the city. The company will spend its entire yearly advertising budget on the logo, so it has to be enormous — one manager decided to use whole buildings as parts of it.

The logo consists of nn vertical stripes of pairwise different heights, numbered 11 to nn from left to right. It is described by a permutation (s1,s2,…,sn)(s_1, s_2, \dots, s_n) of 1,2,…,n1, 2, \dots, n: stripe s1s_1 is the shortest, stripe s2s_2 is the second shortest, and so on, up to stripe sns_n, the tallest. Only the relative order of the stripe heights matters, not their actual values.

There are mm buildings along the main street, and all of their heights are pairwise different. A contiguous block of nn consecutive buildings matches the logo when, inside that block, the building at position s1s_1 is the shortest, the building at position s2s_2 is the second shortest, and so on. For example, heights 5,10,45, 10, 4 match the logo (3,1,2)(3, 1, 2): the building at position 33 (height 44) is the shortest, the one at position 11 is the second shortest, and the one at position 22 is the tallest. Find every place where the logo matches the buildings.

Input

The first line contains two integers nn and mm (2≤n≤m≤1062 \le n \le m \le 10^6).

The second line contains nn integers s1,…,sns_1, \dots, s_n, a permutation of 1,2,…,n1, 2, \dots, n (so 1≤si≤n1 \le s_i \le n and si≠sjs_i \ne s_j for i≠ji \ne j).

The third line contains mm integers h1,…,hmh_1, \dots, h_m, the heights of the buildings (1≤hi≤1091 \le h_i \le 10^9); all hih_i are different.

Within each line the integers are separated by single spaces.

Output

On the first line print the number of matches kk.

On the second line print, in increasing order and separated by single spaces, the 11-based index of the building aligned with stripe number 11 of the logo for each match — equivalently, the starting index of each matching block.

If k=0k = 0, the second line must be empty.

Note

Logo matching example

Both blocks 6,3,8,12,76, 3, 8, 12, 7 and 7,1,10,11,97, 1, 10, 11, 9 match the logo described by the permutation (2,1,5,3,4)(2, 1, 5, 3, 4). In the first block the building at position 22 (height 33) is the shortest, the building at position 11 (height 66) is the second shortest, the building at position 55 (height 77) is the third shortest, and so on.

Examples3

  1. Example 1

    Input
    5 10
    2 1 5 3 4
    5 6 3 8 12 7 1 10 11 9
    
    Expected output
    2
    2 6
    
  2. Example 2

    Input
    2 4
    1 2
    1 3 2 5
    
    Expected output
    2
    1 3
    
  3. Example 3

    Input
    3 6
    3 2 1
    9 5 1 8 4 2
    
    Expected output
    2
    1 4