#P8992. [北大集训 2021] 扑克比大小

[北大集训 2021] 扑克比大小

Background

CTT2021 D3T3

Problem Description

Little ZZ and Little AA are playing a poker card comparison game.

The rules of their game are as follows:

  • Before the game starts, the system deals a pile of hand cards to Little ZZ and a pile of hand cards to Little AA (the numbers of cards in the two piles may be different). Each card has a lowercase letter written on it.

  • In each round of the game, Little ZZ and Little AA simultaneously flip the first card on the top of their piles. If the two flipped cards are different, then the player whose card corresponds to the smaller lowercase letter wins. If the two flipped cards are the same, they put the flipped cards into the bottom of their piles and continue the game until one side wins.

In fact, the system deals cards from a huge deck. Specifically, suppose the deck has nn cards, which are a1,a2,,ana_1,a_2,\cdots,a_n. Then the system randomly selects cards from the ll-th card to the rr-th card to deal to a player. In other words, from the top of the pile to the bottom of the pile, the cards are al,al+1,,ara_l,a_{l+1},\cdots,a_r.

Now Little ZZ and Little AA will play a total of qq games. Little ZZ learns in some way that, in the ii-th game, the cards dealt to Little AA are ali,ali+1,,aria_{l_i},a_{l_i+1},\cdots,a_{r_i}. Little ZZ wants to know how many possible hand card piles he could have that would allow him to beat Little AA. Two hand card piles are considered different if and only if the numbers of cards in the two piles are different, or there exists a position dd such that the cards at distance dd from the top of the piles are different.

Input Format

The first line contains a string aa consisting only of lowercase letters.

The second line contains a positive integer qq and an integer typetype, where typetype indicates the data type.

The next qq lines each contain two integers lil_i and rir_i on line ii.

Output Format

Output qq lines. Each line contains an integer indicating how many possible hand card piles Little ZZ could have that would allow him to beat Little AA.

abbab
5 0
1 3
2 4
3 5
1 4
2 5

4
7
6
2
8

Hint

For all testdata, it holds that 1liria5×1051\le l_i\le r_i\le |a| \le 5\times 10^5, 1q5×1051\le q \le 5\times 10^5.

Subtask Score nn\le qq\le typetype
11 33 10210^2 00
22 500500 2,0002,000
33 44 2,0002,000
44 55 2×1042\times 10^4
55 1313 10510^5 33
66 1717 00
77 1515 5×1055\times 10^5 11
88 22
99 2525 00

The meaning of data type typetype is as follows:

  • type=0type=0: The data has no special restrictions.

  • type=1type=1: It is guaranteed that 1lra\exists 1\le l'\le r'\le |a|, ali,ri+ali,ri=al,ra_{l_i,r_i}+a_{l_i,r_i}=a_{l',r'}.

  • type=2type=2: It is guaranteed that rl=rili+1\forall r'-l'=r_i-l_i+1, if al,r1=ali,ria_{l',r'-1}=a_{l_i,r_i}, then it must hold that aralia_{r'}\neq a_{l_i}.

  • type=3type=3: It is guaranteed that rili105\sum r_i-l_i \le 10^5.

Here al,ra_{l,r} denotes the string alal+1ara_la_{l+1}\cdots a_r. The result of concatenating two strings a+ba+b is the string obtained by appending bb after aa in order.

Translated by ChatGPT 5