QOJ.ac

QOJ

حد الوقت: 5 s حد الذاكرة: 1024 MB مجموع النقاط: 100

#5014. Repetition Degree

الإحصائيات

Given a string $s[1, n]$, each character $i$ is assigned two weights: a left weight $wl_i$ and a right weight $wr_i$.

The left weight $vl(s[l, r])$ of a substring $s[l, r]$ is defined as the sum of the left weights $wl$ of all its matching left endpoints in the original string. The right weight $vr(s[l, r])$ is defined as the sum of the right weights $wr$ of all its matching right endpoints in the original string. Here, all matches of $t$ in $s$ are defined as $\forall 1 \leq i \leq j \leq n, s[i, j]=t$, and we call such $i$ and $j$ the left and right endpoints of a match, respectively.

The "repetition degree" of a substring $s[l, r]$ is defined as the product of its left weight and right weight, i.e., $w(s[l, r])=vl(s[l, r]) \cdot vr(s[l, r])$.

There are $q$ queries, each given by $l_1, r_1, l_2, r_2$. Let $S=\left\{(l, r) \mid r-l \geq \max \left\{r_1-l_1, r_2-l_2\right\}, s\left[l, l+r_1-l_1\right]=s\left[l_1, r_1\right], s\left[r-r_2+l_2, r\right]=s\left[l_2, r_2\right]\right\}$, which is the set of all pairs $(l, r)$ such that $s[l_1, r_1]$ is a prefix of $s[l, r]$ and $s[l_2, r_2]$ is a suffix of $s[l, r]$. Calculate $\sum_{(l, r) \in S} w(s[l, r])$.

Since the answer can be very large, you only need to output the result modulo $2^{64}$.

Input

The first line contains two positive integers $n$ and $q$, representing the length of the string and the number of queries.

The second line contains a string $s$ consisting of lowercase letters.

The third line contains $n$ integers $wl_i$, representing the left weights.

The fourth line contains $n$ integers $wr_i$, representing the right weights.

The next $q$ lines each contain four integers $l_1, r_1, l_2, r_2$, representing a query.

Output

Output $q$ lines, where the $i$-th line contains the answer to the $i$-th query modulo $2^{64}$.

Examples

Input 1

6 5
ababcc
1 1 1 1 1 1
1 1 1 1 1 1
1 1 5 5
3 4 1 2
1 1 2 2
1 2 6 6
5 5 1 2

Output 1

4
9
9
4
0

Constraints

It is guaranteed that $1 \leq n, q \leq 10^5$, $0 \leq wl_i, wr_i < 2^{64}$, $1 \leq l_1 \leq r_1 \leq n$, $1 \leq l_2 \leq r_2 \leq n$, and $s$ consists only of lowercase letters.

  • Subtask 1 (7 pts): $n, q \leq 500$.
  • Subtask 2 (15 pts): $n, q \leq 5000$.
  • Subtask 3 (12 pts): $n \leq 5000$.
  • Subtask 4 (6 pts): $l_1=1$, and for any $2 \leq i \leq n$, $s_1 \neq s_i$.
  • Subtask 5 (16 pts): $s[l_1, r_1]$ appears at most 5 times in $s$.
  • Subtask 6 (21 pts): $n, q \leq 5 \times 10^4$.
  • Subtask 7 (23 pts): No additional restrictions.

Editorials

IDTypeStatusTitlePosted ByLast UpdatedActions
EditorialOpen Official EditorialQingyu- Download

Discussions

About Discussions

The discussion section is only for posting: General Discussions (problem-solving strategies, alternative approaches), and Off-topic conversations.

This is NOT for reporting issues! If you want to report bugs or errors, please use the Issues section below.

Open Discussions 0
No discussions in this category.

Issues

About Issues

If you find any issues with the problem (statement, scoring, time/memory limits, test cases, etc.), you may submit an issue here. A problem moderator will review your issue.

Guidelines:

  1. This is not a place to publish discussions, editorials, or requests to debug your code. Issues are only visible to you and problem moderators.
  2. Do not submit duplicated issues.
  3. Issues must be filed in English or Chinese only.
Active Issues 0
No issues in this category.
Closed/Resolved Issues 0
No issues in this category.