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... 这题就是利用二进制来标记颜色的种 ...
随机推荐
- 摘录android工具类
import android.content.Context; import android.content.pm.PackageInfo; import android.content.pm.Pac ...
- xfire构建webservice项目步骤以及使用
简单搭建xfire开源软件的webservice开发及其步骤: 1.创建好一个web工程,引入xfire下的jar包,注意lib下的和xfire-all.jar 2.定义接口: package com ...
- DAG模型——硬币问题
硬币问题 有n种硬币,面值分别为V1,V2,...,Vn,每种都有无限多.给定非负整数S,可以选用多少个硬币,使得面值之和恰好为S?输出硬币数目的最小值和最大值.1<=n<=100, 0& ...
- [翻译][MVC 5 + EF 6] 1:创建数据模型
原文:Getting Started with Entity Framework 6 Code First using MVC 5 1.新建MVC项目: 2.修改Views\Shared\_Layou ...
- 读取xml文件(可执行文件根目录debug)
xml文件格式如下 <?xml version="1.0" encoding="utf-8" ?> <root> <appKey& ...
- js 中用Dom2级事件处理函数(改变样式)
下面这些客户端 javascript代码用到了事件,它给一个很重要的事件--“load" 事件注册了一个事件处理程序.同时展示了注册”click“事件处理函数更高级的一种方法 <!do ...
- shell脚本修复MySQL主从同步
发布:thebaby 来源:net [大 中 小] 分享一例shell脚本,用于修改mysql的主从同步问题,有需要的朋友参考下吧. 一个可以修改mysql主从同步的shell脚本. 例子 ...
- Instructions Set JAVA_HOME System-Wide
Instructions Set JAVA_HOME System-Wide 1 Start a root terminal session and then change directories t ...
- 3D 服务器端以向量计算为主的角色位置的算法
把我以前学习过的一个东西拿出来分享下~ 3D服务器端玩家行走处理是服务器端根据客户端行走路径.玩家行走时间以及速度(包括变化速度)计算得出玩家的当前位置. 由于客户端行走是一条路径,不使用2D中的格子 ...
- Computer Talker with C# z
Using the Code Add a textbox named 'txtWords' to a form. Add a button named 'btnSpeak' to a form. Ad ...