为什么60分老RE?(快吐了,在线求,急!)
查看原帖
为什么60分老RE?(快吐了,在线求,急!)
285069
LSG_waterlyf楼主2020/7/29 10:33
#include<bits/stdc++.h>
using namespace std;
#define N 2000010
int n,m,cnt[N],pos[N],t,a[N],l[N],r[N],ans,Ans[N],L,R,cntq,cntc,T;
struct question
{
	int pl,pr,l,r,id,t;
	bool operator <(const question &b)const
	{
		if(pl!=b.pl) return pl<b.pl;
		if(pr!=b.pr) return pr<b.pr;
		return t<b.t; 
	}
}q[N];
struct change
{
	int x,col;
}c[N];
void add(int x)
{
	if(!cnt[a[x]]) ans++;
	cnt[a[x]]++;
}
void del(int x)
{
	cnt[a[x]]--;
	if(!cnt[a[x]]) ans--; 
}
void update(int L,int R,int T)
{
	int x=c[T].x,col=c[T].col;
	if(x>=L&&x<=R) 
	{
		if(!cnt[col]) ans++;
		cnt[col]++;
		cnt[a[x]]--;
		if(!cnt[a[x]]) ans--;
	}
	swap(a[x],c[T].col);
}
int main()
{
	cin>>n>>m;
	for(int i=1;i<=n;i++) scanf("%d",&a[i]);
	t=pow(n,1.0/3);
	for(int i=1;i<=t;i++) l[i]=(i-1)*pow(n,2.0/3)+1,r[i]=i*pow(n,2.0/3);
	if(r[t]<n) t++,l[t]=r[t-1]+1,r[t]=n;
	for(int k=1;k<=t;k++)
	  for(int i=l[k];i<=r[k];i++) pos[i]=k;
	for(int i=1,L,R;i<=m;i++) 
	{
		char ch=getchar();
		ch=getchar();
		scanf("%d%d",&L,&R);
		if(ch=='Q') q[++cntq]=(question){pos[L],pos[R],L,R,cntq,cntc};
		else c[++cntc]=(change){L,R};
	}
	sort(q+1,q+1+cntq);
	L=1,R=0,T=0;
	for(int i=1;i<=cntq;i++)
	{
		int x=q[i].l,y=q[i].r,t=q[i].t;
		while(L<x) del(L),L++;
		while(L>x) L--,add(L);
		while(R<y) R++,add(R);
		while(R>y) del(R),R--;
		while(T<t) T++,update(L,R,T);
		while(T>t) update(L,R,T),T--;
		Ans[q[i].id]=ans;
	}
	for(int i=1;i<=cntq;i++) printf("%d\n",Ans[i]);
	return 0;
}

评判记录

2020/7/29 10:33
加载中...