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; }
相关文章推荐
- VOIP开源资源记录
- nsurlsession 的post 方法1
- MySQL中一致性非锁定读
- 基于JavaScript实现动态添加删除表格的行
- nsurlsession 的get用法
- 金融信息交换协议(FIX)5.0 FIXT1.1(3)
- 【Xamain 跨平台机制原理剖析】
- 最全第三方库
- C# WebSocket 服务端示例代码 + HTML5客户端示例代码
- All the tree
- jenkins Hudson fails build with UnsupportedOperationException due to attempted modification of Simpl
- 【转】linux下杀死进程(kill)的N种方法
- java常用工具类之DES和Base64加密解密类
- 循环的几种表示
- 模式匹配KMP
- JQuery上传插件Uploadify使用详解
- highchart.js的使用
- 3. Longest Substring Without Repeating Characters
- zcmu1048
- bash脚本编程之二 条件判断