题目传送门: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. TLS与SSL之间关系——SSL已经被IEFT组织废弃,你可以简单认为TLS是SSL的加强版

    TLS与SSL之间关系 原文地址:SSL vs. TLS - What's the Difference? from:https://juejin.im/post/5b213a0ae51d4506d4 ...

  2. python3.7 安装

    python3.7 安装 下载安装 cd /usr/localwget https://www.python.org/ftp/python/3.7.2/Python-3.7.2.tgztar -xvf ...

  3. [IOS微信] PList文件解析,boost数据读取

    最近在解析IOS版微信数据中的 mmsetting.archive 文件时,第一次接触到PList文件. 注:mmsetting.archive  不是一个标准的PList文件,其中含有汉字,并且很多 ...

  4. js图片时间和倒计时

    图片时间原理        原理:使用定时器每秒获取时间,获取时间的时,分,秒,组成一个6位数的字符串,然后用charAt,截取出对应的字符,图片命名和数字相对应就ok拉 倒计时原理        原 ...

  5. UVa LA 3695 - Distant Galaxy 前缀和,状态拆分,动态规划 难度: 2

    题目 https://icpcarchive.ecs.baylor.edu/index.php?option=com_onlinejudge&Itemid=8&page=show_pr ...

  6. OOP⑺

    1.多态和instanceof 都是去买东西,但是根据我们给别人金额的不同,得到不同的结果!!!! 生活中的多态! 操作是否一致? 一致! 都是买东西! 什么不一样?? 01.消费金额不一样 02.因 ...

  7. HTML中元素的position属性详解

    HTML中元素的position属性详解 转载自:https://blog.csdn.net/wangzunkuan/article/details/81540935   HTML中DOM元素有5种定 ...

  8. 前端vue项目-关于下载文件pdf/excel(三)

    最近在做一些需求,需要下载一些文件信息,最频繁的就是下载excel文件到本地了 看过了很多方法,做个整理吧哈哈哈哈 参考的文章链接: https://www.cnblogs.com/jiangweic ...

  9. flask项目结构(一)mariadb

    简介: 本文主要是根据自己所学,创建一个flask项目,使用sqlalchemy,alembic,mariadb,bootstrap,APScheduler,selenium,request…………技 ...

  10. ppt点击文字出现图片,再次点击消失

    实现效果:在PPT一个页面的任意位置,单击左键,出现图片:在图片上,单击左键,图片消失 实现思路:给图片做两个动画,一个进入,文字作触发器,另一个退出,图片本身为触发器. 实现方法: 1.选中图片…… ...