codeforces - 1234D Distinct Characters Queries

题目链接:https://vjudge.net/problem/CodeForces-1234D

题目描述:

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

Recall that the substring s[l;r]s[l;r] of the string ss is the string slsl+1srslsl+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 c1 pos c (1pos|s|1≤pos≤|s|, cc is lowercase Latin letter): replace sposspos with cc (set spos:=cspos:=c);
  • 2 l r2 l r (1lr|s|1≤l≤r≤|s|): calculate the number of distinct characters in the substring s[l;r]s[l;r].

Input

The first line of the input contains one string ss consisting of no more than 105105 lowercase Latin letters.

The second line of the input contains one integer qq (1q1051≤q≤105) — 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

  题意是给你两种操作,一种是改变某个位置的字符,一个是计算某个区间[l,r]内的不同字符数量
因为数据比较大所以直接暴力肯定是不行的,我们可以把用set把每个字母的位置按照顺序记下来,对
于操作1,我们可以删除原来字符对应的位置然后再添加新字符对应的位置,对于操作2,我么只要判
断一下每个字符的位置是否在区间里面就行了
#include<set>
#include
#include
#include
#include
#include
#include
#include<string>
#include
#include
#include
#include
#include
#include
#define endl '\n'
#define max(a, b) (a > b ? a : b)
#define min(a, b) (a < b ? a : b)
#define mst(a) memset(a, 0, sizeof(a))
#define IOS ios::sync_with_stdio(false)
#define _test printf("~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~\n")
using namespace std;
typedef long long ll;
typedef pair<int, int> P;
const double pi = acos(-1.0);
const double eps = 1e-7;
const ll MOD = 1e9+7;
const int INF = 0x3f3f3f3f;
const int _NAN = -0x3f3f3f3f;
const int NIL = -1;
string str;
set<int> alp[26];
int main(void) {
    IOS;
    int n;
    while(cin >> str >> n) {
        int kase = 0;
        for (auto v : str)
            alp[v-'a'].insert(kase++);
        for (int i = 0, order; ii) {
            cin >> order;
            if (order == 1) {
                int pos;
                char ch[2];
                cin >> pos >> ch;
                --pos;
                alp[str[pos]-'a'].erase(pos);//先删除后插入,因为set不能有重复元素,所以这样避免插入重复元素时只删不增
                alp[ch[0]-'a'].insert(pos);
                str[pos] = ch[0];
            }
            else {
                int l, r;
                cin >> l >> r;
                int cnt = 0;
                for (int i = 0; i<26; ++i) {
                    auto it = alp[i].lower_bound(l-1);
                    if (alp[i].end() != it && *it < r)
                        ++cnt;
                }
                cout << cnt << endl;
                //cout << str << endl;

            }
        }
        for (int i = 0; i<26; ++i)
            alp[i].clear();
    }
    return 0;
}

 

 
  

你可能感兴趣的:(codeforces - 1234D Distinct Characters Queries)