poj 2777

区间染色什么的。。。


#include<map>
#include<string>
#include<cstdio>
#include<cstdlib>
#include<cstring>
#include<ctime>
#include<cmath>
#include<iostream>
#include<algorithm>
const int MAXN = 100005;

int n,T,m;
struct node
{
    int cnt,col;
}tree[MAXN<<2] = {0};

#define L(x) (x<<1)
#define R(x) ((x<<1)|1)

void pushdown(int x)
{
    if(tree[x].col)
    {
        tree[L(x)].col = tree[L(x)].cnt = tree[x].col;
        tree[R(x)].col = tree[R(x)].cnt = tree[x].col;
        tree[x].col = 0;
    }
}
void update(int x)
{
    tree[x].cnt = tree[L(x)].cnt | tree[R(x)].cnt;
}
void color(int l,int r,int ll,int rr,int si,int col)
{
    if(ll == l && rr == r)
        tree[si].col = tree[si].cnt = col;
    else
    {
        int mid = (ll+rr)>>1;

        pushdown(si);
        if(r<=mid) color(l,r,ll,mid,L(si),col);
        else if(l>mid)color(l,r,mid+1,rr,R(si),col);
        else   
        {
            color(l,mid,ll,mid,L(si),col);
            color(mid+1,r,mid+1,rr,R(si),col);
        }
        update(si);
    }
}
int count(int l,int r,int ll,int rr,int si)
{
    if(ll == l && rr == r)return tree[si].cnt;
    else
    {
        int mid = (ll+rr)>>1;

        pushdown(si);
        if(r<=mid)    return count(l,r,ll,mid,L(si));
        else if(l>mid)return count(l,r,mid+1,rr,R(si));
        else      return count(l,mid,ll,mid,L(si)) | count(mid+1,r,mid+1,rr,R(si));
    }
}
int fold(int x)
{
    int ret = 0;
    while(x){ret+=(x&1); x>>=1;}
    return ret;
}
#undef L
#undef R

int main()
{
#ifndef ONLINE_JUDGE    
    freopen("poj2777.in","r",stdin);
    freopen("poj2777.out","w",stdout);
#endif

    scanf("%d%d%d",&n,&T,&m);

    tree[1].col = tree[1].cnt = 1;
    while(m--)
    {
        int a,b,c;
        char ch[5] = {'\0'};
        scanf("%s",ch);

        scanf("%d%d",&a,&b);
        if(a > b) std::swap(a,b);

        switch(ch[0])
        {
            case 'C': scanf("%d",&c); 
                      color(a,b,1,n,1,1<<(c-1)); 
                      break;
            case 'P': printf("%d\n",fold(count(a,b,1,n,1)));
                      break;    
        }
    }

#ifndef ONLINE_JUDGE    
    fclose(stdin);
    fclose(stdout);
#endif  
    return 0;
}
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值