• 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;
    }
    
    东北日出西边雨 道是无情却有情
  • 相关阅读:
    Codeforces Round #505 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final)-C. Plasticine zebra
    牛客网-小白月赛6-J-洋灰三角
    树的最长链-POJ 1985 树的直径(最长链)+牛客小白月赛6-桃花
    BZOJ-4318-OSU!期望DP
    Codeforces Round #504 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final)-D- Array Restoration
    Codeforces Round #504 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final)-C-Bracket Subsequence
    Codeforces Round #504 (rated, Div. 1 + Div. 2, based on VK Cup 2018 Final)-A-Single Wildcard Pattern Matching
    Codeforces Round #503 (by SIS, Div. 2)-C. Elections
    百度之星-day1-1003-度度熊剪纸条
    百度之星-day2-1004-二分答案
  • 原文地址:https://www.cnblogs.com/ccut-ry/p/8287209.html
Copyright © 2020-2023  润新知