Count Color
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 32217   Accepted: 9681

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
思路:线段树,每个节点记录是否当前区间只被一种颜色覆盖,如果是就将此颜色记录下来,如果不是,说明不是单色区间,那么就继续寻找其子区间,直到找到单色区间为止。其实很简单,因为整个区间必定是由一个个单色区间组成的!!!
一开始这题提交的时候超时了,很费解,后来我把全局变量mid改成局部变量居然过了,不知道为什么,难道CPU对这两者的访问效率不同?反正很费解。。。,以后注意吧,少用全局变量。
AC 代码:
 #include<stdio.h>
#include<string.h>
#define L(x) (x << 1)
#define R(x) (x << 1|1)
#define MAX 100000
#define T 40
typedef struct
{
int l,r;
int cover;
}Node;
Node node[*MAX];
int color[T];
int sum;
void build(int l,int r,int k)
{
node[k].l = l;
node[k].r = r;
if(l == r)
return ;
int mid = (l+r) >> ;
build(l,mid,L(k));
build(mid+,r,R(k));
} void insert(int l,int r,int clr,int k)
{
if(l <= node[k].l && r >= node[k].r)
{
node[k].cover = clr;
return ;
}
else
{
if(node[k].cover > )
{
node[L(k)].cover = node[k].cover;
node[R(k)].cover = node[k].cover;
node[k].cover = ;
}
if(l > node[L(k)].r)
insert(l,r,clr,R(k));
else if(r <= node[L(k)].r)
insert(l,r,clr,L(k));
else
{
insert(l,node[L(k)].r,clr,L(k));
insert(node[R(k)].l,r,clr,R(k));
}
}
} void get_result(int l,int r,int k)
{
if(node[k].cover > )
{
color[node[k].cover] = ;
}
else
{
if(l > node[L(k)].r)
get_result(l,r,R(k));
else if(r <= node[L(k)].r )
get_result(l,r,L(k));
else
{
get_result(l,node[L(k)].r,L(k));
get_result(node[R(k)].l,r,R(k));
}
}
} int main()
{
int n,t,m,i,j;
int a,b,c,d,e;
char str[];
//freopen("in.c","r",stdin);
while(~scanf("%d%d%d",&n,&t,&m))
{
memset(str,,sizeof(str));
memset(color,,sizeof(color));
build(,n,);
node[].cover = ;
for(i = ;i <= m;i ++)
{
scanf("%s",str);
if(!strcmp(str,"C"))
{
scanf("%d%d%d",&a,&b,&c);
d = a < b?a:b;
e = a > b?a:b;
insert(d,e,c,);
}
if(!strcmp(str,"P"))
{
sum = ;
scanf("%d%d",&a,&b);
memset(color,,sizeof(color));
d = a < b?a:b;
e = a > b?a:b;
get_result(d,e,);
for(j = ;j <= t;j ++)
{
if(color[j])
sum ++;
}
printf("%d\n",sum);
}
memset(str,,sizeof(str));
}
}
return ;
}

												

Count Color POJ--2777的更多相关文章

  1. Count Color POJ - 2777 线段树

    Chosen Problem Solving and Program design as an optional course, you are required to solve all kinds ...

  2. POJ 2777 Count Color(线段树染色,二进制优化)

    Count Color Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 42940   Accepted: 13011 Des ...

  3. poj 2777 Count Color

    题目连接 http://poj.org/problem?id=2777 Count Color Description Chosen Problem Solving and Program desig ...

  4. poj 2777 Count Color(线段树)

    题目地址:http://poj.org/problem?id=2777 Count Color Time Limit: 1000MS   Memory Limit: 65536K Total Subm ...

  5. poj 2777 Count Color(线段树区区+染色问题)

    题目链接:  poj 2777 Count Color 题目大意:  给出一块长度为n的板,区间范围[1,n],和m种染料 k次操作,C  a  b  c 把区间[a,b]涂为c色,P  a  b 查 ...

  6. POJ - 2777——Count Color(懒标记线段树二进制)

    Count Color Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 53639   Accepted: 16153 Des ...

  7. 【POJ 2777】 Count Color(线段树区间更新与查询)

    [POJ 2777] Count Color(线段树区间更新与查询) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4094 ...

  8. poj 2777 Count Color(线段树、状态压缩、位运算)

    Count Color Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 38921   Accepted: 11696 Des ...

  9. POJ 2777 Count Color(线段树之成段更新)

    Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 33311 Accepted: 10058 Descrip ...

  10. POJ 2777 Count Color(段树)

    职务地址:id=2777">POJ 2777 我去.. 延迟标记写错了.标记到了叶子节点上.. . . 这根本就没延迟嘛.. .怪不得一直TLE... 这题就是利用二进制来标记颜色的种 ...

随机推荐

  1. c常用字符串函数

    获取字符串长度 : size_t strlen(const char *str); 字符串拷贝函数: 把src中内容拷贝到dest中,它会覆盖原来的内容,它会把src中的\0,没有覆盖内容不变 如果s ...

  2. 讲解最好的Python正则表达式

    这篇文章转载自:http://www.cnblogs.com/huxi/archive/2010/07/04/1771073.html#!comments 这里非常感谢作者无私的贡献自己的成果. 请大 ...

  3. 使用 Virtual Box 安装 android x86

    1.安装 跟随别人的教程:http://www.maketecheasier.com/run-android-4-3-in-virtualbox/ 2.问题 安装过程出现以下问题:Kernel pan ...

  4. 《APUE》第三章笔记(3)

    文件共享 UNIX系统支持在不同进程中共享打开的文件,首先先用一幅apue的图来介绍一下内核用于I/O文件的数据结构: 如图所见,一个进程都会有一个记录项,记录项中包含有一张打开文件描述符表,每个描述 ...

  5. ruby环境sass编译中文出现Syntax error: Invalid GBK character错误解决方法

    sass文件编译时候使用ruby环境,无论是界面化的koala工具还是命令行模式的都无法通过,真是令人烦恼. 容易出现中文注释时候无法编译通过,或者出现乱码,找了几天的解决方法终于解决了. 这个问题的 ...

  6. 固定滚动外层div的css

    background-color: #2a3138; position: fixed; bottom: 0; left: 0; width: 100%; height: 57px; overflow: ...

  7. shell脚本修复MySQL主从同步

    发布:thebaby   来源:net     [大 中 小] 分享一例shell脚本,用于修改mysql的主从同步问题,有需要的朋友参考下吧. 一个可以修改mysql主从同步的shell脚本. 例子 ...

  8. oracle安装,配置,启动

    因为主要不是讲oracle这些,所以就略写. 注意安装的时候:确保以前安装过的卸载干净了.安装的路径不能包含中文.安装的时候需要创建两层数据库.因为在选择的安装目录的平行目录会产生文件. 确认卸载完可 ...

  9. python入门 第二天笔记

    程序主文件标志if __name__=="__main__": 在程序执行python 1.py 时候 程序1.py __name__ 为 main调用其他文件是,__name__ ...

  10. SORT_AREA_RETAINED_SIZE

    manual pga: SORT_AREA_RETAINED_SIZE specifies (in bytes) the maximum amount of the user global area ...