线段树区间更新 Count Color

本文介绍了一种基于段式更新与查询操作的高效算法实现,该算法利用二进制位表示不同颜色的状态,通过构建树形数据结构来优化大量更新和查询操作的时间复杂度。
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

这个的关键是用二进制存 每位的0,1表示这位有没有被涂过色 
每次输入的时候 就是1<<(add-1)  
所以更新到父节点的话 就要用 |
最后统计颜色 用与运算 都是1才为1 

#include <iostream>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=100005;
int sum[maxn*4];
struct node
{
    int l,r,val,mark;
}tree[maxn*4];
void build(int i,int l,int r)
{
    tree[i].mark=0;
    tree[i].val=1;
    tree[i].l=l,tree[i].r=r;
    //cout<<l<<" "<<r<<endl;
    if(l==r)return;


    int m=(l+r)/2;
    build(i*2,l,m);
    build(i*2+1,m+1,r);
}
void pushdown(int i)
{
     if(tree[i].mark!=0)
    {
    tree[i*2].mark=tree[i].mark;
    tree[i*2+1].mark=tree[i].mark;


    tree[i*2+1].val=tree[i].val;
    tree[i*2].val=tree[i].val;


    tree[i].mark=0;
    }
}
void update(int i,int l,int r,int add)
{


    //cout<<tree[i].l<<" "<<tree[i].r<<" "<<tree[i].mark<<" "<<tree[i].val<<endl;
    if(tree[i].l==l&&r==tree[i].r)    //要更新的区间比i所含的区间大 全部包含
    {
        tree[i].mark=1;
        tree[i].val=1<<(add-1);     //用不同位上的1表示
        //cout<<tree[i].l<<" "<<tree[i].r<<" "<<tree[i].mark<<" "<<tree[i].val<<endl;
        return;
    }
    if(tree[i].mark!=0)pushdown(i);


    int m=(tree[i].l+tree[i].r)/2;
    if(r<=m)update(i*2,l,r,add);
    else if(l>m)update(i*2+1,l,r,add);
    else
    {
        update(i*2,l,m,add);
        update(i*2+1,m+1,r,add);
    }
    tree[i].val=(tree[i*2].val)|(tree[i*2+1].val);
    //cout<<"CC"<<tree[i].l<<" "<<tree[i].r<<" "<<tree[i].mark<<" "<<tree[i].val<<endl;
    if(tree[i*2].mark&&tree[i*2+1].mark&&tree[i*2].val==tree[i*2+1].val)
    tree[i].mark=1;     //如果左右子树都被标记且颜色相同 就把父节点标记 这样查询的时候可以省时


}
int query(int i,int l,int r)
{
    //cout<<tree[i].l<<" "<<tree[i].r<<" "<<tree[i].mark<<" "<<tree[i].val<<endl;
    if(tree[i].l>=l&&tree[i].r<=r)
    return tree[i].val;
    pushdown(i);
    int ans=0;
    int m=(tree[i].l+tree[i].r)/2;
    if(l<=m)ans|=query(i*2,l,r);
    if(r>m)ans|=query(i*2+1,l,r);
    return ans;


}
int main()
{
    int l,t,o;
    while(scanf("%d%d%d",&l,&t,&o)!=-1)
    {
        char op;
        int a,b,c;
        memset(tree,0,sizeof(tree));
        build(1,1,l);
        while(o--)
        {
            scanf(" %c",&op);
            //cout<<op<<endl;
            if(op=='C')
            {
                scanf("%d%d%d",&a,&b,&c);
                if(a>b)swap(a,b);
                update(1,a,b,c);


            }
            else
            {
                scanf("%d%d",&a,&b);
                if(a>b)swap(a,b);
                int tmp=query(1,a,b);
                int count=0;
                for(int i=0;i<t;i++)
                {
                    if(tmp&(1<<i))count++;


                }
                cout<<count<<endl;
            }
        }


    }
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值