文件详细信息

下载本文件

本文件的大小为 2221 字节。

#include<cstdio>
#include<cstring>
namespace IO{
	const int ARR_SIZE=1<<24;
	#define gc() ((IO::si!=IO::ti||(IO::ti=(IO::si=IO::input)+fread(IO::input,1,IO::ARR_SIZE,stdin))),IO::si!=IO::ti?*(IO::si++):EOF)
	#define pc(ch) ((IO::o.so!=IO::o.to||(fwrite(IO::o.output,1,IO::ARR_SIZE,stdout),IO::o.so=IO::o.output)),*(IO::o.so++)=ch)
	char input[ARR_SIZE],*si=input,*ti=input;
	struct Output_Stream{
		char output[ARR_SIZE],*so=output,*to=output+ARR_SIZE;
		~Output_Stream(){
			if(so==output)return;
			fwrite(output,1,so-output,stdout);
			so=output;
		}
	}o;
	template<typename T>
	void read(T&num){
		int ch=gc();
		num=0;
		while(ch<48||ch>57)ch=gc();
		while(ch>=48&&ch<=57)num=(num<<3)+(num<<1)+(ch^48),ch=gc();
	}
	void getstr(char*str){
		char ch=gc();
		while(ch=='\r'||ch=='\n'||ch==' '||ch=='\t')ch=gc();
		while(ch!='\r'&&ch!='\n'&&ch!=' '&&ch!='\t'&&ch!=EOF)*(str++)=ch,ch=gc();
		*str='\0';
	}
	template<typename T>
	void write(T a){
		static int ch[50],cnt;
		if(a==0)pc('0');
		cnt=0;
		while(a)ch[++cnt]=a%10|48,a/=10;
		while(cnt)pc(ch[cnt--]);
	}
	void putstr(const char*str){
		while(*str!='\0')pc(*(str++));
	}
}
using IO::read;
using IO::getstr;
using IO::write;
using IO::putstr;
template<typename T>
inline T min(const T a,const T b){
	return a<b?a:b;
}
typedef long long ll;
const int inf=0x3f3f3f3f;
struct Matrix{
	int a[3][3];
	void assign(int x){
		memset(a,0,sizeof(a));
		a[x][x]=1;
	}
};
Matrix operator*(const Matrix a,const Matrix b){
	Matrix ans;
	for(int i=0;i<3;i++)
		for(int j=i;j<3;j++){
			ans.a[i][j]=inf;
			for(int k=i;k<=j;k++)ans.a[i][j]=min(ans.a[i][j],a.a[i][k]+b.a[k][j]);
		}
	return ans;
}
const int maxn=100005;
int n,q,x;
char s[maxn],ch;
Matrix T[maxn<<2];
void build(int u,int l,int r){
	if(l==r){
		T[u].assign(s[l]-'a');
		return;
	}
	int m=(l+r)>>1;
	build(u<<1,l,m);
	build(u<<1|1,m+1,r);
	T[u]=T[u<<1]*T[u<<1|1];
}
void modify(int u,int l,int r){
	if(l==r){
		T[u].assign(ch-'a');
		return;
	}
	int m=(l+r)>>1;
	if(x<=m)modify(u<<1,l,m);
	else modify(u<<1|1,m+1,r);
	T[u]=T[u<<1]*T[u<<1|1];
}
int main(){
	read(n),read(q);
	getstr(s+1);
	build(1,1,n);
	for(int i=1;i<=q;i++){
		read(x),getstr(&ch);
		modify(1,1,n);
		write(T[1].a[0][2]),pc('\n');
	}
	return 0;
}