Notice \ (QK \ Leq 10 ^ 5 \) , we areDo notNaturally consider root partition:
When \ (K> \ sqrt {10}. 5 ^ \) , this time \ (Q \) is relatively small, and \ (QM \) correlation algorithm more suitable. Of the string \ (S \) to build the SAM, consider each interrogation, to all \ ((a, b) \ ) according to \ (B \) from small to large, then \ (W \) in \ (S \) matches the SAM, whenever the right point becomes \ (B \) when multiplying jump string \ (s [a: b] \) a location query to endpos.
When \ (Q> \ sqrt {10}. 5 ^ \) , this time \ (K \) is relatively small, and \ (k ^ 2 \) correlation algorithm more suitable. Similarly to the string \ (S \) built SAM, enumeration left endpoint \ (L \) then \ (w [l:] \ ) in all of the calculated matching SAM \ ((l, r) \ ) answers then for all () \ (l, r) \ -half of the current inquiry determined how many of the current inquiry, you can get an answer.
Complexity \ (O (m \ sqrt {logN QK}) \) . A magical thing is the block size \ (316 \) to make life difficult but \ (315 \) can too.