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... 这题就是利用二进制来标记颜色的种 ...
随机推荐
- 第3章 Struts2框架--2、完整的Struts2框架应用实例
1.建立一个Dynamic Web project,项目名:UserManager,把Struts2所必需的JAR复制到项目WEB-INF/lib目录下 2.修改web.xml文件,在web.xml文 ...
- (hdu)1022 Train Problem I 火车进站问题
题目链接:http://acm.split.hdu.edu.cn/showproblem.php?pid=1022 Problem Description As the new term comes, ...
- 选择第n小的元素之python实现源码
def partition(A, p, r): j = p+1 for i in range(p+1, r+1): if(A[i] < A[p]): tmp = A[i] A[i] = A[j] ...
- 控制器 - URL routing HTTP module(一)
URL routing HTTP module 负责处理检查入站请求的 URL,并将它们分派到最合理的处理器上.URL routing HTTP module 也替代了旧版本的 ASP.NET URL ...
- CALayer -- 备忘
CALayer layer是层,每个view上都会最少有一个layer,view上的可视化内容其实都是层. CALayer展示实例 let customView = UIView(frame: CGR ...
- 在ajax当中使用url重写来避免url的暴露
记得一次面试,有这样一道面试题:jsp页面当中需要用到ajax的实现,此时需要调用java的url:此时的问题是如果用户查看页面源码就能看到真是的url,这个问题如何避免.说实话,AJAX我用的只是皮 ...
- CSS禅意
标题取自<css禅意花园>一书,还记得当年读此书时的情景,真的是内容和书名一样的优秀,就以此标题作为自己在该文的一种追求吧,尽管我的水平和见解都和Dave Shea相去甚远.该文算是对前两 ...
- DM8168 debug continue... ...
1.boot VFS: Unable to mount root fs via NFS, trying floppy. VFS: Cannot open root device "n ...
- 关于Java(不同工具或平台与“Hello World”)
对于任何编程语言,都最常见的入门应用: Hello World NetBeans 和 “Hello World” 编写 Java 程序前,先要准备好: Java SE Development Kit ...
- python学习之---函数进阶
一,递归函数: 做程序应该都知道,在一个函数的内部还可以调用其它函数,这叫函数的调用,但是有一种特殊的情况,在一个函数内部对自身函数的调用,我们成这为函数的递归调用. 在此,使用一个家喻户晓的例子来演 ...