题目传送门: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. leetcode-algorithms-25 Reverse Nodes in k-Group

    leetcode-algorithms-25 Reverse Nodes in k-Group Given a linked list, reverse the nodes of a linked l ...

  2. 牛客练习赛24-B-贪心

    链接:https://www.nowcoder.com/acm/contest/157/B来源:牛客网 题目描述 凤凰于飞,翙翙其羽,亦集爰止.                             ...

  3. json 常用的序列化 反序列化对象 代码

    序列化对象: ---------------------------------------------------------- Person p = new Person() { Name = & ...

  4. 九校模拟——餐馆(restaurant)

    1 餐馆(restaurant) 1.1 题目背景 铜企鹅是企鹅餐馆的老板,他正在计划如何使得自己本年度收益增加. 1.2 题目描述 共有n 种食材,一份食材i 需要花ti 小时不间断地进行播种,施肥 ...

  5. ElasticSearch安装部署(Windows)

    测试版本:elasticsearch-5.1.1 1.解压elasticsearch-5.1.1.zip. 2.执行elasticsearch.bat启动服务,启动画面如下: 3.访问ElasticS ...

  6. python 关闭垃圾回收

    import gc gc.disable() http://blog.csdn.net/aixiaohei/article/details/6446869

  7. 转 cousera computational neuroscience week5 学习笔记(part 1)

    (2013-08-14 14:58:41) 转载▼ 标签: 学习笔记 it 很久没有写博文了,之所以重新写还是因为看了coursera的computational neuroscience之后,发现这 ...

  8. 【SQL】group by 及 having

    Group By 分组汇总 HAVING:给分组设置条件 1.概述 “Group By”从字面意义上理解就是根据“By”指定的规则对数据进行分组,所谓的分组就是将一个“数据集”划分成若干个“小区域”, ...

  9. Weka里如何将arff文件或csv文件批量导入MySQL数据库(六)

    这里不多说,直接上干货! 前提博客是 Weka中数据挖掘与机器学习系列之数据格式ARFF和CSV文件格式之间的转换(四) 1.将arff文件批量导入MySQL数据库 我在这里,arff文件以Weka安 ...

  10. linux网络操作 netstat命令

    关闭与启动网卡 ifdown 网卡设备名 #禁用该网卡设备 ifup网卡设备名 #启用该网卡设备 查看网络状态 netstat  命令 ​ -t 列出tcp协议端口 -u 列出udp协议端口 -n 不 ...