求助分块+gp_hash_table,TLE+RE
查看原帖
求助分块+gp_hash_table,TLE+RE
490314
小铭同学lym楼主2022/8/10 16:56

检查了好久,没找出问题。

#include<bits/stdc++.h>
#include<ext/pb_ds/assoc_container.hpp>
#include<ext/pb_ds/hash_policy.hpp>
#define inl inline 
#define rg register int 
using namespace std;
using namespace __gnu_pbds;
gp_hash_table<int,int>bz[1005];
const int N=1e6+5;
int n,m,fff,len,tot,L[N],R[N],id[N],a[N],ans;
char ch,cz;
inl int frad()
{
	fff=0,ch=getchar();
	while(ch<'0'||ch>'9')	ch=getchar();
	while(ch>='0'&&ch<='9')	fff=fff*10+ch-'0',ch=getchar();
	return fff;
}
void build()
{
	len=sqrt(n),tot=n/len;
	if(n%len)	tot++;
	for(rg i=1;i<=tot;++i)
		L[i]=(i-1)*len+1,R[i]=i*len;
	R[tot]=n;
	for(rg i=1;i<=n;++i)
		id[i]=(i-1)/len+1,bz[id[i]][a[i]]++;
}
void change(int wz,int bh)
{
	bz[id[wz]][a[wz]]--,a[wz]=bh,bz[id[wz]][a[wz]]++;
}
inl int qry(int l,int r,int k)
{
	ans=0;
	if(id[l]==id[r])
	{
		for(rg i=l;i<=r;++i)
			if(a[i]==k)
				ans++;
		return ans;
	}
	for(rg i=l;i<=R[id[l]];++i)
		if(a[i]==k)
			ans++;
	for(rg i=L[id[r]];i<=r;++i)
		if(a[i]==k)
			ans++;
	for(rg i=id[l]+1;i<id[r];++i)
		ans+=bz[i][k];
	return ans;
}
int main()
{
	n=frad(),m=frad();
	for(rg i=1;i<=n;++i)
		a[i]=frad();
	build();
	int l,r,k;
	while(m--)
	{
		scanf("%c",&cz),l=frad(),r=frad();
		if(cz=='C')
			change(l,r);
		else
		{
			k=frad();
			printf("%d\n",qry(l,r,k));
		}
	}
	return 0;
}

# 1 # 3 # 4 # 5 # 6 TLE

# 2 # 7 # 8 RE 太蒟蒻了。:(

2022/8/10 16:56
加载中...