• 线段树染色


    Language:
    Count Color
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 53531   Accepted: 16114

    Description

    Chosen Problem Solving and Program design as an optional course, you are required to solve all kinds of problems. Here, we get a new problem. 

    There is a very long board with length L centimeter, L is a positive integer, so we can evenly divide the board into L segments, and they are labeled by 1, 2, ... L from left to right, each is 1 centimeter long. Now we have to color the board - one segment with only one color. We can do following two operations on the board: 

    1. "C A B C" Color the board from segment A to segment B with color C. 
    2. "P A B" Output the number of different colors painted between segment A and segment B (including). 

    In our daily life, we have very few words to describe a color (red, green, blue, yellow…), so you may assume that the total number of different colors T is very small. To make it simple, we express the names of colors as color 1, color 2, ... color T. At the beginning, the board was painted in color 1. Now the rest of problem is left to your. 

    Input

    First line of input contains L (1 <= L <= 100000), T (1 <= T <= 30) and O (1 <= O <= 100000). Here O denotes the number of operations. Following O lines, each contains "C A B C" or "P A B" (here A, B, C are integers, and A may be larger than B) as an operation defined previously.

    Output

    Ouput results of the output operation in order, each line contains a number.

    Sample Input

    2 2 4
    C 1 1 2
    P 1 2
    C 2 2 2
    P 1 2
    

    Sample Output

    2
    1
    

    Source

    解题思路:
           利用sum[33]来记录颜色的有无,有为1,无为0;
           用树的根节点来记录一段的颜色;
    #include<cstdio>
    #include<cstring>
    using namespace std;
    
    const int maxn=100000+2;
    
    struct segtree
    {
    	int l,r,co;
    };
    segtree tree[maxn*3+1];
    
    int sum[40];//记录颜色是否存在;
    int L,T,O,a,b,c,d,e;
    char ch;
    
    void build(int n,int l,int r)
    {
    	tree[n].l=l;
    	tree[n].r=r;
    	tree[n].co=1;//初始的颜色都是1;
    	if(l==r)
    	{
    		return;
    	}
    	int mid=(tree[n].l+tree[n].r)/2;
    	build(n*2,l,mid);
    	build(n*2+1,mid+1,r);//用递归建树;
    }
    
    void updata(int n,int l,int r,int co)
    {
    	if(tree[n].l==l&&tree[n].r==r)
    	{
    		tree[n].co=co;
    		return;
    	}
    	if(tree[n].co!=-1)
    	{
    		tree[n*2].co=tree[n*2+1].co=tree[n].co;
    		tree[n].co=-1;
    	}
    	int mid=(tree[n].l+tree[n].r)/2;
    	if(r<=mid)
    	{
    		updata(n*2,l,r,co);
    	}
    	else if(l>mid)
    	{
    		updata(n*2+1,l,r,co);
    	}
    	else
    	{
    		updata(n*2,l,mid,co);
    		updata(n*2+1,mid+1,r,co);
    	}
    }
    
    void query(int n,int l,int r)
    {
    	if(tree[n].co!=-1)
    	{
    		sum[tree[n].co]=1;
    		return; 
    	}
    	int mid=(tree[n].l+tree[n].r)/2;
    	if(r<=mid)
    	{
    		query(n*2,l,r);
    	}
    	else if(l>mid)
    	{
    		query(n*2+1,l,r);
    	}
    	else
    	{
    		query(n*2,l,mid);
    		query(n*2+1,mid+1,r);
    	}
    }
    
    int main()
    {
            scanf("%d%d%d",&L,&T,&O);
    		build(1,1,L);
    		
    	//	printf("%d
    ",O);
    		while(O--)
    		{
    			getchar();
    			scanf("%c",&ch);
    		    if(ch=='C')
    		    {
    			    scanf("%d%d%d",&a,&b,&c);
    			    updata(1,a,b,c);
    			    //printf("!!"); 
    		    }
    		    else if(ch=='P')
    		    {
    		    	scanf("%d%d",&d,&e);
    	    		memset(sum,0,sizeof(sum));
    	     		query(1,d,e);
    		    	int ans=0;
    		    	for(int i=1;i<=33;i++)
    		        {
    		 	    	if(sum[i]!=0)
    			    	{
    				    	ans++;
    			    	}
    		    	}
    	     		printf("%d
    ",ans);
    	    	}
        	}
    	return 0;
    }
    

      

    #include<cstdio>
    #include<cstring>
    using namespace std;
    #define maxn 100005
    //#define maxn 100000+5 //define的作用是用一些符号代替另一些符号,只是死的代替,不会加其他的东西;
    //maxn*3 等效于 100000+5*3 ,而不是(100000+5)*3; 
    struct segtree{
        int l,r,co;
    }tree[maxn*3+1];
    int sum[33];//记录颜色是否存在 ;
    int L,T,O,a,b,c,d,e;
    char ch;
    void build(int n,int l,int r){
        tree[n].l=l;
        tree[n].r=r;
        tree[n].co=1;//At the beginning, the board was painted in color 1 ;
        if(l==r){
            return ;//此时树无法再分支; 
        }else{
            int mid=(tree[n].l+tree[n].r)/2;//找出中心点,分成两半;
            build(n*2,l,mid);//再两边继续分支; 
            build(n*2+1,mid+1,r); 
        }
    }
    void updata(int n,int l,int r,int co){
        if(tree[n].l==l&&tree[n].r==r){
            tree[n].co=co;//如果刚好是该区间就直接涂,涂完就表示结束;
            return;
        }else{//不然的话,就要去找子区间; 
            if(tree[n].co!=-1){//表示该根节点有颜色,所以它的子节点的颜色与它相同; 
                tree[n*2].co=tree[n*2+1].co=tree[n].co;
                tree[n].co=-1;//记录一下,表示它的颜色也已分配到枝上; 
            }
            int mid=(tree[n].l+tree[n].r)/2;
            if(r<=mid){//在整个区间的左边;
                updata(n*2,l,r,co); 
            }else if(l>mid){//在整个区间的右边; 
                updata(n*2+1,l,r,co);
            }else{//在整个区间的中间; 
                updata(n*2,l,mid,co);
                updata(n*2+1,mid+1,r,co);
            }
        }
    }
    void query(int n,int l,int r){
        if(tree[n].co!=-1){
            sum[tree[n].co]=1;
            return;
        }else{
            int mid=(tree[n].l+tree[n].r)/2;
            if(l>mid){
                query(n*2+1,l,r);
            }else if(r<=mid){
                query(n*2,l,r);
            }else{
                query(n*2,l,mid);
                query(n*2+1,mid+1,r);
            }
        }
    }
    int main(){
        scanf("%d%d%d",&L,&T,&O);
        build(1,1,L);
        while(O--){
            getchar();//吸收换行符,换行符会被当做字符输入; 
            scanf("%c",&ch);
            if(ch=='C'){
                scanf("%d%d%d",&a,&b,&c);
                updata(1,a,b,c);
            }else if(ch=='P'){
                scanf("%d%d",&d,&e);
                memset(sum,0,sizeof(sum));
                query(1,d,e);
                int ans=0;
                for(int i=1;i<33;i++){
                    ans+=sum[i];
                }
                printf("%d
    ",ans);
            } 
        }
        return 0;
    } 
  • 相关阅读:
    Ubuntu 14.04 LTS 火狐浏览器中,鼠标选择文字被删除的解决办法
    Android 冷启动时间优化
    Word 2010 小技巧篇
    Word 2010 制作文档结构之图标自动编号设置
    Word 2010 制作文档结构之章节自动编号
    Word 2010 制作文档结构之页码从正文开始设置
    字幕通-字幕翻译工具
    图灵社区 和 大家网
    Python GUI编程之WxPython
    VLC媒体视频播放器 v3.0.2官方版
  • 原文地址:https://www.cnblogs.com/qqshiacm/p/10502740.html
Copyright © 2020-2023  润新知