`
Simone_chou
  • 浏览: 185271 次
  • 性别: Icon_minigender_2
  • 来自: 广州
社区版块
存档分类
最新评论

Count Color(线段树 + 区间统计)

    博客分类:
  • POJ
 
阅读更多
Count Color
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 34605   Accepted: 10445

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

 

      题意:

      给出 L, T, O,代表有一条长度为 L(1 ~ 100000) 的板,有 T(1 ~ 30) 种颜色,有 O (1 ~ 100000)个操作。后给出 O 个操作,操作有两类,第一类 C 代表有将 L 到 R 这段区间涂为 Ti 号颜色,第二类 P 代表询问 L 到 R 这段颜色有多少种,每次 P 操作的时候就输出这个数。

 

      思路:

      线段树 + 延迟标记。因为一共有 30 号颜色,所以用一个二进制数来表示每一段涂色的情况,树维护二进制涂色的情况,同时标记维护覆盖的情况,push_down 操作为直接覆盖操作,所以直接等于 mark 标记颜色就好,push_up 操作为左右子区间的或操作。统计区间内的颜色数的时候也需要用 或 来维护结果,最后对结果求出二进制有多少个 1 的情况即为答案。注意这题给出的左右区间数不严格遵守左边大于右边,所以还要判断要不要交换。

 

      AC:

#include <cstdio>
#include <cstring>
#include <algorithm>

using namespace std;

const int MAX = 100005;

int color[MAX * 3], mark[MAX * 3];
int sum;

int count_one() {
        int ans = 0;

        while (sum) {
                if (sum % 2) ++ans;
                sum >>= 1;
        }

        return ans;
}

void push_up(int node) {
        color[node] = color[node << 1] | color[node << 1 | 1];
}

void push_down(int node, int l, int r) {
        if (mark[node]) {
                mark[node << 1] = mark[node];
                mark[node << 1 | 1] = mark[node];

                color[node << 1] = mark[node];
                color[node << 1 | 1] = mark[node];

                mark[node] = 0;
        }
}

void build (int node, int l, int r) {
        if (l == r) {
                color[node] = 1;
                mark[node] = 0;
        } else {
                int mid = (l + r) >> 1;
                build(node << 1, l, mid);
                build(node << 1 | 1, mid + 1, r);
                push_up(node);
        }
}

void update (int node, int l, int r, int cl, int cr, int c) {

        if (cl > r || cr < l) return;
        if (cl <= l && cr >= r) {
                mark[node] = c;
                color[node] = c;
                return;
        }

        push_down(node, l, r);

        int mid = (r + l) >> 1;
        update(node << 1, l, mid, cl, cr, c);
        update(node << 1 | 1, mid + 1, r, cl, cr, c);
        push_up(node);
}

void query (int node, int l, int r, int cl, int cr) {
        if (cl > r || cr < l) return;
        if (cl <= l && cr >= r) {
                sum |= color[node];
                return;
        }

        push_down(node, l, r);
        int mid = (r + l) >> 1;
        query(node << 1, l, mid, cl, cr);
        query(node << 1 | 1, mid + 1, r, cl, cr);
        push_up(node);
}

int main() {
        int n, c, o;

        while(~scanf("%d%d%d", &n, &c, &o)) {

                build(1, 1, n);

                while (o--) {
                        char c;
                        scanf(" %c", &c);

                        if (c == 'C') {
                                int cl, cr, col;
                                scanf("%d%d%d", &cl, &cr, &col);
                                if (cl > cr) swap(cl, cr);

                                update(1, 1, n, cl, cr, 1 << (col - 1));
                        } else {
                                int cl, cr;
                                scanf("%d%d", &cl, &cr);
                                if (cl > cr) swap(cl, cr);

                                sum = 0;
                                query(1, 1, n, cl, cr);

                                printf("%d\n", count_one());
                        }
                }

        }

        return 0;
}

 

 

 

分享到:
评论

相关推荐

Global site tag (gtag.js) - Google Analytics