#P3709. 大爷的字符串题

    ID: 2685 远端评测题 1000ms 125MiB 尝试: 0 已通过: 0 难度: 7 上传者: 标签>莫队离散化洛谷原创O2优化洛谷月赛

大爷的字符串题

Description

You are given a string aa. For each query, you are asked about the contribution of a sub-interval.

Definition of contribution:

Each time, pick a character xx from this interval (the order is up to you), then delete xx from the interval. Repeat until the interval is empty. You need to maintain a set SS.

  • If SS is empty, your rp decreases by 11.
  • If there exists an element in SS that is not less than xx, then your rp decreases by 11, and you clear SS.
  • After that, insert xx into SS.

Since you are the boss, problems you have done usually appear in exams. After you process all characters in this interval, what is the maximum rp you can still have? The initial rp is 00.

Queries are independent.

Input Format

The first line contains two integers n,mn, m, the length of the string and the number of queries.

The next line contains nn numbers. The ii-th integer is the ii-th character aia_i of the given string.

Then mm lines follow. Each line contains two integers l,rl, r, meaning a query interval.

Output Format

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

3 3
3 3 3
3 3
3 3
3 3
-1
-1
-1

Hint

Constraints

  • For 10%10\% of the testdata, they are the sample.
  • For another 10%10\% of the testdata, n,m100n, m \le 100.
  • For another 10%10\% of the testdata, n,m103n, m \le 10^3.
  • For another 10%10\% of the testdata, n,m104n, m \le 10^4.
  • For another 10%10\% of the testdata, n,m105n, m \le 10^5.
  • For 100%100\% of the testdata, 1n,m2×1051 \leq n, m \le 2 \times 10^5, 1ai1091 \leq a_i \leq 10^9, 1l,rn1 \leq l, r \leq n.

The testdata is as easy as a certain province’s NOI Qualifier Day 1 T2. Feel free to pass it with brute force.

It’s okay. As long as you are in a good school, even if you can only get 10 points on this problem, you can still make the team.

Translated by ChatGPT 5