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. xcode编译运行报错纪录(持续更新)

    ---恢复内容开始--- 1. Undefined symbols for architecture i386: "_deflate", referenced from: -[NS ...

  2. 一些简单的帮助类(2)-- JavaSctipt Array Linq

    在日程工作中经常会遇到这样的问题 一个JS数组 我们要找出其中 一些符合要求的类容 又或者对数组里的类容求和求平均数之类的一般的做法是循环里面的类容做判断添加到一个新的集合里 var array = ...

  3. Flask,HelloWorld

    Flask,HelloWorld # -*- coding:utf-8 -*- ''' Created on 2015年10月19日 ''' from flask import Flask app = ...

  4. weblogic 12c 配置jvm的内存大小

    每个weblogic server 都是运行在一个java虚拟机上 ,对weblogic的内存设置也就是对java虚拟机的内存设置. MEM_ARGS=-Xms512m -Xmx1024m -XX:M ...

  5. 暑假集训(1)第三弹 -----Dungeon Master(Poj2251)

    Description You are trapped in a 3D dungeon and need to find the quickest way out! The dungeon is co ...

  6. 《sed的流艺术之四》-linux命令五分钟系列之二十四

    本原创文章属于<Linux大棚>博客,博客地址为http://roclinux.cn.文章作者为rocrocket. 为了防止某些网站的恶性转载,特在每篇文章前加入此信息,还望读者体谅. ...

  7. C# 给picturebox添加滚动条

    在一个项目中需要给picturebox增加滚动条,我先前的做法和网上一样,将picturebox放在一个panel上,将panel的AutoScroll设置为ture,将picturebox的Size ...

  8. 在Apache下开启SSI配置支持include shtml html和快速配置服务器

    作为前端开发,使用Apache快速搭建服务器极为方便. 1.找到apach安装目录,找到conf目录下 的httpd.conf 使用SSI(Server Side Include)的html文件扩展名 ...

  9. yii2 ActiveRecord常用用法

    User::find()->all();    返回所有数据   User::findOne($id);   返回 主键 id=1  的一条数据   User::find()->where ...

  10. php练习3——猜拳游戏,评委打分问题

    用户与计算机猜拳 程序caiQuan.html和caiQuan.php: 结果: 评委打分问题,去掉一个最低分和最高分,求平均分,并找出最低分和最高分对应第几个评委,    再找出最佳评委(打分最接近 ...