0分求调
查看原帖
0分求调
694389
Cainiao_Chen楼主2025/1/18 11:18

蒟蒻样例过了,五个WA,剩下都T了(┬┬﹏┬┬)

注:蒟蒻用的C语言


#include<stdio.h>
int n,m,tr[5000005],arr[1000005],brr[1000005];
struct Tree{
	int l,r,num;
}crr[10000005];
int read()
{
	register int flag=1;
	register int n=0;
	register char ch=getchar();
	while((ch<'0')|(ch>'9'))
	{
		if(ch=='-')
			flag=-1;
		ch=getchar();
	}
	while((ch>='0')&(ch<='9'))
	{
		n=(n<<1)+(n<<3)+ch-'0';
		ch=getchar();
	}
	return flag*n;
}
int lowbit(int x)
{
	return x&(-x);
}
void ins(int pos,int val)
{
	while(pos<=n)
	{
		tr[pos]+=val;
		pos+=lowbit(pos);
	}
}
int query(int pos)
{
	int sum=0;
	while(pos)
	{
		sum+=tr[pos];
		pos-=lowbit(pos);
	}
	return sum;
}
int main()
{
	freopen(".in","r",stdin);
	freopen(".out","w",stdout);
	n=read();
	register int i=1,j;
	while(i<=n)
	{
		arr[i]=read();
		i++;
	}
	m=read();
	i=1;
	while(i<=m)
	{
		crr[i].l=read();
		crr[i].r=read();
		crr[i].num=i;
		i++;
	}
	int num=1;
	i=1;
	while(i<=m)
	{
		j=num;
		while(j<=crr[i].r)
		{
			if(brr[arr[j]])
				ins(brr[arr[j]],-1);
			ins(j,1);
			brr[arr[j]]=j;
			j++;
		}
		num=crr[i].r+1;
		printf("%d\n",query(crr[i].r)-query(crr[i].l-1));
		i++;
	}
	fclose(stdin);
	fclose(stdout);
	return 0;
}

谢谢dalao!

2025/1/18 11:18
加载中...