您的位置:首页 > 其它

POJ2777线段树(结构体指针实现)

2016-02-01 11:28 447 查看
#include<iostream>
#include<cstdio>
using namespace std;
struct node{
int s,t,color;
struct node *l,*r;
node(){
color=1;
l=r=NULL;
}
};
int ys[31];
struct node *h;
void create_tree(struct node *x){
int mid;
mid=(x->s+x->t)/2;
struct node *p,*q;
if(x->t-x->s>1){
p=new node;
q=new node;
p->s=x->s;
p->t=mid;
x->l=p;
q->s=mid;
q->t=x->t;
x->r=q;
create_tree(p);
create_tree(q);
}
}
void c(struct node *k,int x,int y,int z){
int mid=(k->s+k->t)/2;
if(k->s==x && k->t==y){
k->color=z;
return ;
}else if(y<=mid){
if(k->color!=-1){
k->l->color=k->color;
k->r->color=k->color;
}
k->color=-1;
c(k->l,x,y,z);
}else if(x>=mid){
if(k->color!=-1){
k->l->color=k->color;
k->r->color=k->color;
}
k->color=-1;
c(k->r,x,y,z);
}else{
if(k->color!=-1){
k->l->color=k->color;
k->r->color=k->color;
}
k->color=-1;
c(k->l,x,mid,z);
c(k->r,mid,y,z);
}
}
void cal(struct node *k,int x,int y){
int mid=(k->s+k->t)/2;
if(k->color!=-1){
ys[k->color]=1;
return;
}else if(y<=mid){
cal(k->l,x,y);
}else if(x>=mid){
cal(k->r,x,y);
}else{
cal(k->l,x,mid);
cal(k->r,mid,y);
}
}
int main(){
int i,j,k,m,n,ans;
char paint;
int x,y,z;
scanf("%d%d%d",&m,&k,&n);
h=new node;
h->s=0;h->t=m;
create_tree(h);
for(i=1;i<=n;i++){
char tmp;
tmp=getchar();
scanf("%c",&paint);
if(paint=='C'){
scanf("%d%d%d",&x,&y,&z);
if(x<y){
x--;
c(h,x,y,z);
}else{
y--;
c(h,y,x,z);
}
}else{
scanf("%d%d",&x,&y);
for(j=1;j<=k;j++)ys[j]=0;
ans=0;
if(x<y){
x--;
cal(h,x,y);
}else{
y--;
cal(h,y,x);
}
for(j=1;j<=k;j++)ans+=ys[j];
cout<<ans<<endl;
}
}
return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: