萌新不会煎蛋分块求吊
查看原帖
萌新不会煎蛋分块求吊
631787
Watanabe楼主2023/11/5 10:02

只过了3,4,6,其它 wa 完了

#include<iostream>
#include<cmath>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<map>
#define re register

using namespace std;
const int N=1e5+100,M=1e3+100,B=1e3+10;
int n,Q,a[N];
struct Node{
	int ql,qr,qx,qv,id;
	char ch;
}q[N];
map <int,int> fac;

//
int m,bnum,belong[N],L[B],R[B],res[B][N];
struct node{
	int v,id;
}b[N<<1];
inline bool cmp0(node A,node B)
{
	return A.v<B.v;
}
inline void init()
{
	int tot=0;
	for(re int i=1;i<=n;++i) b[++tot].v=a[i],b[tot].id=tot;
	for(re int i=1;i<=Q;++i) b[++tot].v=q[i].qv,b[tot].id=tot;
	sort(b+1,b+tot+1,cmp0);
	int num=0;
	for(re int i=1;i<=tot;++i)
	{
		if(b[i].v!=b[i-1].v) ++num,fac[num]=b[i].v;
		if(b[i].id<=n) a[b[i].id]=num;
		else q[b[i].id-n].qv=num;
	}
	
	m=M;
	for(re int i=1;i<=n;++i) belong[i]=(i-1)/m+1;
	bnum=(n-1)/m+1;
	for(re int i=1;i<=bnum;++i) L[i]=(i-1)*m+1,R[i]=i*m;
	R[bnum]=n;
	for(re int i=1;i<=bnum;++i)
	{
		for(re int j=L[i];j<=R[i];++j)
		{
			res[i][a[j]]++;
		}
	}
}
inline void change(int x,int y) 
{
	res[belong[x]][a[x]]--;
	a[x]=y;
	res[belong[x]][a[x]]++;
}
inline int ask(int l,int r,int x)
{
	int bl=belong[l],br=belong[r],cnt=0;
	if(bl==br)
	{
		for(re int i=l;i<=r;++i) cnt+= (a[i]==x);
		return cnt;
	}
	for(re int i=l;i<=R[bl];++i) cnt+= (a[i]==x);
	for(re int i=L[br];i<=r;++i) cnt+= (a[i]==x);
	for(re int i=bl+1;i<=br-1;++i) cnt+=res[i][x];
	return cnt;
}

int qx,qv,ql,qr;
char ch;
signed main()
{
	cin>>n>>Q;
	for(re int i=1;i<=n;++i) cin>>a[i];
	for(re int i=1;i<=Q;++i)
	{
		cin>>q[i].ch;
		if(q[i].ch=='C') cin>>q[i].qx>>q[i].qv;
		else cin>>q[i].ql>>q[i].qr>>q[i].qv;
		q[i].id=i;
	}
	init();
	for(re int i=1;i<=Q;++i)
	{
		if(q[i].ch=='C') change(q[i].qx,q[i].qv);
		else cout<<fac[ask(q[i].ql,q[i].qr,q[i].qv)]<<"\n";
	}
	return 0;
}
2023/11/5 10:02
加载中...