POJ 2777 Count Color (线段树)

 
Count Color
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 29895   Accepted: 8919

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

Source

 
 

分析 一个有n(n<100000)个单位的画板,但颜色只有30种,我没有马上想到位运算,但后来和一个宋词学仙交流的时候她告诉了我可以用位,不过她也是先用的布尔数组来判断的,可能会TLE
这个线段树的提就比较标准了,我先前写的时候一直没有想到好的延时更新的方法,最后慢慢想了一下午和一晚上终于弄对了
AC代码
 #include<iostream>
#include<cstdio>
#define maxx 100005
#define L(u) (u<<1)
#define R(u) (u<<1|1)
using namespace std;
int n,t,Q;
int result,ans;
struct xx{
int ls,rs;
int clr;
int sta;
}node[maxx<<];
void pushup(int u)
{
node[u].clr=node[R(u)].clr|node[L(u)].clr;
}
void pushdown(int u)
{
node[u].sta=;
node[R(u)].clr=node[u].clr;
node[L(u)].clr=node[u].clr;
if(node[L(u)].ls!=node[L(u)].rs)
node[L(u)].sta=;
if(node[R(u)].ls!=node[R(u)].rs)
node[R(u)].sta=;
}
void Build(int u,int l,int r)
{
node[u].ls=l;
node[u].rs=r;
node[u].clr=;
if(l==r)
return;
int mid=(l+r)>>;
Build(L(u),l,mid);
Build(R(u),mid+,r);
}
void Updata(int u,int l,int r,int c)
{
if(l==node[u].ls&&node[u].rs==r)
{
node[u].clr=(<<c);
if(node[u].ls!=node[u].rs)node[u].sta=;
return;
}
if(node[u].sta)pushdown(u); //下传是必要的
int mid=(node[u].ls+node[u].rs)>>;
if(r<=mid) Updata(L(u),l,r,c);
else if(l>mid) Updata(R(u),l,r,c);
else{
Updata(L(u),l,mid,c);
Updata(R(u),mid+,r,c);
}
pushup(u);
}
void Qurey(int u,int l,int r)
{
if(l==node[u].ls&&node[u].rs==r)
{
result=result|node[u].clr;
return;
}
if(node[u].sta)pushdown(u); //下传是必要的
int mid=(node[u].ls+node[u].rs)>>;
if(r<=mid) Qurey(L(u),l,r);
else if(l>mid) Qurey(R(u),l,r);
else{
Qurey(L(u),l,mid);
Qurey(R(u),mid+,r);
}
}
int main()
{
while(~scanf("%d %d %d\n",&n,&t,&Q))
{
Build(,,n);
while(Q--)
{
char x;
int a,b,c;
scanf("%c",&x);
if(x=='P')
{
result=,ans=;
scanf("%d %d\n",&a,&b);
if(a>b)
{a=a+b;b=a-b;a=a-b;}
Qurey(,a,b);
while(result)
{
result>>=;
if(&result)ans++;
}
cout<<ans<<endl;
}
else
{
scanf("%d %d %d\n",&a,&b,&c);
if(a>b)
{a=a+b;b=a-b;a=a-b;}//注意查询的区间可能左大于右数据怪怪的
Updata(,a,b,c);
}
}
}
return ;
}

Count Colour_poj2777(线段树+位)的更多相关文章

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

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

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

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

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

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

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

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

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

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

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

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

  7. poj 3225 线段树+位运算

    略复杂的一道题,首先要处理开闭区间问题,扩大两倍即可,注意输入最后要\n,初始化不能随便memset 采用线段树,对线段区间进行0,1标记表示该区间是否包含在s内U T S ← S ∪ T 即将[l, ...

  8. poj 2777 Count Color(线段树)

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

  9. Subsequence Count (线段树)

    Time Limit: 1000 ms   Memory Limit: 256 MB Description 给定一个01串 $S_{1 \cdots n}$ 和 $Q$ 个操作. 操作有两种类型: ...

随机推荐

  1. metasploit升级(BT5)

    1.apt-get update 2.apt-get install metasploit 3.修改文件:/opt/metasploit/ruby/lib/ruby/1.9.1/i686-linux/ ...

  2. C# 正则表达式及返回匹配的所有结果

    C# 正则表达式是在  System.Text.RegularExpressions 空间定义的,其中的 IsMatch() 方法用于返回 bool 类型,表示要搜索的字符串与传入的正则表达式是否匹配 ...

  3. Activity

    activity的生命周期: 第一个是:点击按钮切换到另一个activity界面. 第二个是:单击物理返回键的时候,是对当前的activity进行销毁动作. Activity的启动方式:直接启动和匿名 ...

  4. 百度地图API示例之添加/删除工具条、比例尺控件

    代码 <!DOCTYPE html> <html> <head> <meta http-equiv="Content-Type" cont ...

  5. PHP时间日期

    PHP常用的几个时间日期函数有:date(),mktime(),strtotime(); 一.string date ( string $format [, int $timestamp ] ) 函数 ...

  6. Spring day02笔记

    spring day01回顾 编写流程(基于xml) 1.导入jar包:4+1 --> beans/core/context/expression | commons-logging 2.编写目 ...

  7. {CSDN}{英雄会}{反相互}

    思路: 给定一个字符串,求两个不重叠的字串,他们翻转互补.其中一个字符串可以是删掉最多两个字符的原字符串子串. 动态规划,由于可以对子串进行删除操作,我首先想到了LCS问题,但需要枚举所有的长度,这样 ...

  8. Intel 82599 万兆网卡

    http://www.cnblogs.com/zhuyp1015/archive/2012/08/23/2653264.html http://bbs.chinaunix.net/thread-424 ...

  9. perl中读取外部文件

    打开一个在电脑G盘111文件下的一个文件 #!/usr/bin/perl -w  use strict; open(IN,"G:/111/mylove.txt"); while($ ...

  10. 四则运算(Android)版

    实验题目: 将小学四则运算整合成网页版或者是Android版.实现有无余数,减法有无负数.... 设计思路: 由于学到的基础知识不足,只能设计简单的加减乘除,界面设计简单,代码量少,只是达到了入门级的 ...