Count Color POJ--2777
Time Limit: 1000MS | Memory Limit: 65536K | |
Total Submissions: 32217 | Accepted: 9681 |
Description
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
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
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的更多相关文章
- Count Color POJ - 2777 线段树
Chosen Problem Solving and Program design as an optional course, you are required to solve all kinds ...
- POJ 2777 Count Color(线段树染色,二进制优化)
Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 42940 Accepted: 13011 Des ...
- poj 2777 Count Color
题目连接 http://poj.org/problem?id=2777 Count Color Description Chosen Problem Solving and Program desig ...
- poj 2777 Count Color(线段树)
题目地址:http://poj.org/problem?id=2777 Count Color Time Limit: 1000MS Memory Limit: 65536K Total Subm ...
- poj 2777 Count Color(线段树区区+染色问题)
题目链接: poj 2777 Count Color 题目大意: 给出一块长度为n的板,区间范围[1,n],和m种染料 k次操作,C a b c 把区间[a,b]涂为c色,P a b 查 ...
- POJ - 2777——Count Color(懒标记线段树二进制)
Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 53639 Accepted: 16153 Des ...
- 【POJ 2777】 Count Color(线段树区间更新与查询)
[POJ 2777] Count Color(线段树区间更新与查询) Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4094 ...
- poj 2777 Count Color(线段树、状态压缩、位运算)
Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 38921 Accepted: 11696 Des ...
- POJ 2777 Count Color(线段树之成段更新)
Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 33311 Accepted: 10058 Descrip ...
- POJ 2777 Count Color(段树)
职务地址:id=2777">POJ 2777 我去.. 延迟标记写错了.标记到了叶子节点上.. . . 这根本就没延迟嘛.. .怪不得一直TLE... 这题就是利用二进制来标记颜色的种 ...
随机推荐
- java新手笔记2 数据类型
1.注释 /** doc注释 * 类说明信息 */ //声明类 文件名与类名一致 public class World {//类定界符 //声明方法 main方法 public static void ...
- References & the Copy-Constructor
1 There are certain rules when using references: (Page 451) A reference must be initialized when it ...
- React组件一
<div id='test'></div> <script type='text/babel'> var Zu=React.createClass({ return ...
- php验证复选框有效性的示例
本文介绍一个简单的php通过代码验证复选框值的有效性,有需要的可以参考一下 验证复选框的php代码,如下: 复制代码代码如下: <?php /** * 在php中验证复选框的有效性 * ...
- 互联网HTTP连接等出错代码大全
100 - Continue 101 - Switching Protocols Success Codes 200 - OK 201 - Created 202 - Accepted 20 ...
- WPF 得一些问题汇总
1.CallMethodAction <TextBox Height="30" Name="txtUserName" Width="160&qu ...
- 使用dynamic来简化反射实现
dynamic是Framework4.0的新特性,dynamic的出现让C#具有了弱语言类型的特性,编译器在编译的时候,不再对类型进行检查,不会报错,但是运行时如果执行的是不存在的属性或者方法,运行程 ...
- 禁止生产pyc
sys.dont_write_bytecode = 1 来自为知笔记(Wiz)
- POJ 1699 Best Sequence dfs
题目: http://poj.org/problem?id=1699 无意间A了..超时一次,加了一句 if(len > ans)return; 然后就A了,dfs题,没有太多好说的,代码写的效 ...
- 15个Docker基本命令及用法
Docker入门教程:15个Docker基本命令及用法 本文中,我们将学习15个Docker命令以及命令的用法和功能,并通过实践学习它是如何工作的. AD:51CTO 网+ 第十二期沙龙:大话数据 ...