带修莫队

#include<bits/stdc++.h>
using namespace std; 
typedef long long ll;

int n,m,l,r,sz,a[350005];
int flag[1000005]={0},Ans[50005]={0},vis[50005]={0},last[50005],ans,now,tot=0,top=0;

struct shu
{
	int l,r,id,pre;
	friend bool operator <(shu a,shu b)
	{
		if(a.l/sz!=b.l/sz) return a.l/sz<b.l/sz;
		if(a.r/sz!=b.r/sz) return a.r/sz<b.r/sz;
		return a.pre<b.pre;
	}
}q[50005];
struct
{
	int o,x,y;
}g[50005];

void up(int x)
{
    if(vis[x])
    {
        if(--flag[a[x]] == 0) ans--;
    }
    else
    {
        if(++flag[a[x]] == 1)ans++;
    }
    vis[x]^=1;
}

void re(int x, int v)
{
    if(vis[x])
    {
        up(x);a[x]=v;up(x);
    }
    else a[x]=v;
}

int main()
{ 
	//freopen("in.txt","r",stdin);
	//freopen("out.txt","w",stdout);
   	scanf("%d%d",&n,&m);
   	sz=pow(n,2.0/3.0);
   	for(int i=1;i<=n;i++) 
	{
	   scanf("%d",&a[i]);
	   last[i]=a[i];
	}
   	int x,y;
   	char ch[5];
   	for(int i=1;i<=m;i++)
   	{
   		scanf("%s%d%d",ch,&x,&y);
   		x++; 
   		if(ch[0]=='Q') q[++tot].l=x,q[tot].r=y,q[tot].id=tot,q[tot].pre=top;
        else g[++top].x=x,g[top].y=y,g[top].o=last[x],last[x]=y;
	}
	sort(q+1,q+1+tot);
	l=1,r=0,ans=0,now=0;
	for(int i=1;i<=tot;i++)
	{
		while(now<q[i].pre) ++now,re(g[now].x,g[now].y);
        while(now>q[i].pre) re(g[now].x,g[now].o),--now;
		while(l<q[i].l) up(l++);
		while(l>q[i].l) up(--l);
		while(r>q[i].r) up(r--);
		while(r<q[i].r) up(++r);
		Ans[q[i].id]=ans;
	}
	for(int i=1;i<=tot;i++)
	printf("%d\n",Ans[i]);
   	return 0;
}
  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值