码迷,mamicode.com
首页 > 其他好文 > 详细

POJ P2777 Count Color——线段树状态压缩

时间:2017-04-10 21:31:43      阅读:186      评论:0      收藏:0      [点我收藏+]

标签:pre   ...   make   poj   yellow   and   space   time   ++   

Description

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. 
          --by POJ
http://poj.org/problem?id=2777


题目大意:
区间染色,统计区间颜色数;
解法,线段树维护区间颜色情况,开桶;
因为颜色种类少,故压成二进制,应该快些;
挺水的题,代码也短;
代码如下:
#include<cstdio>
using namespace std;
const int MAXN=100010;
int tree[MAXN<<2];
int lz[MAXN<<2];
int n,t,o,L,R;
void up(int );
void down(int ,int ,int );
void col(int ,int ,int ,int );
int ask(int ,int ,int );
int ans(int );
int main()
{
    int i,j,k;
    char s[3];
    scanf("%d%d%d",&n,&t,&o);
    lz[1]=1;
    tree[1]=1;
    for(i=1;i<=o;i++){
        scanf("%s",s);
        if(s[0]==C){
            scanf("%d%d%d",&L,&R,&j);
            if(L>=R)
                k=L,L=R,R=k;
            col(1,n,1,j);
        }
        else{
            scanf("%d%d",&L,&R);
            if(L>=R)
                k=L,L=R,R=k;
            j=ask(1,n,1);
            printf("%d\n",ans(j));
        }
    }
}
void up(int nu){
    tree[nu]=tree[nu<<1]|tree[nu<<1|1];
}
void down(int l,int r,int nu){
    if(!lz[nu])return ;
    tree[nu<<1]=lz[nu];
    tree[nu<<1|1]=lz[nu];
    lz[nu<<1]=lz[nu<<1|1]=lz[nu];
    lz[nu]=0;
}
void col(int l,int r,int nu,int x){
    if(L<=l&&r<=R){
        tree[nu]=lz[nu]=(1<<(x-1));
        return ;
    }
    down(l,r,nu);
    int mid=(l+r)>>1;
    if(L<=mid)
        col(l,mid,nu<<1,x);
    if(R>mid)
        col(mid+1,r,nu<<1|1,x);
    up(nu);
}
int ask(int l,int r,int nu){
    if(L<=l&&r<=R)
        return tree[nu];
    down(l,r,nu);
    int ans=0,mid=(l+r)>>1;
    if(L<=mid)
        ans|=ask(l,mid,nu<<1);
    if(R>mid)
        ans|=ask(mid+1,r,nu<<1|1);
    return ans;
}
int ans(int x){
    int re=0;
    while(x){
        if(x&1)
            re++;
        x>>=1;
    }
    return re;
}

  

POJ P2777 Count Color——线段树状态压缩

标签:pre   ...   make   poj   yellow   and   space   time   ++   

原文地址:http://www.cnblogs.com/nietzsche-oier/p/6690794.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!