luoguP1558 色板游戏

题目背景

阿宝上学了,今天老师拿来了一块很长的涂色板。

题目描述

色板长度为L,L是一个正整数,所以我们可以均匀地将它划分成L块1厘米长的小方格。并从左到右标记为1, 2, … L。现在色板上只有一个颜色,老师告诉阿宝在色板上只能做两件事:1. “C A B C” 指在A到 B 号方格中涂上颜色 C。2. “P A B” 指老师的提问:A到 B号方格中有几种颜色。学校的颜料盒中一共有 T 种颜料。为简便起见,我们把他们标记为 1, 2, … T. 开始时色板上原有的颜色就为1号色。 面对如此复杂的问题,阿宝向你求助,你能帮助他吗?

输入输出格式

输入格式:
第一行有3个整数 L (1 <= L <= 100000), T (1 <= T <= 30) 和 O (1 <= O <= 100000). 在这里O表示事件数, 接下来 O 行, 每行以 “C A B C” 或 “P A B” 得形式表示所要做的事情(这里 A, B, C 为整数, 可能A> B)

输出格式:
对于老师的提问,做出相应的回答。每行一个整数。

输入输出样例

输入样例#1:
2 2 4
C 1 1 2
P 1 2
C 2 2 2
P 1 2

输出样例#1:
2
1

分析:
线段树
看到T的范围不大,我就干脆在每个线段树节点中都开了一个30的数组,记录该区间每种颜色的数量
其余的就是朴素操作了

tip

一开始狂WA不止,知道wo看到了这句话
这里 A, B, C 为整数, 可能A> B
改了之后就A了。。。。坑坑坑!!!

luogu上我每个点都是1000ms–(没有吸氧)
这里写图片描述

但是在poj2777上时限都一样,
却是悲惨的TLE
只能说明poj的评测机比较辣鸡。。

这里写代码片
#include<cstdio>
#include<iostream>
#include<cstring>

using namespace std;

const int N=100001;
struct node{
    int x,y,la,T[32];
};
node t[N<<2];
int n,O,C,co[32];

inline void print(int bh,int z)
{
    for (int i=1;i<=C;i++)
        if (i!=z) t[bh].T[i]=0;
        else t[bh].T[i]=t[bh].y-t[bh].x+1;
}

void push(int bh)
{
    if (t[bh].x!=t[bh].y&&t[bh].la)
    {
        int lc=bh<<1;
        int rc=lc+1;
        print(lc,t[bh].la);
        print(rc,t[bh].la);
        t[lc].la=t[rc].la=t[bh].la;
        t[bh].la=0;
    }
}

inline void update(int bh)
{
    for (int i=1;i<=C;i++) 
        t[bh].T[i]=t[bh<<1].T[i]+t[bh<<1|1].T[i];
}

void build(int bh,int l,int r)
{
    t[bh].x=l;
    t[bh].y=r;
    if (l==r)
    {
        t[bh].T[1]=1;
        return;
    }
    int mid=(l+r)>>1;
    build(bh<<1,l,mid);
    build(bh<<1|1,mid+1,r);
    update(bh);
}

void change(int bh,int l,int r,int z)
{
    push(bh);
    if (t[bh].x>=l&&t[bh].y<=r)
    {
        print(bh,z);
        t[bh].la=z;
        return;
    }
    int mid=(t[bh].x+t[bh].y)>>1;
    if (l<=mid) change(bh<<1,l,r,z);
    if (r>mid) change(bh<<1|1,l,r,z);
    update(bh);
}

void copy(int bh)
{
    for (int i=1;i<=C;i++)
        co[i]+=t[bh].T[i];
}

void ask(int bh,int l,int r)
{
    push(bh);
    if (t[bh].x>=l&&t[bh].y<=r)
    {
        copy(bh);
        return;
    }
    int mid=(t[bh].x+t[bh].y)>>1;
    if (l<=mid) ask(bh<<1,l,r);
    if (r>mid) ask(bh<<1|1,l,r);
}

int main()
{
    scanf("%d%d%d",&n,&C,&O);
    build(1,1,n);
    char s[2];
    for (int i=1;i<=O;i++)
    {
        int x,y,z;
        scanf("%s",&s);
        if (s[0]=='C')
        {
            scanf("%d%d%d",&x,&y,&z);
            if (x>y) swap(x,y);
            change(1,x,y,z);
        }
        else
        {
            int tot=0;
            memset(co,0,sizeof(co));
            scanf("%d%d",&x,&y);
            if (x>y) swap(x,y);
            ask(1,x,y);
            for (int j=1;j<=C;j++) if (co[j]>0) tot++;
            printf("%d
",tot);
        }
    }
    return 0;
}
原文地址:https://www.cnblogs.com/wutongtong3117/p/7673258.html