您的位置:首页 > 其它

poj 2777 Count Color

2012-08-05 18:20 375 查看
http://poj.org/problem?id=2777

一个坑爹的错误找了我半天,脑残呀!我用的数状态压存储的,加上延迟标记下就行了

#include<cstdio>
#include<cstring>
#include<iostream>
#include<algorithm>

#define maxn 555555

using namespace std;

#define lson l,m,rt<<1
#define rson m+1,r,rt<<1|1
typedef long long ll;

ll d[maxn<<2];
bool col[maxn<<2];

void pushdown(int rt)
{
    if(col[rt])
    {
        col[rt<<1]=1;
        col[rt<<1|1]=1;
        d[rt<<1]=d[rt];
        d[rt<<1|1]=d[rt];
        col[rt]=0;
    }
}
void update(int L,int R,int C,int l,int r,int rt)
{
    //cout<<l<<" "<<r<<" "<<rt<<endl;
    if(L<=l&&R>=r)
    {
        d[rt]=(1LL)<<C;
        col[rt]=1;  // 注意把它赋值为1
        return;
    }
    pushdown(rt);
    int m=(l+r)>>1;
    if(L<=m) update(L,R,C,lson);
    if(R>m)  update(L,R,C,rson);
    d[rt]=d[rt<<1]|d[rt<<1|1];
}
ll query(int L,int R,int l,int r,int rt)
{
    if(L<=l&&R>=r) return d[rt];

    pushdown(rt);

    int m=(l+r)>>1;
    ll ret1=0,ret2=0;
    if(L<=m) ret1=query(L,R,lson);
    if(R>m)  ret2=query(L,R,rson);
    return ret1|ret2;
}
int main()
{
    int T,L,Q,l,r,c;
    char ord[5];
    while(scanf("%d%d%d",&L,&T,&Q)==3)
    {
        memset(col,0,sizeof(col));
        d[1]=1;
        col[1]=1;
        while(Q--)
        {
            scanf("%s",ord);
            if(ord[0]=='C')
            {
                scanf("%d %d %d",&l,&r,&c);
                if(l>r) swap(l,r);
                update(l,r,c-1,1,L,1);
            }
            else
            {
                scanf("%d %d",&l,&r);
                if(l>r) swap(l,r);
                ll ans=query(l,r,1,L,1);
                int cnt=0;
                for(int i=0;i<T;i++)
                  if(ans&((1LL)<<i)) cnt++;
                printf("%d\n",cnt);
            }
        }
    }
    return 0;
}
内容来自用户分享和网络整理,不保证内容的准确性,如有侵权内容,可联系管理员处理 点击这里给我发消息
标签: