QOJ.ac

QOJ

Time Limit: 1 s Memory Limit: 512 MB Total points: 100

#21644. Gene Sequence Problem

Statistics

Many diseases are often caused by genetic mutations. Such genetic mutations arise from a normal DNA sequence through inheritance over several generations. In other words, a normal DNA sequence can produce different genetic sequences through various mutations. A fundamental problem in bioinformatics research is, for a given DNA sequence, to determine whether it is the same when reversed.

Let $X = x_1 x_2 \dots x_n$ be a given DNA sequence, where $x_i \in \{A, C, G, T\}$ and $1 \le i \le n$. For $1 \le i \le j \le n$, the substring of $X$ from $i$ to $j$, denoted as $x_i x_{i+1} \dots x_j$, is called the $[i, j]$ segment of $X$.

We define a segment of $X$ as "good" if and only if it reads the same from left to right as it does from right to left, i.e., the segment is a palindromic genetic sequence. Now, the Swiss biologist Wilson Edwards has posed $Q$ questions to you. Each question provides two integers $L$ and $R$, and you need to calculate how many distinct "good" sub-segments exist within the $[L, R]$ segment of $X$. If a sub-segment appears multiple times within $[L, R]$, it should only be counted once.

Input

The first line contains the sequence $X$. The next line contains an integer $Q$. The following $Q$ lines each contain two integers $L$ and $R$.

Output

For each query, output a single integer on a new line representing the answer.

Constraints

  • For 10% of the data, $|S| \le 14800$, $Q \le 6500$
  • For 40% of the data, $|S| \le 23\,000$, $Q \le 23\,000$
  • For another 10% of the data, $|S| \le 5\,000$
  • For another 20% of the data, $|S| \le 50\,000$, $Q \le 50\,000$
  • For 100% of the data, $|S| \le 230\,000$, $Q \le 230\,000$

Examples

Input 1

ACCACTTGTCAGCT
5
1 5
3 8
2 7
4 13
5 6

Output 1

5
6
6
6
2

Mathematical definition of the DNA sequence elements

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.