zoukankan      html  css  js  c++  java
  • poj

    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

    题目分析 : 有两种操作,第一种是将区间的所有的数全被替换成所给的颜色,第二种是输出此区间有多少种不同的颜色, 此题采用一个特殊的标记 f 记录当前区间颜色,如果全部是同一种颜色,那么就将此区间的 f 更新为此区间的颜色,如果此区间的颜色不是全部相同,那么此区间的 f 记为 -1 。
          注意 :: 题干在最后说了一句话就是 a 可能 > b !!!
    代码示例 :
    const int eps = 1e5+5;
    const int maxn = 0x3f3f3f3f;
    #define ll long long
    #define lson k<<1
    #define rson k<<1|1
    
    int n, cnt, m;
    struct node
    {
        int l, r;
        int f;
    }t[eps<<2];
    int num[50];
    
    void build(int l, int r, int k){
        t[k].l = l;
        t[k].r = r;
        t[k].f = 1;
        
        if (l == r) return;
        int m = (l + r) >> 1;
        build(l, m, lson);
        build(m+1, r, rson);
    }
    
    void pushup(int k){
        if (t[lson].f == t[rson].f) t[k].f = t[lson].f;
        else t[k].f = -1;
    }
    
    void pushdown(int k){
        if (t[k].f != -1){
            t[lson].f = t[rson].f = t[k].f;
        }
    }
    
    void update(int l, int r, int c, int k){
        if (l <= t[k].l && t[k].r <= r){
            t[k].f = c;
            return;
        }
        pushdown(k);
        int m = (t[k].l + t[k].r) >> 1;
        if (l <= m) update(l, r, c, lson);
        if (r > m) update(l, r, c, rson);
        pushup(k);
        
    }
    
    void query(int l, int r, int k){
        if (l <= t[k].l && t[k].r <= r && t[k].f != -1 ){
            num[t[k].f] = 1;
            return;
        }
        pushdown(k);
        
        int m = (t[k].l + t[k].r) >> 1;
        if (l <= m) query(l,  min(m, r), lson);
        if (r > m) query(max(m+1, l), r, rson);
    }
    
    int main() {
        char s[5];
        int a, b, c;
        
        cin >> n >> cnt >> m;
        build(1, n, 1);
        while(m--){
            scanf("%s", s);
            if (s[0] == 'C'){
                scanf("%d%d%d", &a, &b, &c);
                if (a > b) swap(a, b);
                update(a, b, c, 1);    
            }
            else {
                scanf("%d%d", &a, &b);
                if (a > b) swap(a, b);
                memset(num, 0, sizeof(num));
                query(a, b, 1);
                int ans = 0;
                for(int i = 1; i <= 35; i++){
                    if (num[i]) ans++;
                }
                printf("%d
    ", ans);
            }
        }
        return 0;
    }
    
    东北日出西边雨 道是无情却有情
  • 相关阅读:
    C++继承 派生类中的内存布局(单继承、多继承、虚拟继承)
    Linux 共享库(动态库)
    虚幻4
    从头认识java-16.5 nio的数据转换
    JavaScript实现禁用键盘和鼠标的点击事件
    Codeforces Round #277.5 (Div. 2)部分题解
    iOS-WKWebView使用
    我学cocos2d-x (三) Node:一切可视化对象的祖先
    Android Studio右下角不显示当前branch名称
    Neo4j简单的样例
  • 原文地址:https://www.cnblogs.com/ccut-ry/p/8287209.html
Copyright © 2011-2022 走看看