题目传送门:http://poj.org/problem?id=2777

Count Color
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 45259   Accepted: 13703

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 线段树对区间操作,简单题
给你一段L长的线让你染色,有T中颜色,共有O次命令,
命令分为
C:给x到y染成c色
P:看x到y一共有多少种颜色
 #include<stdio.h>
#include<string.h>
int cc[];
struct A{
int l,r,c;
}t[*+];
void bulid(int p,int l,int r)
{
t[p].l=l;t[p].r=r;t[p].c=;
if(l==r)
return ;
int mid=(t[p].l+t[p].r)>>;
bulid(p*,l,mid);
bulid(p*+,mid+,r);
}
void cha(int l,int r,int c,int p)
{
if(t[p].l==l&&t[p].r==r)
{
t[p].c=c;
return ;
}
if(t[p].c==c)
return ;
if(t[p].c!=-)
{
t[p*].c=t[p].c;
t[p*+].c=t[p].c;
t[p].c=-; }int mid=(t[p].l+t[p].r)>>;
if(r<=mid)
{
cha(l,r,c,p*);
}
else if(l>mid)
{
cha(l,r,c,p*+);
}
else
{
cha(l,mid,c,p*);
cha(mid+,r,c,p*+);
}
}
void se(int l,int r,int p)
{
if(t[p].c!=-)
{
cc[t[p].c]=;
// printf("!%d\n",t[p].c);
return ;
}
int mid=(t[p].l+t[p].r)>>;
if(r<=mid)
{
se(l,r,p*);
}
else if(l>mid)
{
se(l,r,p*+);
}
else
{
se(l,mid,p*);
se(mid+,r,p*+); }
}
int main()
{
int l,t,o,i,j,a,b,c;
char aa;
while(scanf("%d%d%d",&l,&t,&o)!=EOF)
{
bulid(,,l);
while(o--)
{
getchar();
aa=getchar(); if(aa=='C')
{
scanf("%d%d%d",&a,&b,&c);
if(a>b)
{
a=a^b;
b=a^b;
a=a^b;
}
cha(a,b,c,);
}
else if(aa=='P')
{
scanf("%d%d",&a,&b);
if(a>b)
{
a=a^b;
b=a^b;
a=a^b;
}
memset(cc,,sizeof(cc));
se(a,b,);
int sun=;
for(j=;j<=t;j++)
{
if(cc[j]==)
sun++;
} printf("%d\n",sun); }
} }
return ;
}

POJ2777-Count Color (线段树)的更多相关文章

  1. [poj2777] Count Color (线段树 + 位运算) (水题)

    发现自己越来越傻逼了.一道傻逼题搞了一晚上一直超时,凭啥子就我不能过??? 然后发现cin没关stdio同步... Description Chosen Problem Solving and Pro ...

  2. POJ2777 Count Color 线段树区间更新

    题目描写叙述: 长度为L个单位的画板,有T种不同的颜料.现要求按序做O个操作,操作分两种: 1."C A B C",即将A到B之间的区域涂上颜色C 2."P A B&qu ...

  3. Count Color(线段树+位运算 POJ2777)

    Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 39917 Accepted: 12037 Descrip ...

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

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

  5. poj 2777 Count Color(线段树)

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

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

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

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

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

  8. poj 2777 Count Color - 线段树 - 位运算优化

    Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 42472   Accepted: 12850 Description Cho ...

  9. POJ P2777 Count Color——线段树状态压缩

    Description Chosen Problem Solving and Program design as an optional course, you are required to sol ...

  10. POJ 2777 Count Color (线段树成段更新+二进制思维)

    题目链接:http://poj.org/problem?id=2777 题意是有L个单位长的画板,T种颜色,O个操作.画板初始化为颜色1.操作C讲l到r单位之间的颜色变为c,操作P查询l到r单位之间的 ...

随机推荐

  1. kohana task 编写计划任务

    kohana 框架  我们经常使用gleez作为我们二次开发. 收先我们要把文件建在Task文件夹下,比如新建文件为:testcron <?phpdefined('SYSPATH') or di ...

  2. Hash应用例子

    八.Hash应用例子搜索引擎会通过日志文件把用户每次检索使用的所有检索串都记录下来,每个查询串的长度为1-255字节.假设目前有一千万个记录(这些查询串的重复度比较高,虽然总数是1千万,但如果除去重复 ...

  3. MI04 盘点单数据录入

    *&---------------------------------------------------------------------* *& Report ZRHSSD002 ...

  4. 解决iOS第三方SDK之间重复的symbols问题

    前言:今天公司项目准备使用高德导航,其中用到了高德3D地图SDK,然后就出现bug了.在真机上可以完美运行,但是在模拟器上,就出现了一大片的bug:提示有82个Duplicate symbols,仔细 ...

  5. MySQL查询性能调优化

    一.索引的概念 索引:类似于字典的目录,设置索引可以 加速数据查找,对数据进行约束: 二.索引类型: 主键索引:保证数据唯一性,不能重复+不能为空 普通索引:加速数据查找 唯一索引:加速查找+不能重复 ...

  6. Mysql可重复读原理

    mysql可重复读现象及原理分析 InnoDB---可重复读隔离级别的底层实现原理 概念 可重复读的实现 Repeatable Read(可重复读):一个事务在执行过程中可以看到其他事务已经提交的新插 ...

  7. 开发Web应用(2)(二十一)

    在完成配置之后,举一个简单的例子,在快速入门工程的基础上,举一个简单的示例来通过Thymeleaf渲染一个页面. 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 1 ...

  8. 使用Swagger2构建强大的RESTful API文档(1)(二十二)

    由于Spring Boot能够快速开发.便捷部署等特性,相信有很大一部分Spring Boot的用户会用来构建RESTful API.而我们构建RESTful API的目的通常都是由于多终端的原因,这 ...

  9. linux系统管理 设置别名

    查看和设定别名 'alias' 查看系统中所有的命令别名 [root@ssgao1987 ~]# alias alias cp='cp -i' alias l.='ls -d .* --color=a ...

  10. Win10系列:C#应用控件基础1

    Button控件 在Windows应用商店应用的开发中,Button控件是使用比较频繁的控件之一,当用户单击Button控件时,会触发相应的单击事件并在定义好的事件处理方法中执行指定的功能.下面将介绍 ...