//树状数组 OR set +lower_bound D. Distinct Characters Queries Codeforces Round #590 (Div. 3)

版权声明:本文为博主原创文章,遵循 CC 4.0 BY-SA 版权协议,转载请附上原文出处链接和本声明。
本文链接: https://blog.csdn.net/weixin_43880084/article/details/101906257

LINK-》Codeforces Round #590 (Div. 3)

D. Distinct Characters Queries
time limit per test2 seconds
memory limit per test256 megabytes
inputstandard input
outputstandard 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
inputCopy
abacaba
5
2 1 4
1 4 b
1 5 b
2 4 6
2 1 7
outputCopy
3
1
2
inputCopy
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
outputCopy
5
2
5
2
6

void assign(size_type n,const T& x = T());
赋n个值为x的元素到vector容器中,这个容器会清除掉vector容器中以前的内容。

  • 开26个树状数组(~~)
#include<bits/stdc++.h>
using namespace std;
string s;
int N;
struct tr
{
 vector<int>v;
 void reset(int n)
 {
 	v.assign(n+1,0);
 }	
 void update(int x,int vv)
 {
 	for(;x<=N;x+=(x&(-x)))
 	v[x]+=vv;
 }
 int sum(int x)
 {
 	int ans=0;
 	for(;x>0;x-=(x&(-x)))
 	ans+=v[x];
 	return ans;
 }
 int query(int l,int r)
 {
 	return sum(r)-sum(l-1);
 }
};
int main()
{
	ios::sync_with_stdio(false);cin.tie(0);
	tr h[30];int n;
	cin>>s;N=s.length();
	for(int i=0;i<26;i++) h[i].reset(N);
    for(int i=0;i<N;i++)
	{
		h[s[i]-'a'].update(i+1,1);
	}
	cin>>n;int x,y;char c;
	while(n--)
	{
		int oo;cin>>oo;
		if(oo==1)
		{
			cin>>x>>c;
			h[s[x-1]-'a'].update(x,-1);
			s[x-1]=c;
			h[s[x-1]-'a'].update(x,1);
		}else 
		{
			cin>>x>>y;int ans=0;
			for(int i=0;i<26;i++)
			{
				if(h[i].query(x,y)>0) ans++;
			}
			cout<<ans<<endl;
		}
	}
	
}
  • set+lower_bound(想法类似
#include<bits/stdc++.h>
using namespace std;
string s;
int main()
{
	ios::sync_with_stdio(false);cin.tie(0);
	set <int>h[30];
	int n;
	cin>>s;
    for(int i=0;i<s.length();i++)
	{
		h[s[i]-'a'].insert(i+1);
	}
	cin>>n;int x,y;char c;
	while(n--)
	{
		int oo;cin>>oo;
		if(oo==1)
		{
			cin>>x>>c;
			h[s[x-1]-'a'].erase(x);
			s[x-1]=c;
			h[s[x-1]-'a'].insert(x);
		}else 
		{
			cin>>x>>y;int ans=0;
			for(int i=0;i<26;i++)
			{
				auto it=h[i].lower_bound(x);
				if(it!=h[i].end()&&(*it)<=y ) ans++;
			}
			cout<<ans<<endl;
		}
	}
	
}

猜你喜欢

转载自blog.csdn.net/weixin_43880084/article/details/101906257