poj2777 Count Color

发布于 2020-02-20  96 次阅读


状压+线段树

Language:Default Count Color
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 58085 Accepted: 17377
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.
InputFirst 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.OutputOuput 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

题目大意讲的是有一块板子初始颜色为1

要求你实现区间染色和查询区间有几种颜色两种操作

由于总共最多有种30种颜色,所以可以使用状态压缩来解决统计,并用线段树来维护区间和,时间复杂度n(logn)

AC代码

#include <iostream>
#include <cstdio>
#include <algorithm>
#include <string>
#include <cstring>
using namespace std;
struct node
{
    int l, r, v;
} tree[810000];
int lazy[810000];
inline int ct(int x)
{
    x = (x & 0x55555555) + ((x >> 1) & 0x55555555);
    x = (x & 0x33333333) + ((x >> 2) & 0x33333333);
    x = (x & 0x0F0F0F0F) + ((x >> 4) & 0x0F0F0F0F);
    x = (x & 0x00FF00FF) + ((x >> 8) & 0x00FF00FF);
    x = (x & 0x0000FFFF) + ((x >> 16) & 0x0000FFFF);
    return x;
}
inline int make(int n)
{
    int ans = 1;
    for (int i = 1; i < n; i++)
    {
        ans <<= 1;
    }
    return ans;
}
void build(int l, int r, int pos)
{
    lazy[pos] = 0;
    if (l == r)
    {
        tree[pos].v = 1;
        tree[pos].l = l;
        tree[pos].r = r;
        return;
    }
    int mid = (l + r) >> 1;
    build(l, mid, pos * 2);
    build(mid + 1, r, pos * 2 + 1);
    tree[pos].l = l;
    tree[pos].r = r;
    tree[pos].v = 1;
    return;
}
int update(int l, int r, int v, int pos)
{
    if (tree[pos].l >= l && tree[pos].r <= r)
    {
        tree[pos].v = v;
        if (tree[pos].r != tree[pos].l)
            lazy[pos] = tree[pos].v;
        return tree[pos].v;
    }
    int mid = (tree[pos].l + tree[pos].r) >> 1;
    int ans = 0;
    if (lazy[pos] != 0)
    {
        tree[pos * 2].v = lazy[pos];
        tree[pos * 2 + 1].v = lazy[pos];
        lazy[pos * 2] = lazy[pos * 2 + 1] = lazy[pos];
        lazy[pos] = 0;
    }
    if (l <= mid)
    {
        ans = ans | update(l, r, v, pos * 2);
    }
    else
        ans = ans | tree[pos * 2].v;
    if (r > mid)
    {
        ans = ans | update(l, r, v, pos * 2 + 1);
    }
    else
    {
        ans = ans | tree[pos * 2 + 1].v;
    }
    tree[pos].v = ans;
    return ans;
}
int query(int l, int r, int pos)
{

    if (tree[pos].l >= l && tree[pos].r <= r)
    {
        return tree[pos].v;
    }
    int mid = (tree[pos].l + tree[pos].r) >> 1;
    int ans = 0;
    if (lazy[pos])
    {
        tree[pos * 2].v = lazy[pos];
        tree[pos * 2 + 1].v = lazy[pos];
        lazy[pos * 2] = lazy[pos * 2 + 1] = lazy[pos];
        lazy[pos] = 0;
    }
    if (l <= mid)
        ans = ans | query(l, r, pos * 2);
    if (r > mid)
    {
        ans = ans | query(l, r, pos * 2 + 1);
    }
    return ans;
}
int main()
{
    //reopen("in.txt", "r", stdin);
    int l, t, o;
    while (scanf("%d%d%d", &l, &t, &o) == 3)
    {
        build(1, l, 1);
        char s[10];
        int a, b, c;
        for (int i = 0; i < o; i++)
        {
            scanf("%s", s);
            if (s[0] == 'C')
            {
                scanf("%d%d%d", &a, &b, &c);
                if (a > b)
                    swap(a, b);
                update(a, b, make(c), 1);
            }
            else
            {
                scanf("%d%d", &a, &b);
                if (a > b)
                {
                    swap(a, b);
                }
                int tmp = query(a, b, 1);
                printf("%d\n", ct(tmp));
            }
        }
    }
    return 0;
}

💭💭💭💭💭💭