#P4143. 采集矿石

采集矿石

Description

ZRQ finds that there are NN ores arranged in a line.

He uses a lowercase letter to represent each ore, and he also finds that each ore has an importance value ViV_i.

ZRQ wants to collect a consecutive segment of ores and bring them back to the lab.

He is very strict: the collected segment must satisfy that the lexicographic rank in descending order of this substring among all substrings equals the sum of importance values of this segment.

Here, multiple occurrences of substrings that are identical in content but appear at different positions share the same lexicographic rank.

For example, if the letter string is aa, then the rank of the first a and the second a is the same, both are 2 (the 1 is aa).

ZRQ asks you: which substrings in the original string can be collected?

Here, substrings are considered different if their positions of occurrence are different, that is, identical substrings appearing at different positions are all counted separately (of course, only if the sum of importance equals the rank).

For example, there are 44 ores, the lowercase letter string is abcd, and the importance values are 10 0 1 1.

We rank all substrings in descending lexicographic order: 1:d 2:cd 3:c 4:bcd 5:bc 6:b 7:abcd 8:abc 9:ab 10:a.

Then the rank of d is 11 (the largest), and its importance sum is 11, so it can be collected.

The rank of cd is 22, and its importance sum is 22, so it can be collected.

The rank of a is 1010, and its importance sum is 1010, so it can be collected.

Other substrings do not satisfy this condition, so there are three substrings that can be collected.

Input Format

The first line contains a string of length NN consisting of lowercase letters, where each character represents an ore.

The second line contains NN integers, representing ViV_i.

Output Format

The first line contains a single integer, the number SS of substrings that can be collected.

Then output SS lines, each with two integers L,RL,R, representing the left and right endpoints of each collectable substring, sorted by increasing left endpoint as the primary key and increasing right endpoint as the secondary key.

abcd
10 0 1 1
3
1 1
3 4
4 4
aaaa
1 1 1 1
0
aaa
1 1 1
2
1 2
2 3
aaa
1 1 2
1
1 2

Hint

There are 1010 testpoints, each worth 1010 points, totaling 100100 points.

pcg6nP.png

For all testpoints, N105N \leq 10^5, 0Vi10000 \le V_i \le 1000. It is guaranteed that the number of substrings that can be collected for each test is at most 10510^5.

Explanation for Sample #1 is included in the statement.

Explanation for Sample #2:

No substring satisfies the condition.

The rank of a is 44, and the importance sums are all 11.

The rank of aa is 33, and the importance sums are all 22.

The rank of aaa is 22, and the importance sums are all 33.

The rank of aaaa is 11, and the importance sums are all 44.

Explanation for Sample #3:

The rank of a is 33, and the importance sums are all 11.

The rank of aa is 22, and the importance sums are all 22. There are two substrings aa, at positions 11~22 and 22~33.

The rank of aaa is 11, and the importance sums are all 33.

Explanation for Sample #4:

We can see that the substring 22~33 (the second aa) no longer satisfies the condition. Its rank is still 22, but the importance sum is 33.

Translated by ChatGPT 5