【线段树】Codeforces Round #590 (Div. 3) D. Distinct Characters Queries

Codeforces Round #590 (Div. 3) D. Distinct Characters Queries

题意:

  • 有一个字符串,对这个字符串有两种操作
  • 1:修改某个位置的字符为其他字符
  • 2:查询 [ l, r ] 的不同字符的数目

思路:

  • 一共有26个字符,所以我们用26位的二进制来表示字符的存在情况:a ((1 >> 0) & 1), b(1 >> 1 & 1)…… = 1即存在,= 0即不存在
  • 依此来建线段树,父亲结点的值是左右儿子的值的按位或
#include <iostream>
#include <cstdio>
#include <cmath>
#include <string>
#include <cstring>
#include <algorithm>
#include <limits>
#include <vector>
#include <stack>
#include <queue>
#include <set>
#include <map>
#define MID (l + r) >> 1
#define lsn rt << 1
#define rsn rt << 1 | 1
#define Lson lsn, l, mid
#define Rson rsn, mid + 1, r
#define QL Lson, ql, qr
#define QR Rson, ql, qr
using namespace std;
typedef long long ll;
const int maxN = 1e5 + 7;
const int maxM = 2e6;

int q;
char s[maxN];
int tree[maxN << 2];
void pushup(int rt)
{
    tree[rt] = tree[lsn] | tree[rsn];
    return;
}
void build_tree(int rt, int l, int r)
{
    if(l == r) { tree[rt] = 1 << s[l - 1] - 'a'; return; }
    int mid = MID;
    build_tree(Lson);
    build_tree(Rson);
    pushup(rt);
}
void update_point(int rt, int l, int r, int pos, int val)
{
    if(l == r) { tree[rt] = val; return ;}
    int mid = MID;
    if(pos <= mid) update_point(Lson, pos, val);
    else update_point(Rson, pos, val);
    pushup(rt);
}
int query(int rt, int l, int r, int ql, int qr)
{
    if(l >= ql && r <= qr) return tree[rt];
    int mid = MID;
    if(qr <= mid) return query(QL);
    else if(ql > mid) return query(QR);
    else return query(QL) | query(QR);
}

int main()
{
    scanf("%s", s);
    int n = strlen(s);
    scanf("%d", &q);
    build_tree(1, 1, n);
    while(q -- )
    {
        int a; scanf("%d", &a);
        if(a == 1)
        {
            int b; char c;
            scanf("%d", &b);
            getchar();
            scanf("%c", &c);
            update_point(1, 1, n, b, 1 << c - 'a');
        }
        else
        {
            int b, c; scanf("%d%d",&b, &c);
            int ans = query(1, 1, n, b, c);
            int res = 0;
            for(int i = 0; i < 26; i ++ )
            {
                if((ans >> i) & 1)
                    res ++;
            }
            printf("%d\n", res);
        }
    }
    return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值