线段树染色问题(区间一共有多少种不同颜色)poj2777

描述

选择问题解决和程序设计作为选修课程,您需要解决各种问题。在这里,我们遇到了一个新问题。 

有一个很长的板长L厘米,L是正整数,所以我们可以将板均分为L段,从左到右标记为1,2,...... L,每个为1厘米长。现在我们必须为电路板上色 - 只有一种颜色的一个部分。我们可以在电路板上执行以下两项操作:

1。“CAB C”将电路板从A段染色到B段,颜色为C. 
2.“PA B”输出A段和B段之间绘制的不同颜色的数量(包括)。 

在我们的日常生活中,我们用很少的词来描述颜色(红色,绿色,蓝色,黄色......),因此您可以假设不同颜色T的总数非常小。为了简单起见,我们将颜色的名称表示为颜色1,颜色2,...颜色T.开始时,板上涂有颜色1.现在剩下的问题留给你了。 

输入

第一行输入包含L(1 <= L <= 100000),T(1 <= T <= 30)和O(1 <= O <= 100000)。这里O表示操作次数。在O行之后,每个包含“CAB C”或“PA B”(这里A,B,C是整数,A可以大于B)作为先前定义的操作。

产量

输出操作的输出结果按顺序,每行包含一个数字。

样本输入

<span style="color:#000000">2 2 4
C 1 1 2
P 1 2
C 2 2 2
P 1 2
</span>

样本输出

<span style="color:#000000">2
1</span>

 首先本题的区间是一个一个的整数,所以不用乘2考虑开区间闭区间的问题了.所以这里的线段树维护一个color信息,其中color>0表示节点所指的区间都有某种颜色color, color=-1 表示该节点的子节点的颜色不一致.

       build操作: 递归建树,并且所有color初始设置为1.

       PushUp操作: 根据子节点的color更新i节点的color.如果子节点color一致,那么i节点color与子节点color一样.否则i节点color为-1.

       PushDown操作: color>0时才把color传递到下面去.PushDown操作可以不用改变i(父)节点的color.

       update操作: 如果[ql, qr]包括了[l, r] ,那么就直接置新color值,否则先PushDonw,然后分段更新color,再PushUp.

       query操作: 本操作主要是用一个cnt全局变量反馈[ql, qr]区间内一共有多少个不同的color.每次查询前都初始化vis[30]为false,出现一种颜色就vis[i]=true.如果[ql, qr]包括了[l, r]区间,那么直接更新vis,否则PushDown后,分段查找.
 

<span style="font-size:18px;">//一次AC
#include <iostream>
#include<cstdio>
#include<algorithm>
#include<cstring>
#include<cmath>
using namespace std;
#define lson i*2,l,m
#define rson i*2+1,m+1,r
const int MAXN = 100000 + 100;
bool vis[35];
int cnt;//用来计数颜色
struct IntervalTree
{
    int color[MAXN * 4];
 
    void build(int i, int l, int r)
    {
        color[i] = 1;
        if(l == r) return ;
        int m = (l + r) / 2;
        build(lson);
        build(rson);
    }
 
    void PushDown(int i)
    {
        if(color[i] > 0)
            color[i * 2] = color[i * 2 + 1] = color[i];
    }
 
    void PushUp(int i)
    {
        if(color[i * 2] == -1 || color[i * 2 + 1] == -1)
            color[i] = -1;
        else if(color[i * 2] == color[i * 2 + 1])
            color[i] = color[i * 2];
        else
            color[i] = -1;
    }
 
    void update(int ql, int qr, int v, int i, int l, int r)
    {
        if(ql <= l && r <= qr)
        {
            color[i] = v;
            return ;
        }
        PushDown(i);
        int m = (l + r) / 2;
        if(ql <= m) update(ql, qr, v, lson);
        if(m < qr) update(ql, qr, v, rson);
        PushUp(i);
    }
 
    void query(int ql, int qr, int i, int l, int r)
    {
        if(color[i] > 0)
        {
            if(vis[color[i]] == false)
                cnt++;
            vis[color[i]] = true;
            return ;
        }
        //PushDown(i);
        int m = (l + r) / 2;
        if(ql <= m) query(ql, qr, lson);
        if(m < qr) query(ql, qr, rson);
    }
};
IntervalTree T;
int main()
{
    int n, t, q;
    while(scanf("%d%d%d", &n, &t, &q) == 3)
    {
        T.build(1, 1, n);
        while(q--)
        {
            char str[10];
            scanf("%s", str);
            if(str[0] == 'C')
            {
                int x, y, z;
                scanf("%d%d%d", &x, &y, &z);
                T.update(x, y, z, 1, 1, n);
            }
            else if(str[0] == 'P')
            {
                int x, y;
                scanf("%d%d", &x, &y);
                memset(vis, 0, sizeof(vis));
                cnt = 0;
                T.query(x, y, 1, 1, n);
                printf("%d\n", cnt);
            }
        }
    }
    return 0;
}
</span>

猜你喜欢

转载自blog.csdn.net/qq_40859951/article/details/84134455