codeforces - 1234D Distinct Characters Queries

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

  题意是给你两种操作,一种是改变某个位置的字符,一个是计算某个区间[l,r]内的不同字符数量
因为数据比较大所以直接暴力肯定是不行的,我们可以把用set把每个字母的位置按照顺序记下来,对
于操作1,我们可以删除原来字符对应的位置然后再添加新字符对应的位置,对于操作2,我么只要判
断一下每个字符的位置是否在区间里面就行了
#include<set>
#include<map>
#include<stack>
#include<queue>
#include<cmath>
#include<cstdio>
#include<cctype>
#include<string>
#include<vector>
#include<climits>
#include<cstring>
#include<cstdlib>
#include<iostream>
#include<algorithm>
#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; i<n; ++i) {
            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;
}

 


posted @ 2020-02-05 21:09  shuitiangong  阅读(294)  评论(0编辑  收藏  举报