【BZOJ2120】數顏色 循環

樹套樹!Oh!God!No!

一個萌萌噠的同班同學(PoPoQQQ)告訴我,這題其實是水暴力!

(PS:這麼水的題你爲什麼要刷?RE:因爲下午偷偷玩遊戲被老師發現結果假期被罰BZOJ十道題~~~況且我本來就是一隻大水怪~~~)

令人費解的是,此題必須用到離散化才能過。

(PS:你逗我,10^6直接做就好了啦!RE:我tm也不知道爲什麼。可能是某個神奇的常數~~~)

附:數顏色 C++代碼(未離散化,TLE):

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define N 10010
#define M 1000010
int n,m,a[N],mark[M],ans;
bool getopt()
{
	char ch;
	while(!isalpha(ch=getchar()));
	if(ch=='Q') return true;
	return false;
}
int main()
{
	cin>>n>>m;
	for(int i=1;i<=n;i++)
		scanf("%d",&a[i]);
	for(int x,y,i=1;i<=m;i++)
	{
		bool opt=getopt();
		scanf("%d%d",&x,&y);
		ans=0;
		if(opt)
		{
			for(int j=x;j<=y;j++)
				if(mark[a[j]]!=i)
					ans++,mark[a[j]]=i;
			printf("%d\n",ans);
		}
		else
			a[x]=y;
	}
}
數顏色 C++代碼實現(2216MS ACCEPTED):

#include <cstdio>
#include <cstring>
#include <iostream>
#include <algorithm>
using namespace std;
#define N 12010
#define M 1000010
int n,m,a[N],used[M],mark[N],ans,tot,now;
char ch[10];
int main()
{
	cin>>n>>m;
	for(int x,i=1;i<=n;i++)
	{
		scanf("%d",&x);
		if(!used[x])
			used[x]=++tot;
		a[i]=used[x];
	}
	for(int x,y,i=1;i<=m;i++)
	{
		scanf("%s%d%d",ch,&x,&y);
		if(ch[0]=='Q')
		{
			ans=0;++now;
			for(int j=x;j<=y;j++)
				if(mark[a[j]]!=now)
					ans++,mark[a[j]]=now;
			printf("%d\n",ans);
		}
		else
		{
			if(!used[y])
				used[y]=++tot;
			a[x]=used[y];
		}
	}
}


發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章