zoukankan      html  css  js  c++  java
  • POJ 2777 Count Color (线段树)

    Count Color
    Time Limit: 1000MS   Memory Limit: 65536K
    Total Submissions: 29895   Accepted: 8919

    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. 

    Input

    First line of input contains L (1 <= L <= 100000), T (1 <= T <= 30) and O (1 <= O <= 100000). Here O denotes the number of operations. Following O lines, each contains "C A B C" or "P A B" (here A, B, C are integers, and A may be larger than B) as an operation defined previously.

    Output

    Ouput results of the output operation in order, each line contains a number.

    Sample Input

    2 2 4
    C 1 1 2
    P 1 2
    C 2 2 2
    P 1 2
    

    Sample Output

    2
    1
    

    Source

     
     

    题意:长度为n(1~100000)个单位的画板,有t(1~30,位运算的可能性)种颜料。现在叫你完成m组操作:

          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).

    思路:很经典的线段树。学习到了很多的新知识,最重要的是三点:1.延迟覆盖的操作。2.位操作,用 | 来合并颜色种类。3.updata操作时递归回来,两个子节点的信息对父节点的更新。

    #include<iostream>
    #include<cstdio>
    #include<cstring>
    
    using namespace std;
    
    const int N=100010;
    
    #define L(rt) (rt<<1)
    #define R(rt) (rt<<1|1)
    
    struct Tree{
        int l,r;
        int col;    //  用一个32位的int型,每一位对应一种颜色,用位运算代替bool col[32]
        bool cover; //  表示这个区间都被涂上同一种颜色,线段树效率的体现,否则插入就是0(n)了。
    }tree[N<<2];
    
    void PushUp(int rt){    // 最后递归回来再更改父节点的颜色
        tree[rt].col=tree[L(rt)].col | tree[R(rt)].col;
    }
    
    void build(int L,int R,int rt){
        tree[rt].l=L;
        tree[rt].r=R;
        tree[rt].col=1; //  开始时都为涂有颜色1,看题要仔细,要注意状态。
        tree[rt].cover=1;
        if(tree[rt].l==tree[rt].r)
            return ;
        int mid=(L+R)>>1;
        build(L,mid,L(rt));
        build(mid+1,R,R(rt));
    }
    
    void PushDown(int rt){  //  延迟覆盖的操作
        tree[L(rt)].col=tree[rt].col;
        tree[L(rt)].cover=1;
        tree[R(rt)].col=tree[rt].col;
        tree[R(rt)].cover=1;
        tree[rt].cover=0;
    }
    
    void update(int val,int L,int R,int rt){
        if(L<=tree[rt].l && R>=tree[rt].r){
            tree[rt].col=val;
            tree[rt].cover=1;
            return ;
        }
        if(tree[rt].col==val)  //剪枝
            return ;
        if(tree[rt].cover)
            PushDown(rt);
        int mid=(tree[rt].l+tree[rt].r)>>1;
        if(R<=mid)
            update(val,L,R,L(rt));
        else if(L>=mid+1)
            update(val,L,R,R(rt));
        else{
            update(val,L,mid,L(rt));
            update(val,mid+1,R,R(rt));
        }
        PushUp(rt);      // 最后递归回来再更改父节点的颜色
    }
    
    int sum;
    
    void query(int L,int R,int rt){
        if(L<=tree[rt].l && R>=tree[rt].r){
            sum |= tree[rt].col;
            return ;
        }
        if(tree[rt].cover){     //  这个区间全部为1种颜色,就没有继续分割区间的必要了
            sum |= tree[rt].col;     //  颜色种类相加的位运算代码
            return;
        }
        int mid=(tree[rt].l+tree[rt].r)>>1;
        if(R<=mid)
            query(L,R,L(rt));
        else if(L>=mid+1)
            query(L,R,R(rt));
        else{
            query(L,mid,L(rt));
            query(mid+1,R,R(rt));
        }
    }
    
    int solve(){
        int ans=0;
        while(sum){
            if(sum&1)
                ans++;
            sum>>=1;
        }
        return ans;
    }
    
    void swap(int &a,int &b){
        int tmp=a;a=b;b=tmp;
    }
    
    int main(){
    
        //freopen("input.txt","r",stdin);
    
        int n,t,m;
        while(~scanf("%d%d%d",&n,&t,&m)){
            build(1,n,1);
            char op[3];
            int a,b,c;
            while(m--){
                scanf("%s",op);
                if(op[0]=='C'){
                    scanf("%d%d%d",&a,&b,&c);
                    if(a>b)
                        swap(a,b);
                    update(1<<(c-1),a,b,1); // int型的右起第c位变为1,即2的c-1次方。
                }else{
                    scanf("%d%d",&a,&b);
                    if(a>b)
                        swap(a,b);
                    sum=0;
                    query(a,b,1);
                    printf("%d\n",solve());
                }
            }
        }
        return 0;
    }
  • 相关阅读:
    python把汉字转换成拼音实现程序
    C#第三方zip解压压缩工具,带事例源码
    实现在线压缩文件的实现程序代码
    如何处理JSON中的特殊字符
    《汉字简体、繁体相互转换》 查看源代码
    中文字符转拼音源码
    upper_bound()函数使用方法
    CodeFroces New Assignment 二分图匹配
    选拔赛 hash 字符串匹配 哈希算法(白书p374)
    lower_bound()函数使用
  • 原文地址:https://www.cnblogs.com/jackge/p/3041800.html
Copyright © 2011-2022 走看看