蒟蒻求助!!样例二都过不了
查看原帖
蒟蒻求助!!样例二都过不了
437643
huangboyu楼主2022/7/10 21:51
#include<bits/stdc++.h>
using namespace std;
const int N=5e5+5;
#define ll long long
int n,m;
struct Tree{
	int l,r;
	ll i,o,io,oi,ioi;
	void clr(){i=io=oi=o=ioi=0;}
}t[4*N];
int a[N];/*
void clear(int now){t[now].i =t[now].io =t[now].ioi =t[now].o =t[now].oi =0;}
void clr(Tree &a){a.i=a.o=a.io=a.oi=a.ioi=0;}*/
Tree add(Tree a,Tree b)
{
	Tree c;c.clr();
	c.i =a.i +b.i ;c.o =a.o +b.o ;
	c.io =a.io +b.io +a.i *b.o ;
	c.oi =a.oi +b.oi +a.o *b.i ;
	c.ioi =a.ioi +b.ioi +a.io *b.i +a.i *b.oi ;
	return c;
}
void build(int now,int l,int r)
{
	t[now].clr() ;
	t[now].l =l;t[now].r =r;
	if(l==r)
	{
		if(a[l]==1)t[now].i =1;
		else t[now].o =1;
		return;
	}
	int mid=(l+r)>>1;
	build(now<<1,l,mid);
	build(now<<1|1,mid+1,r);
	t[now]=add(t[now<<1],t[now<<1|1]);
	return;
}
void change(int now,int x,int c)
{
	if(t[now].l==t[now].r)
	{
		//t[now].clr() ;
		if(c==1) t[now].i =1,t[now].o =0;
		else t[now].o =1,t[now].i =0;
		return;
	}
	int mid=(t[now].l+t[now].r)>>1;
	if(x<=mid)change(now<<1,x,c);
	else change(now<<1|1,x,c);
	t[now]=add(t[now<<1],t[now<<1|1]);
	return; 
}
Tree ask(int now,int l,int r)
{
	if(l<=t[now].l&&t[now].r<=r)return t[now];
	int mid=(t[now].l +t[now].r)>>1;
	Tree ans;ans.clr();
	if(mid>=l)ans=add(ans,ask(now<<1,l,r));
	if(mid+1<=r)ans=add(ans,ask(now<<1|1,l,r));
	return ans;
}
int main()
{
	scanf("%d%d",&n,&m);
	for(int i=1;i<=n;i++)
	{
		char c;cin>>c;
		if(c=='I')a[i]=1;
		else a[i]=0;
	}
	build(1,1,n);
	printf("%lld\n",ask(1,1,7).ioi );
	while(m--)
	{
		int op;
		scanf("%d",&op);
		if(op==1)
		{
			int x,c;char cc;
			scanf("%d",&x);
			cin>>cc;
			if(cc=='I')c=1;
			else c=0;
			change(1,x,c);
		}
		if(op==2)
		{
			int l,r;
			scanf("%d%d",&l,&r);
			printf("%lld\n",ask(1,l,r).ioi );
		}
	}
	return 0;
  }  
2022/7/10 21:51
加载中...