UPC-Simple String Queries(二维树状数组)

简介: UPC-Simple String Queries(二维树状数组)

你的偶像光芒万丈,你不能一身戾气。

Simple String Queries

You are given a string S of length N consisting of lowercase English letters.

Process Q queries of the following two types:

Type 1: change the iq-th character of S to cq. (Do nothing if the iq-th character is already cq.)

Type 2: answer the number of different characters occurring in the substring of S between the lq-th and rq-th characters (inclusive).


Constraints

·N, Q, iq, lq, and rq are integers.

·S is a string consisting of lowercase English letters.

·cq is a lowercase English letter.

·1≤N≤500000

·1≤Q≤20000

·|S|=N

·1≤iq≤N

·1≤lq≤rq≤N

·There is at least one query of type 2 in each testcase.

输入

Input is given from Standard Input in the following format:

N

S

Q

Query1

QueryQ

Here,Queryi in the 4-th through (Q+3)-th lines is one of the following:

1 iq cq

2 lq rq

输出

For each query of type 2, print a line containing the answer.


样例输入 Copy

7

abcdbbd

6

2 3 6

1 5 z

2 1 1

1 4 a

1 7 d

2 1 7

样例输出 Copy

3

1

5

提示

In the first query, cdbb contains three kinds of letters: b , c , and d, so we print 3.

In the second query, S is modified to abcdzbd.

In the third query, a contains one kind of letter: a, so we print 1.

In the fourth query, S is modified to abcazbd.

In the fifth query, S does not change and is still abcazbd.

In the sixth query, abcazbd contains five kinds of letters: a, b, c, d, and z, so we print 5.


题意: 给你一个字符串和若干次操作,操作1是更改字符串里的一个字母,操作2是查询该区间里不同字母的数量。要求输出操作2的查询结果。

思路: 裸的树状数组的单点修改和区间查询,我们用一个二维数组记录每个字母在此之前出现的次数,然后按照树状数组的思路写就可以了。

本题要注意:数组要开到5e5+10左右。每次更改时要把之前的字母删去。每次更改时要修改字符串里的字母。

代码:

#include<iostream>
using namespace std;
int n,m;
int tr[26][500010];
int lowbit(int x){
    return x&-x;
}
void add(int i,int x,int y){
    for(int j=i;j<=n;j+=lowbit(j))
        tr[x][j]+=y;
}
int ask(int x,int y){
    int res=0;
        for(int j=x;j;j-=lowbit(j))
            res+=tr[y][j];
    return res;
}
char s[500010];
int main(){
    cin>>n;
    cin>>s+1;
    for(int i=1;i<=n;i++){
        int x=s[i]-'a';
        add(i,x,1);
    }
    cin>>m;
    while(m--){
        int a,b,c;char ch;
        cin>>a;
        if(a==1){
            cin>>b>>ch;
            c=ch-'a';
            add(b,s[b]-'a',-1);
            add(b,c,1);
            s[b]=ch;
        }
        else{
            int res=0;
            cin>>b>>c;
            for(int i=0;i<26;i++){
                int sum=ask(c,i)-ask(b-1,i);
                if(sum>0) res++;
            }
            cout<<res<<endl;
        }
    }
    return 0;
}
目录
相关文章
|
8月前
|
JavaScript
[Vue warn]_ Avoid using non-primitive value as key, use string_number value instea
[Vue warn]_ Avoid using non-primitive value as key, use string_number value instea
100 1
|
人工智能 自然语言处理 语音技术
Invalid prop: type check failed for prop “index“. Expected String with value “5“问题解决
Invalid prop: type check failed for prop “index“. Expected String with value “5“问题解决
160 0
|
Java
Can&#39;t convert boolean to string automatically, because the &quot;boolean_format&quot; setting was &quot;true,false&quot;
五月 11, 2017 5:06:50 下午 freemarker.log._JULLoggerFactory$JULLogger error 严重: Error executing FreeMarker template FreeMarker template error: Can't con...
2796 0
|
Android开发 数据格式 JSON
android报错 Expected BEGIN_OBJECT but was STRING at line 1 column 39 path $
      我在使用retrofit和Gson配合时,出现了这个问题,疑惑中乱七八糟瞎搞了一个下午没有解决。期间怀疑Gson解析不能使用泛型(因为我的解析使用了泛型),后来又觉得可能是我的关键字正好是解析器的某个关键字导致的异常,也打算过自定义Gson的解析过程,其实这些都不是。         第二天才搞明白,真正的问题是我的数据结构有问题,或者说我的解析出现了问题。  
4373 0
TypeError: sequence item 0: expected string, int found
TypeError: sequence item 0: expected string, int found
|
XML 数据格式
Do not concatenate text displayed with setText,use resource string with placeholders.
Do not concatenate text displayed with setText,use resource string with placeholders.
279 0
Do not concatenate text displayed with setText,use resource string with placeholders.
|
关系型数据库 MySQL 数据库
当你遇到Error: ER_TRUNCATED_WRONG_VALUE_FOR_FIELD: Incorrect string value:
当你遇到Error: ER_TRUNCATED_WRONG_VALUE_FOR_FIELD: Incorrect string value: