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

    传送门: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. 

    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(N <= 100000)的数列Si,紧接着Q(Q <= 100000)条操作,操作 形式有两种: 1. "C A B C" 将A到B的数都染成C这种颜色。 2. "P A B" 输出A和B之间不同颜色的数目。

    题解:

    首先要想到的就是线段树,经典的区间维护问题,这里需要用到一个叫懒惰标记的方法,

    这样可以大大缩短更新所需要的时间

    这里还有一个巧妙地地方,就是区间染色的个数,这个可以用二进制位来解决,最多30种,用

    一个int就可以存下,每个位代表一个颜色,区间上的颜色可以取其子区间或和(详细看代码)。

    关于懒惰标记:

    lazy是一个很经典的思想。所谓lazy,就是懒惰,每次不想做太多,只要插入的区间完

    全覆盖了当前结点所管理的区间就不再往下做了,在当前结点上打上一个lazy标记,然

    后直接返回。下次如果遇到当前结点有lazy标记的话,直接传递给两个儿子,自己的标

    记清空。这样做肯定是正确的。我们以染色为例,可以这样想,如果当前结点和它的子

    孙都有lazy标记的话,必定是子孙的先标记,因为如果是自己先标记,那么在访问子孙

    的时候,必定会将自己的标记下传给儿子,而自己的标记必定会清空,那么lazy标记也

    就不存在了。所以可以肯定,当前的lazy标记必定覆盖了子孙的,所以直接下传即可,

    不需要做任何判断

    代码:

    #include <stdio.h>
    #include <algorithm>
    #include <cmath>
    #include <cstring>
    #include <deque>
    #include <iomanip>
    #include <iostream>
    #include <list>
    #include <map>
    #include <queue>
    #include <set>
    #include <utility>
    #include <vector>
    #define mem(arr, num) memset(arr, 0, sizeof(arr))
    #define _for(i, a, b) for (int i = a; i <= b; i++)
    #define __for(i, a, b) for (int i = a; i >= b; i--)
    #define IO                     
      ios::sync_with_stdio(false); 
      cin.tie(0);                  
      cout.tie(0);
    using namespace std;
    typedef long long ll;
    const ll inf = 0x3f3f3f3f;
    const double EPS = 1e-10;
    const ll mod = 1000000007LL;
    const int N = 1 << 19;
    int dat[N];
    void down(int k)
    {
        dat[k << 1] = dat[k << 1 | 1] = dat[k];
    }
    void update(int a, int b, int k, int l, int r, int c)
    {
        if (b < l || a > r)
            ;
        else if (a <= l && b >= r)
        {
            dat[k] = 1 << (c - 1);
        }
        else if (a <= r && b >= l)
        {
            if (log2(dat[k]) == (int)log2(dat[k])) down(k);
            update(a, b, k << 1, l, (l + r) / 2, c);
            update(a, b, k << 1 | 1, (l + r) / 2 + 1, r, c);
            dat[k] = dat[k << 1] | dat[k << 1 | 1];
        }
    }
    int query(int a, int b, int k, int l, int r)
    {
        if (a > r || b < l) return 0;
        if (a <= l && b >= r)
        {
            return dat[k];
        }
        else if (a <= r && b >= l)
        {
            if (log2(dat[k]) == (int)log2(dat[k])) down(k);
            return query(a, b, k << 1, l, (l + r) / 2) | query(a, b, k << 1 | 1, (l + r) / 2 + 1, r);
        }
    
    }
    int main()
    {
        int n, t, o, a, b, c;
        while (scanf("%d%d%d", &n, &t, &o) != EOF)
        {
            mem(dat,0);
            dat[1] = 1;
            _for(i, 1, o)
            {
                char op;
                getchar();
                scanf("%c", &op);
                if (op == 'C')
                {
                    scanf("%d%d%d", &a, &b, &c);
                    if (a > b) swap(a, b);
                    update(a, b, 1, 1, n, c);
                }
                else
                {
                    scanf("%d%d", &a, &b);
                    if (a > b) swap(a, b);
                    int ans = query(a, b, 1, 1, n);
                    int sum = 0;
                    while (ans) ans &= (ans - 1), sum++;
                    printf("%d
    ", sum);
                }
            }
        }
        return 0;
    }
    /*
    8 5 30
    C 1 3 2
    C 2 4 3
    C 3 5 4
    C 4 6 5
    C 5 7 6
    C 6 8 7
    P 1 8
    
    */
    宝剑锋从磨砺出 梅花香自苦寒来
  • 相关阅读:
    nginx日志格式字段
    set_include_path和get_include_path用法详解
    nginx try_files 详解
    ul ol li的序号编号样式
    PHP中报500错误时如何查看错误信息
    nginx的access.log文件详解
    一些常用服务命令和配置目录
    PHP 使用 Redis
    HTML5-indexedDB使用常见错误总结
    浏览器数据库 IndexedDB 入门
  • 原文地址:https://www.cnblogs.com/GHzcx/p/8933290.html
Copyright © 2011-2022 走看看