Codeforces 1234D Distinct Characters Queries set、lower_bound的妙用

D. Distinct Characters Queries

time limit per test: 2 seconds

memory limit per test: 256 megabytes

You are given a string ss consisting of lowercase Latin letters and qq queries for this string.

Recall that the substring s[l;r] of the string ss is the string SlSl+1…SrSlSl+1…Sr. For example, the substrings of "codeforces" are "code", "force", "f", "for", but not "coder" and "top".

There are two types of queries:

  • 1 pos c (1 ≤ pos ≤ |s|c is lowercase Latin letter): replace Spos with c (set Spos:=c);
  • 2 l r (1 ≤ l ≤ r ≤ |s|): calculate the number of distinct characters in the substring s[l;r].

Input

The first line of the input contains one string s consisting of no more than 10^5 lowercase Latin letters.

The second line of the input contains one integer q (1 ≤ q ≤ 10^5) — the number of queries.

The next qq lines contain queries, one per line. Each query is given in the format described in the problem statement. It is guaranteed that there is at least one query of the second type.

Output

For each query of the second type print the answer for it — the number of distinct characters in the required substring in this query.

Examples

input

abacaba
5
2 1 4
1 4 b
1 5 b
2 4 6
2 1 7

output

3
1
2

input

dfcbbcfeeedbaea
15
1 6 e
1 4 b
2 6 14
1 7 b
1 12 c
2 6 8
2 1 6
1 7 c
1 2 f
1 10 a
2 7 9
1 10 a
1 14 b
1 1 f
2 1 11

output

5
2
5
2
6

 题意

给定一个字符串 S,然后有 q 组查询,每次查询为 "1" 时,将 s[pos] 替换为 c;查询为 "2" 时,输出从 l 到 r 区间,s[l...r] 这个子串里的不同字符的个数。

分析

我们可以用一组 set 保存每个字符出现的位置。替换时,将对应字符的 set 中保存的当前位置 erase 掉,然后将新添加的字符对应的 set 添加一个当前位置。查询时,只需要在每个字符的 set 中二分查找看有没有一个值 x,使得 l <= x <= r,如果有的话那就 cnt++,最后输出 cnt 即可。

lower_bound() 和 upper_bound() 的使用:

两者都是在排好序的数组中进行二分查找

lower_bound(begin, end, num):从数组的 begin 位置到 end-1 位置二分查找第一个大于或等于 num 的数字,找到返回该数字的地址,不存在则返回 end。通过返回的地址减去起始地址 begin,得到找到数字在数组中的下标。

upper_bound(begin, end, num):从数组的 begin 位置到 end-1 位置二分查找第一个大于 num 的数字,找到返回该数字的地址,不存在则返回 end。通过返回的地址减去起始地址 begin,得到找到数字在数组中的下标。

代码

#include <iostream>
#include <set>
#include <algorithm>
#include <vector>
using namespace std;

vector<set<int>> v(26);

int main()
{
    string s;
    int n, q, l, r, pos;
    char c;
    cin >> s;

    int len = s.length();
    for(int i = 0; i < len; i++){
        v[s[i] - 'a'].insert(i);
    }

    cin >> n;
    while(n--){
        cin >> q;
        if(q == 1){
            cin >> pos >> c;
            pos--;
            v[s[pos] - 'a'].erase(pos);
            s[pos] = c;
            v[s[pos] - 'a'].insert(pos);
        }
        else {
            cin >> l >> r;
            l--, r--;
            int cnt = 0;
            for(int i = 0; i < 26; i++){
                auto it = v[i].lower_bound(l);
                if(it != v[i].end() && *it <= r){
                    cnt++;
                }
            }
            cout << cnt << endl;
        }
    }
    return 0;
}

 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值