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

#FT0000190. Truy vấn ký tự trong xâu (Simple String Queries)

HỌC THEO LỘ TRÌNH

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

1 lộ trình

Truy vấn ký tự trong xâu (Simple String Queries)

Simple String Queries (Truy vấn ký tự trong xâu)

Source: AtCoder – ABC157 E Original statement: https://atcoder.jp/contests/abc157/tasks/abc157_e Version: Phuoc Hung OJ; same problem semantics and maximum source limits, with educational subtasks.

Problem Statement

Given a lowercase English string S of length N indexed from 1, process 1 i c, replacing the i-th character by c (a no-op if unchanged), and 2 l r, counting how many distinct letters appear in the inclusive substring S[l..r]. Repetitions of a letter count only once.

Input

Line 1: N; line 2: string S; line 3: Q; then Q operations 1 i c or 2 l r.

Output

For each query operation, print exactly one integer on its own line in query order. Updates produce no output. Some sums exceed 32-bit integers; use signed 64-bit integers.

Subtasks

  • Subtask 1 (20 points): 1≤N≤301\le N\le 30; 1≤Q≤1001\le Q\le 100; ∣S∣=N|S|=N, string and replacement characters consist only of a–z; 1 i c has 1≤i≤N1\le i\le N; 2 l r has 1≤l≤r≤N1\le l\le r\le N; at least one type-2 query.
  • Subtask 2 (30 points): 1≤N≤30001\le N\le 3000; 1≤Q≤40001\le Q\le 4000; ∣S∣=N|S|=N, string and replacement characters consist only of a–z; 1 i c has 1≤i≤N1\le i\le N; 2 l r has 1≤l≤r≤N1\le l\le r\le N; at least one type-2 query.
  • Subtask 3 (50 points): 1≤N≤5000001\le N\le 500000; 1≤Q≤200001\le Q\le 20000; ∣S∣=N|S|=N, string and replacement characters consist only of a–z; 1 i c has 1≤i≤N1\le i\le N; 2 l r has 1≤l≤r≤N1\le l\le r\le N; at least one type-2 query.

Example 1

Input

7
abcdbbd
6
2 3 6
1 5 z
2 1 1
1 4 a
1 7 d
2 1 7

Output

3
1
5

Explanation

Positions 3–6 spell cdbb, containing 3 distinct letters. Replacing position 5 with z produces abcdzbd; querying position 1 returns 1. After changing position 4 to a and leaving position 7 as d, abcazbd has 5 distinct letters.

Operation-by-operation trace

  1. 2 3 6: substring cdbb contains distinct letters bcd; print 3.
  2. 1 5 z: replace b at position 5 with z; current string is abcdzbd.
  3. 2 1 1: substring a contains distinct letters a; print 1.
  4. 1 4 a: replace d at position 4 with a; current string is abcazbd.
  5. 1 7 d: replace d at position 7 with d; current string is abcazbd.
  6. 2 1 7: substring abcazbd contains distinct letters abcdz; print 5.

Example 2

Input

5
abcaa
6
2 1 5
1 2 a
2 1 3
1 3 a
2 1 5
2 4 4

Output

3
2
1
1

Explanation

Initially abcaa contains letters a, b and c. Replacing the second character with a makes aacaa; its first three characters have two distinct letters. Replacing the third character with a leaves aaaaa, whose every nonempty substring contains one distinct letter.

Operation-by-operation trace

  1. 2 1 5: substring abcaa contains distinct letters abc; print 3.
  2. 1 2 a: replace b at position 2 with a; current string is aacaa.
  3. 2 1 3: substring aac contains distinct letters ac; print 2.
  4. 1 3 a: replace c at position 3 with a; current string is aaaaa.
  5. 2 1 5: substring aaaaa contains distinct letters a; print 1.
  6. 2 4 4: substring a contains distinct letters a; print 1.