poj 2777 Count color

来源:互联网 发布:淘宝做工问题退货规则 编辑:程序博客网 时间:2024/05/02 04:53

线段树~~一个手误让我调了一晚上。。。


#include<stdio.h> 

#include<string.h>
#define MAX 1000001
int used[31];
int ans;

struct node
{
int l,r;
int color;
}f[MAX*6];

void build(int l,int r,int step)
{
f[step].l=l;
f[step].r=r;
f[step].color=1;//一开始一种颜色,一开始打成-1,坑爹了。。
if(l==r)
return;
int mid=(l+r)>>1;
build(l,mid,step<<1);
build(mid+1,r,step<<1|1);
}

void update(int l,int r,int temp,int step)
{
if(f[step].l==l&&f[step].r==r)
{
f[step].color=temp;
return;
}
//color!=-1,代表该区间颜色是固定的,则更新它的孩子节点。
if(f[step].color!=-1)
{
f[step<<1].color=f[step<<1|1].color=f[step].color;
   f[step].color=-1; 
}
int mid=(f[step].l+f[step].r)>>1;
if(r<=mid)
update(l,r,temp,step<<1);
else if(l>mid)
update(l,r,temp,step<<1|1);
else
{
update(l,mid,temp,step<<1);
update(mid+1,r,temp,step<<1|1);
}
}

void query(int l,int r,int step)
{
if(f[step].color!=-1)
{
if(!used[f[step].color])//且没出现过
{
used[f[step].color]=1;//标记为1
ans++;
}
return;
}
int mid=(f[step].l+f[step].r)>>1;
if(l>mid)
query(l,r,step<<1|1);
else if(r<=mid)
query(l,r,step<<1);
else
{
query(l,mid,step<<1);
query(mid+1,r,step<<1|1);
}
}

int main()
{
int l,t,o,a,b,c,e;
char op[2];
scanf("%d%d%d",&l,&t,&o);

build(1,l,1);
while(o--)
{
ans=0;
memset(used,0,sizeof(used));
scanf("%s",op);
if(op[0]=='C')
{
scanf("%d%d%d",&a,&b,&c);
if(a>b)//a可能比b大
{
e=a;
a=b;
b=e;
}
update(a,b,c,1);
}
else 
{
scanf("%d%d",&a,&b);
if(a>b)
{
e=a;
a=b;
b=e;
}
query(a,b,1);
printf("%d\n",ans);
}
}

return 0;
}
原创粉丝点击