BZOJ 2120/BZOJ 2453

时间:2023-03-08 17:31:56

分块傻逼题。

memset很慢的。。。而且其实也没有用。。。。

#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#define maxn 10050
#define maxm 1000500
using namespace std;
int n,m,col[maxn],pre[maxn],last[maxm],b[maxm],pos[maxn],len,num;
int x,y;
char s[];
void reset(int x)
{
int l=(x-)*len+,r=min(x*len,n);
for (int i=l;i<=r;i++)
b[i]=pre[i];
sort(b+l,b+r+);
}
void build()
{
if (n%len==) num=n/len;
else num=n/len+;
for (int i=;i<=num;i++)
reset(i);
}
void change()
{
scanf("%d%d",&x,&y);
for(int i=;i<=n;i++) last[col[i]]=;
col[x]=y;
for (int i=;i<=n;i++)
{
int regis=pre[i];
pre[i]=last[col[i]];last[col[i]]=i;
if (regis!=pre[i]) reset(pos[i]);
}
}
int find(int pos,int x)
{
int l=(pos-)*len+,r=min(pos*len,n);
int re=l;
while (l<=r)
{
int mid=(l+r)>>;
if (b[mid]<x) l=mid+;
else r=mid-;
}
return l-re;
}
void ask()
{
scanf("%d%d",&x,&y);
int ans=;
if (pos[x]==pos[y])
{
for (int i=x;i<=y;i++)
if (pre[i]<x) ans++;
}
else
{
for (int i=x;i<=pos[x]*len;i++)
if (pre[i]<x) ans++;
for (int i=(pos[y]-)*len+;i<=y;i++)
if (pre[i]<x) ans++;
for (int i=pos[x]+;i<=pos[y]-;i++)
ans+=find(i,x);
}
printf("%d\n",ans);
}
int main()
{
scanf("%d%d",&n,&m);
len=int(sqrt(n)+log(*n)/log());
for (int i=;i<=n;i++)
{
scanf("%d",&col[i]);
pre[i]=last[col[i]];
last[col[i]]=i;
pos[i]=(i-)/len+;
}
build();
for (int i=;i<=m;i++)
{
scanf("%s",s);
if (s[]=='Q') ask();
else change();
}
return ;
}