zoukankan      html  css  js  c++  java
  • POJ P2777 Count Color——线段树状态压缩

    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
    ",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;
    }

      

  • 相关阅读:
    [轉]多memcached 和 mysql 主从 环境下PHP开发: 代码详解
    [轉]windows下mysql主从同步备份步骤
    PHP Mysqli Class收集
    [轉]ASP的类(class)详解
    [轉]pdo 帮助类 pdo class php 数据库 处理层 pdo dbhelper
    [轉]FCK编辑区的背景色 FCK工具条的背景 修改
    XSchEmA, Change your entire life! 发布倒计时 2天
    RBAC 权限入门
    防范式编程原则参数验证
    Noebe V2.0 美味的持久层 API 文档 一
  • 原文地址:https://www.cnblogs.com/nietzsche-oier/p/6690794.html
Copyright © 2011-2022 走看看