PHƯỚC HƯNG OJ · THỬ THÁCH THUẬT TOÁN

#PS0000129. Đếm số lần đổi trạng thái (Count State Changes)

HỌC THEO LỘ TRÌNH

Bài tập thuộc lộ trình

1 lộ trình

Đếm số lần đổi trạng thái (Count State Changes)

Count State Changes

Source: Phước Hưng OJ

Version: Phuoc Hung OJ Extended

Problem Statement

Given a string ss of nn lowercase English letters, indexed from 1, a state change occurs between adjacent unequal characters. For every interval [l,r][l,r], count changes whose two characters both lie inside it: positions ii with l<i≤rl<i\le r and si≠si−1s_i\ne s_{i-1}. The string is immutable.

Input

The first line contains n,qn,q, the length and query count.

The second line contains the string ss of length nn.

Each of the next qq lines contains l,rl,r.

Output

Print qq lines, each containing the number of changes inside its interval.

Subtasks

  • Subtask 1 (30 points): 1≤n≤1001\le n\le100; 1≤q≤1001\le q\le100; 1≤l≤r≤n1\le l\le r\le n for each query; ∣s∣=n|s|=n; si∈{a,…,z}s_i\in\{\mathtt{a},\ldots,\mathtt{z}\} for every 1≤i≤n1\le i\le n.
  • Subtask 2 (30 points): 1≤n≤2000001\le n\le200000; 1≤q≤2000001\le q\le200000; 1≤l≤r≤n1\le l\le r\le n for each query; ∣s∣=n|s|=n; si∈{a,b}s_i\in\{\mathtt{a},\mathtt{b}\} for every 1≤i≤n1\le i\le n.
  • Subtask 3 (40 points): 1≤n≤2000001\le n\le200000; 1≤q≤2000001\le q\le200000; 1≤l≤r≤n1\le l\le r\le n for each query; ∣s∣=n|s|=n; si∈{a,…,z}s_i\in\{\mathtt{a},\ldots,\mathtt{z}\} for every 1≤i≤n1\le i\le n.

Example 1

Input

6 2
abbaaa
1 6
2 6

Output

2
1

Explanation

In 1–6, changes occur at pairs (1,2) from a to b and (3,4) from b to a. Only (3,4) lies entirely inside 2–6, giving 1.