D. Distinct Characters Queries(set用法、auto,线段树做法)

题目

D. Distinct Characters Queries

time limit per test

2 seconds

memory limit per test

256 megabytes

input

standard input

output

standard output

You are given a string s

consisting of lowercase Latin letters and q

queries for this string.

Recall that the substring s[l;r]

of the string s is the string slsl+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 105

    lowercase Latin letters.

    The second line of the input contains one integer q

    (1≤q≤105

    ) — the number of queries.

    The next q

    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

    Copy

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

    Copy

    3
    1
    2
    
    Input

    Copy

    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

    Copy

    5
    2
    5
    2
    6
  • 法1:使用Set暴力求解

  • #include<bits/stdc++.h>
    using namespace std;
    char a[200005];
    int len;
    int q;
    set<int> st[30];
    int main()
    {
    	cin>>a+1;
    	len=strlen(a+1);
    	for(int i=1;i<=len;i++)
    	{
    		int cc=a[i]-'a';
    		st[cc].insert(i);//插入元素 
    	}
    	cin>>q;
    	while(q--)
    	{
    		int p;
    		cin>>p;
    		if(p==1)
    		{
    			int m;
    			char n[5];
    			scanf("%d",&m);
    			scanf("%s",n);
    			int cc=a[m]-'a';
    			st[cc].erase(m);//删除元素 
    			st[n[0]-'a'].insert(m);
    			a[m]=n[0];
    			
    		}
    		else 
    		{
    			int l,r;
    			scanf("%d%d",&l,&r);
    			int ans=0;
    			for(int i=0;i<26;i++)
    			{//auto ->::iterator 类型自动判断 
    				auto it=st[i].lower_bound(l);//二分查找 
    				if(it!=st[i].end()&&*it<=r) ans++;
    			}
    			cout<<ans<<endl;
    		}
    	}
    }

    法2:线段树维护区间状态

  • 线段树简单题

  • #include<bits/stdc++.h> 
    using namespace std;
    #define ls o*2
    #define rs o*2 +1
    #define mid (l+r)/2	
    //线段树预处理操作 
    char a[200005];
    int len,q;
    int tree[4*200005][30];//用过一个二维数组表示区间状态 
    int ans[30];
    void up(int o,int l,int r,int k,char v) 
    {
    	if(l==r)
    	{
    		for(int i=0;i<26;i++) tree[o][i]=0;//每次赋值都进行初始化 
    		tree[o][v-'a']=1;
    		return ;
    	}
    	if(k<=mid) up(ls,l,mid,k,v);
    	else up(rs,mid+1,r,k,v);
    	for(int i=0;i<26;i++) tree[o][i]=tree[ls][i]+tree[rs][i];
    	//子节点更新后更新父节点 
    }
    void qu(int o,int l,int r,int ql,int qr)
    {
    	if(ql<=l&&r<=qr)
    	{
    		for(int i=0;i<26;i++)
    		{
    			if(tree[o][i]) ans[i]=1;
    		}
    		return ;
    	}
    	if(ql<=mid) qu(ls,l,mid,ql,qr);//!!!!!!l,mid 
    	if(mid+1<=qr) qu(rs,mid+1,r,ql,qr);//!!!!mid+1,r 
    	return ;
    }
    int main()
    {
    	cin>>a+1;
    	len=strlen(a+1);
    	cin>>q;
    	for(int i=1;i<=len;i++)
    	{
    		up(1,1,len,i,a[i]);//建树 
    	}
    	while(q--)
    	{
    		int p;cin>>p;
    		if(p==1)
    		{
    			int n;
    			char m[3];
    			cin>>n>>m;
    			up(1,1,len,n,m[0]);//修改 
    		}
    		else 
    		{
    			int nn,mn;
    			cin>>nn>>mn;
    			for(int i=0;i<26;i++)
    			{
    				ans[i]=0;
    				
    			}
    			qu(1,1,len,nn,mn);//查询区间状态 
    			int aaa=0;
    			for(int i=0;i<26;i++)
    			{
    				if(ans[i]) aaa++;				
    			}
    			cout<<aaa<<endl;
    		}		
    	}
    }
发布了44 篇原创文章 · 获赞 6 · 访问量 1189

猜你喜欢

转载自blog.csdn.net/qq_43868883/article/details/102396305
今日推荐