POJ 2777 Count color 树状数组


#include <iostream>

#include<cstdio>
#include<algorithm>


using namespace std;
const int MAX=100003;
struct node
{
    int col;
    bool book;
    int l,r;
}tre[MAX*4];
int sum;
void build(int s,int e,int rot)
{
    tre[rot].l=s;
    tre[rot].r=e;
    tre[rot].col=1;
    tre[rot].book=1;
    if(s==e)
        return;


    int mid=(s+e)/2;
    build(s,mid,rot*2);
    build(mid+1,e,rot*2+1);
}
void pushdown(int rt)
{
    tre[rt*2].col=tre[rt].col;
    tre[rt*2+1].col=tre[rt].col;
    tre[rt*2].book=1;
    tre[rt*2+1].book=1;
    tre[rt].book=0;
}
void update(int s,int e,int c,int rot)
{
    if(s<=tre[rot].l&&e>=tre[rot].r)
    {
        tre[rot].book=1;
        tre[rot].col=c;         
        return;
    }
    if(tre[rot].col==c)   //如果此时本节点区间内为一个颜色就且等于待更新颜色
        return;           //直接返回了,不用更新
    if(tre[rot].book)     //如果此时的覆盖成立,则向下更新子节点
        pushdown(rot);


    int mid=(tre[rot].l+tre[rot].r)/2;
    if(mid>=e)
        update(s,e,c,rot*2);
    else if(s>mid)
        update(s,e,c,rot*2+1);
    else
    {
        update(s,mid,c,rot*2);
        update(mid+1,e,c,rot*2+1);
    }
    tre[rot].col=tre[rot*2].col|tre[rot*2+1].col;
}
void quiry(int s,int e,int rt)
{
    if(s<=tre[rt].l&&e>=tre[rt].r)
    {
        sum|=tre[rt].col;
        return;
    }
    if(tre[rt].book)
    {
        sum|=tre[rt].col;
        return;
    }


    int mid=(tre[rt].l+tre[rt].r)/2;    //注意此处是根据根节点的mid分区间检查
    if(mid>=e)
        quiry(s,e,rt*2);
    else if(s>mid)
        quiry(s,e,rt*2+1);
    else                           //注意一旦分区间查,要把区间分开去查下去
    {
        quiry(s,mid,rt*2);
        quiry(mid+1,e,rt*2+1);
    }
}
int solve(int sum)
{
    int s=0;
    while(sum)
    {
        if(sum&1)
            s++;
        sum>>=1;
    }
    return s;
}
int main()
{
    int L,T,O;
    scanf("%d%d%d",&L,&T,&O);
    build(1,L,1);
    char o;
    while(O--)
    {
        int s,e,c;
        getchar();
            scanf("%c",&o);
            scanf("%d%d",&s,&e);
        if(s>e)
            s^=e^=s^=e;
        if(o=='C')
        {
            scanf("%d",&c);
            update(s,e,1<<(c-1),1);
        }
        else
        {
            sum=0;
            quiry(s,e,1);
            printf("%d\n",solve(sum));
        }
    }
    return 0;
}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值