#P6514. [QkOI#R1] Quark and Strings

[QkOI#R1] Quark and Strings

Description

You need to maintain a sequence of strings {Sn}\{S_n\}, where there are nn strings, all initially empty. Then there are qq operations, and two types of operations are supported. Let the current operation be the ii-th one. The character set in this problem is [1,q]N+[1,q]\cap \N_+:

  • 1 l r means appending the character ii to the end of every string whose index is in [l,r][l,r]. Here ii is an integer in the range [1,q][1,q].
  • 2 l r means querying the length of the longest common subsequence of all strings whose indices are in [l,r][l,r]. When l=rl=r, we consider the longest common subsequence length to be the length of that string.

Input Format

The first line contains two positive integers n,qn,q.

The next qq lines each contain three positive integers in the form 1 l r or 2 l r.

Output Format

For each operation 22, output one line with a non-negative integer as your answer.

5 3
1 1 3
1 1 5
2 1 4

1
8 8
2 8 8
1 3 8
2 5 8
1 1 8
1 1 1
2 3 5
2 1 7
1 2 7
0
1
2
1

Hint

Sample Explanation

For the first sample:
After the first operation, the sequence is {[1],[1],[1],[],[]}\{[1],[1],[1],[],[]\}.
After the second operation, the sequence is {[1,2],[1,2],[1,2],[2],[2]}\{[1,2],[1,2],[1,2],[2],[2]\}.
For the third operation, it is easy to see that the longest common subsequence queried is [2][2], with length 11.


Constraints

This problem uses bundled testdata.

  • Subtask 1 (10 pts): n,q500n,q\le 500.
  • Subtask 2 (20 pts): n,q1000n,q\le 1000.
  • Subtask 3 (15 pts): n1000n\le 1000, operation 11 occurs no more than 500500 times, time limit 20002000ms.
  • Subtask 4 (15 pts): n1000n\le 1000, operation 22 occurs no more than 500500 times, time limit 20002000ms.
  • Subtask 5 (40 pts): no special restrictions, time limit 30003000ms.

For 100%100\% of the data, 1n,q1051\le n,q\le 10^5. Except for the specially marked Subtasks, the time limit for the other Subtasks is 10001000ms.

Translated by ChatGPT 5