树套树!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];
}
}
}