POJ2777 Count Color 线段树

【题解】Count Color POJ - 2777 (线段树 区间染色)⭐⭐⭐ Count Color POJ - 2777 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 le... 阅读详情

颜色不多只有30种,用状态压缩每一位代表一种颜色,最后统计区间里面有多少个1就可以了。

线段树的成段更新和查询。

C A B C 将A B 段染成C

P A B 查询 A B段的颜色数量

Count Color
Time Limit: 1000MS Memory Limit: 65536K
Total Submissions: 30425 Accepted: 9083

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
#include<iostream>
#include<cstdio>
#include<string>
#include<algorithm>

using namespace std;

#define MAXN 100100
#define lson l,m,root<<1
#define rson m+1,r,root<<1|1

int col[MAXN<<2],node[MAXN<<2];

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

void push_down(int root)
{
    if(col[root])
    {
        col[root<<1]=col[root<<1|1]=col[root];
        node[root<<1]=node[root<<1|1]=col[root];
        col[root]=0;
    }
}

void build(int l,int r,int root)
{
    col[root]=0;
    if(l==r)
    {
        node[root]=1;
        return ;
    }
    int m=(l+r)>>1;
    build(lson);
    build(rson);
    push_up(root);
}

void update(int l,int r,int root,int L,int R,int d)
{
    if(l>=L&&r<=R)
    {
        node[root]=d;
        col[root]=d;
        return;
    }
    int m=(l+r)>>1;
    push_down(root);
    if(L<=m)
        update(lson,L,R,d);
    if(R>m)
        update(rson,L,R,d);
    push_up(root);
}

int query(int l,int r,int root,int L,int R )
{
    if(l>=L&&r<=R)
    {
        return node[root];
    }
    int m=(l+r)>>1,ret=0;
    push_down(root);
    if(L<=m)
        ret|=query(lson,L,R);
    if(R>m)
        ret|=query(rson,L,R);
    return ret;
}

int counts(int k)
{
    int ans=0;
    while(k)
    {
        if(k&1) ans++;
        k>>=1;
    }
    return ans;
}

int n,t,o;

int main()
{
    while(~scanf("%d%d%d",&n,&t,&o))
    {
        build(1,n,1);
        char str[5];
        int u,v,w;
        for(int i=0;i<o;i++)
        {
            scanf("%s",str);
            if(str[0]=='C')
            {
                scanf("%d%d%d",&u,&v,&w);
                if(u>v) swap(u,v);
                int col=1<<(w-1);
                update(1,n,1,u,v,col);
            }
            else
            {
                scanf("%d%d",&u,&v);
                if(u>v) swap(u,v);
                int ans=counts(query(1,n,1,u,v));
                printf("%d\n",ans);
            }
        }
    }
    return 0;
}


线段树(hdu3308LCIS、poj2777Count Color hdu3308 LCIS Time Limit: 6000/2000 MS (Java/Others) Memory Limit: 65536/32768 K (Java/Others) Total Submission(s): 10830 Accepted Submission(s): 4632 Problem Description Given n integers. You have two operations: U A B: replace the Ath number by B. ( 阅读详情

相关推荐

poj 2777 Count Color 线段树

题目大意:有一段长为L(1 C A B C ——将区间[A, B]涂为颜色C P A B —— 查询区间[A, B]间有多少种颜色 PS:初始时木板颜色为1;A不一定小于B。做题时需注意这两个地方。 解题思路: 首先,这是个更新区间,查询区间的问题,很容易想到要用线段树。那么接下来的工作就是线段树中需要哪些数据域来解决这个问题呢?一共有30种颜料,不多,但也不能每个节点开个数组

暮雨潇湘 1424

[poj2777 Count Color]线段树

Count Color Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 36607   Accepted: 11047 Description Chosen Problem Solving and Program design as an optional course

When you go away 1349

POJ 2777 Count Color(线段树

题目地址:POJ 2777 我去。。延迟标记写错了。标记到了叶子节点上。。。。这根本就没延迟嘛。。。怪不得一直TLE。。。 这题就是利用二进制来标记颜色的种类。然后利用或|这个符号来统计每个区间不同颜色种数。 代码如下: #include #include #include #include #include #include #include #include #incl

scf0920 1169

POJ2777 Count Color线段树 区间覆盖】

Count Color http://poj.org/problem?id=2777 Time Limit:1000MS Memory Limit:65536K Total Submissions:54255 Accepted:16304 Description Chosen Problem Solving and Program design...

逐梦者 395

POJ2777 Count Color线段树

Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 62027 Accepted: 18419 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 i

海岛Blog 459

POJ 2777 Count Color 线段树

线段树

岩之痕 1282

POJ 2777 Count Color 线段树

Count ColorTime Limit: 1000MS Memory Limit: 65536KTotal Submissions: 13340 Accepted: 3790<br />DescriptionChosen Problem Solving and Program design as an optional course, you are required to solve all kinds of problems. Here, we get a new problem. <br /><b

hqd_acm的专栏 776

Count Color poj2777 线段树

Count Color poj2777 线段树 题意 有一个长木板,现在往上面在一定区间内刷颜色,后来刷的颜色会掩盖掉前面刷的颜色,问每次一定区间内可以看到多少种颜色。 解题思路 这里使用线段树,因为刷颜色可以看作是区间修改,使用lazy标记区间的颜色种类,下传标记后,当前节点的lazy标记就标记为0,然后使用vis数组来标记颜色(颜色种类很少)。剩下的基本就是线段树的模板了。 代...

weixin_30432579的博客 153

POJ 2777Count Color线段树区间更新与查询)

POJ 2777Count Color线段树区间更新与查询) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 40949   Accepted: 12366 Description Chosen Problem Solving and Program d

ACMer' 1346

[POJ 2777]Count Color[线段树区间更新查询]

题目链接:[POJ 2777]Count Color[线段树区间更新查询] 题意分析: 给出一个长为L的长板,划分成L个单元,每个单元初始时颜色为1。先给出O个操作,包含更改区间l,r为颜色c和查询区间l,r中有多少种不同的颜色。颜色数不超过30个。 解题思路: 题目需要查询的是区间中有多少种不同的颜色,而颜色数又不超过30个,我们可以用二进制来表示一个区间中拥有的颜色,更新的时候等于两个子

闲停 734

Count Color POJ - 2777 线段树

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, ...

aocan6909的博客 178

POJ2777 Count Color 【简单的线段树

【题目大意】 现在有一个长度为L cm的板,每一厘米划分成一个格子,一开始每个格子颜色涂为1(1号颜色),之后有O个操作,一共有两种操作: 1、C a b c 代表将区间[a,b]全部刷成c颜色(一共有T种颜色,会读入) 2、P a b 代表询问区间[a,b]有多少种不同的颜色 数据范围:1 注意可能读入的a,b出现a>b的情况,需要交换一下; 【解题思路】(虽然我A了但是貌似时间和空

Lannister_Stark的博客 460

POJ 2777 Count Color线段树,区间修改)

线段树,区间修改 网上老哥思路: 线段树节点存储该区间内的颜色,单点区间肯定就是染成的颜色,大区间如果标记为-1则表示该区间由多种颜色的小区间组成, 注意初始情况下木板均为同一种颜色,可以记为1。一共有最多30种颜色,所以用bool vis[30]数组标记,在每次查询的时候清空, 查询到点或区间颜色相同时,对该颜色标记为true,最后统计1~T这T种颜色使用的个数即vis是true的情况 本题要点: 1、节点维护的信息: 区间范围: [l, r] 区间的颜色: color 延迟标记: flag 2、 套用线

qq_38232157的博客 222
上一篇: UVA10917 Walk Through the Forest SPFA+DP
下一篇: SDOI2011 染色 动态树
Albafica
博客等级 码龄15年 98粉丝 359原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值