Sorting Machine

시간 제한1.5초메모리 제한2048 MB

요약
각 질의마다 행 A..B에서 열 L..R만 남긴 뒤, X번째 행이 안정 정렬 후 몇 번째에 오는지 구한다.
난이도

보통10점 중 7점

유형
정렬, 문자열, 분할 정복, 구현
정답자
아직 제출이 없습니다

문제

Adrian knows that Morgan the robot is capable of sorting texts, but he is uncertain about Morgan’s efficiency in doing the task. Adrian decides to give Morgan a test on his efficiency.

First, Adrian gives Morgan a list of NN equal-length texts, numbered from 11 to NN. Each text is a string S_iS\_i that contains MM characters, indexed from 11 to MM. S_ijS\_{ij} represents character jj in string S_iS\_i.

Adrian will give Morgan QQ tasks. Each task is represented by a tuple \<A,B,L,R,X>\<A, B, L, R, X> satisfying the following.

  • 1≤A≤B≤N1 ≤ A ≤ B ≤ N
  • 1≤L≤R≤M1 ≤ L ≤ R ≤ M
  • 1≤X≤B−A+11 ≤ X ≤ B - A + 1

For each task, Morgan should perform the following procedures.

  1. Copy the original list of texts; let TT be the copied list. This list will be updated throughout the task.
  2. Remove all texts ii from TT that are not within the range of A≤i≤BA ≤ i ≤ B.
  3. For all remaining texts in TT, remove all characters at index jj that are not within the range of L≤j≤RL ≤ j ≤ R;
  4. The remaining texts in TT is renumbered from 11 to B−A+1B - A + 1. Mark text XX in TT.
  5. Sort TT lexicographically; let the result be T′T'. Note that the performed sort is a stable sort, meaning that if two texts are equal, then they maintain their order in the sorted list.
  6. Output the position of the marked text in T′T'. The lexicographically smallest text will be at position 11 (one-based).

The image below are the ilustrations how the procedure works.

It turns out that it takes Morgan a lot of time to solve those tasks. Therefore, Adrian asks for your help to improve Morgan’s program so that he can solve those tasks quickly and accurately.

A string ss of length nn is lexicographically smaller than string tt with the same length if there exists an integer 1≤i≤n1 ≤ i ≤ n such that s_j=t_js\_j = t\_j for all 1≤j<i1 ≤ j < i, and s_i<t_is\_i < t\_i.

입력

Input begins with two integers NN MM (1≤N,M≤100,0001 ≤ N, M ≤ 100\\, 000; 1≤N×M≤100,0001 ≤ N \times M ≤ 100\\, 000) representing the number of texts and the length of each text, respectively. Each of the next NN lines contains a string S_iS\_i representing text ii. Each text contains MM lower-case characters.

The next line contains an integer QQ (1≤Q≤100,0001 ≤ Q ≤ 100\\, 000) representing the number of tasks. Each of the next QQ lines contains five integers AA BB LL RR XX (1≤A≤B≤N1 ≤ A ≤ B ≤ N; 1≤L≤R≤M1 ≤ L ≤ R ≤ M; 1≤X≤B−A+11 ≤ X ≤ B - A + 1) representing a task.

출력

For each task, output an integer in a single line representing the answer of that task.

예제2

  1. 예제 1

    입력
    5 6
    adrian
    morgan
    george
    undine
    stella
    5
    1 5 1 6 1
    1 5 1 6 2
    1 2 3 6 1
    2 4 3 5 3
    1 2 5 6 2
    
    예상 출력
    1
    3
    2
    1
    2
    
  2. 예제 2

    입력
    3 9
    indonesia
    nationaln
    contestco
    3
    2 2 1 9 1
    1 2 3 7 2
    1 3 2 5 2
    
    예상 출력
    1
    2
    1