比赛 山东省选(随意做) 评测结果 ATTTTTTTTA
题目名称 HH的项链 最终得分 20
用户昵称 .Xmz 运行时间 0.000 s
代码语言 C++ 内存使用 0.00 MiB
提交时间 2010-04-12 16:14:52
显示代码纯文本
#include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <cstdlib>

using namespace  std;

const int maxn=50000+1;
const int maxm=200000+1;

int num[1000001],n,m;
int a[maxn];
struct question
{
	int l,r,id,ans;
}q[maxm];

void init()
{
	scanf("%d",&n);
	for (int i=1;i<=n;i++)
	{
		scanf("%d",&a[i]);
	}
	scanf("%d",&m);
	for (int i=1;i<=m;i++)
	{
		scanf("%d%d",&q[i].l,&q[i].r);
		q[i].id=i;
	}
}

int cmp(const void *a,const void *b)
{
	if (((question *)a)->l > ((question *)b)->l) return 1;
	else if (((question *)a)->l < ((question *)b)->l) return -1;
		else return ((question *)a)->r - ((question *)b)->r;
}

int cmp1(const void *a,const void *b)
{return ((question *)a)->id - ((question *)b)->id;}

void solve()
{
	qsort(q+1,m,sizeof(q[0]),cmp);
	int ans=0;
	for (int i=1;i<=m;i++)
	{
		int l=q[i].l,r=q[i].r;
		if (l!=q[i-1].l)
		{
			ans=0;
			memset(num,0,sizeof(num));
			for (int j=l;j<=r;j++)
			{
				if (num[a[j]]++ == 0) ans++;
			}
		}
		else
		{
			for (int j=q[i-1].r+1;j<=r;j++)
			{
				if (num[a[j]]++ == 0) ans++;
			}
		}
		q[i].ans=ans;
	}
	qsort(q+1,m,sizeof(q[0]),cmp1);
	for (int i=1;i<=m;i++)
	{
		printf("%d\n",q[i].ans);
	}
}

int main()
{
	freopen("diff.in","r",stdin);
	freopen("diff.out","w",stdout);
	init();
	solve();
	return 0;
}