#FT0000190. Truy vấn ký tự trong xâu (Simple String Queries)
Bài tập thuộc lộ trình
Mục lục đề ⌄
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): ; ; , string and replacement characters consist only of
a–z;1 i chas ;2 l rhas ; at least one type-2 query. - Subtask 2 (30 points): ; ; , string and replacement characters consist only of
a–z;1 i chas ;2 l rhas ; at least one type-2 query. - Subtask 3 (50 points): ; ; , string and replacement characters consist only of
a–z;1 i chas ;2 l rhas ; 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
2 3 6: substringcdbbcontains distinct lettersbcd; print 3.1 5 z: replacebat position 5 withz; current string isabcdzbd.2 1 1: substringacontains distinct lettersa; print 1.1 4 a: replacedat position 4 witha; current string isabcazbd.1 7 d: replacedat position 7 withd; current string isabcazbd.2 1 7: substringabcazbdcontains distinct lettersabcdz; 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
2 1 5: substringabcaacontains distinct lettersabc; print 3.1 2 a: replacebat position 2 witha; current string isaacaa.2 1 3: substringaaccontains distinct lettersac; print 2.1 3 a: replacecat position 3 witha; current string isaaaaa.2 1 5: substringaaaaacontains distinct lettersa; print 1.2 4 4: substringacontains distinct lettersa; print 1.