线段树521

最近线段树狂写中 争取以后啥都不看就能写出来
#include<stdio.h>
#include
<string.h>

struct node
{
    node 
* pl, * pr;
    
int left, right;
    
int color;
    
int colCount;
    
int cl, cr;
}
mem[200020];

int memCount;
int Col[31];
int ColCnt;
int result;
node 
* newNode()
{
    node 
* pt= &mem[memCount++];
    pt
->color= pt->colCount= pt->cr= pt->cl= 1;
    
return pt;
}


node 
* buildTree(int l, int r)
{
    node 
* root=newNode();
    root
->left=l;
    root
->right=r;
    
if(r-l>1)
    
{
        
int mid=(l+r)/2;
        root
->pl=buildTree(l, mid);
        root
->pr=buildTree(mid, r);
    }

    
return root;
}


void color(node * root, int c)
{
    root
->colCount=1;
    root
->color = root->cl = root->cr = c;
}


void update(node * root, int l, int r, int c)
{
    
if(root->left==&& root->right==r)
    
{
        color(root, c);
        
return ;
    }

    
if(root->colCount==1)
    
{
        color(root
->pl,root->color);
        color(root
->pr,root->color);
    }

    
if(root->colCount==1 && root->color==c)
        
return;
    root
->colCount=2;
    
if(root->pl->right>l)
    
{
        
if(root->pl->right>=r)
            update(root
->pl,l,r,c);
        
else
        
{
            
int mid=(root->left+root->right)/2;
            update(root
->pl,l,mid,c);
            update(root
->pr,mid,r,c);
        }

    }

    
else
    
{
        update(root
->pr,l,r,c);
    }

    root
->cl=root->pl->cl;
    root
->cr=root->pr->cr;
}


void ques(node * root, int l, int r)
{
    
if(root->colCount==1)
    
{
        Col[ColCnt
++]=root->color;
        
return;
    }


    
if(root->right-root->left<=1)
        
return ;
    
if(root->pl->right>l)
    
{
        
if(root->pl->right>=r)
            ques(root
->pl,l,r);
        
else
        
{
            
int mid=(root->left+root->right)/2;
            ques(root
->pl,l,mid);
            ques(root
->pr,mid,r);
        }

    }

    
else
    
{
        ques(root
->pr,l,r);
    }

}


void clean()
{
    
int i, j;
    
for(i=1; i<=30; i++)
        
for(j=0; j<ColCnt; j++)
        
{
            
if(i==Col[j])
            
{
                result
++;
                
break;
            }

        }

}


int main()
{
    
int l, t, o;
    
while(scanf("%d%d%d"&l, &t, &o)==3)
    
{
        memCount
=0;
        node 
* root=buildTree(0, l);
        
int i;
        
char op;
        
int l, r, c;
        
for(i=0; i<o; i++)
        
{
            scanf(
"%s"&op);
            
if(op=='C')
            
{
                scanf(
"%d%d%d"&l, &r, &c);
                
if(l<=r)
                    update(root,l
-1,r,c);
                
else
                    update(root,r
-1,l,c);
            }

            
else
            
{
                ColCnt
=0;
                result
=0;
                memset(Col,
0,sizeof(Col));
                scanf(
"%d%d"&l, &r);
                
if(l<=r)
                    ques(root,l
-1, r);
                
else
                    ques(root,r
-1,l);
                clean();
                printf(
"%d\n", result);
            }

        }

    }

    
return 0;
}

原文地址:https://www.cnblogs.com/SQL/p/926666.html