这道题对于我这样的初学者还是有点难度的不过2遍A了还是很开心,下面说说想法……

Count Color

Time Limit: 1000MS Memory Limit: 65536K

Total Submissions: 40302 Accepted: 12161

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(1<=L<=100000)颜色共有T种(1<=T<=30)给你O条操作(1<=O<=100000)操作分两种

(1)“C A B C”操作C:将【A,B】染成C色(开始时,画板都是颜色1)

(2)“P A B”操作P:【A,B】范围内颜色种数

(此题有个蛋疼的地方,读入的AB可能顺序颠倒...也是略坑)
这道题用位移来做,如果染成t种颜色,就把颜色左移t-1位,位移运算在这道题里完美展现,所以以后还是得注重这种表示方法,了解多了,自然做题顺畅
用二进制表示对应的区间涂了第几种颜色,这样每个区间除了延迟标记外,可以再开一个数组统计当前涂了哪几种颜色。这样就和一般的线段树一样了。
(这种思想值得学习)

最后再统计一下1的数目

下面是代码:

//这波位移非常的nice啊!
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
using namespace std;
#define maxl 100001
int color[maxl<<2]={0},sum[maxl<<2]={0}; void updata(int now)
{
sum[now]=sum[now<<1]|sum[now<<1|1];
}//or void pushdown(int now)
{
if (color[now]!=0)
{
color[now<<1]=color[now<<1|1]=color[now];
sum[now<<1]=color[now];
sum[now<<1|1]=color[now];
color[now]=0;
}
} void build(int l,int r,int now)
{
color[now]=1;
if (l==r) return;
int mid=(l+r)>>1;
build(l,mid,now<<1);
build(mid+1,r,now<<1|1);
updata(now);
} void change(int L,int R,int l,int r,int now,int newcolor)
{
if (L<=l && R>=r)
{
color[now]=1<<(newcolor-1);
sum[now]=color[now];
return; }//位移表示颜色
int mid=(l+r)>>1;
pushdown(now);
if (L<=mid)
change(L,R,l,mid,now<<1,newcolor);
if (R>mid)
change(L,R,mid+1,r,now<<1|1,newcolor);
updata(now);
} int query(int L,int R,int l,int r,int now)
{
if (L<=l && R>=r)
return sum[now];
pushdown(now);
int mid=(l+r)>>1;
int ans=0;
if (L<=mid)
ans=ans|query(L,R,l,mid,now<<1);
if (R>mid)
ans=ans|query(L,R,mid+1,r,now<<1|1);//要对结果取or
return ans;
} int main()
{
int l,t,o;
while(~scanf("%d%d%d",&l,&t,&o))
{
build(1,l,1);
for (int i=1; i<=o; i++)
{
char command[2];
int left,right,data;
scanf("%s",&command);
if (command[0]=='C')
{
scanf("%d%d%d",&left,&right,&data);
if (left>right)
{
int temp=left;
left=right;
right=temp;
}
change(left,right,1,l,1,data);
}
else
{
scanf("%d%d",&left,&right);
if (left>right)
{
int temp=left;
left=right;
right=temp;
}
int ans=query(left,right,1,l,1);
int answer=0;
while (ans>0)
{
if (ans & 1)
answer++;
ans=ans>>1;
}//这里表示颜色数
printf("%d\n",answer);
}
}
printf("\n");
}
return 0; }

POJ-2777Count Color 线段树+位移的更多相关文章

  1. POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化)

    POJ.2299 Ultra-QuickSort (线段树 单点更新 区间求和 逆序对 离散化) 题意分析 前置技能 线段树求逆序对 离散化 线段树求逆序对已经说过了,具体方法请看这里 离散化 有些数 ...

  2. Buy Tickets POJ - 2828 思维+线段树

    Buy Tickets POJ - 2828 思维+线段树 题意 是说有n个人买票,但是呢这n个人都会去插队,问最后的队列是什么情况.插队的输入是两个数,第一个是前面有多少人,第二个是这个人的编号,最 ...

  3. poj 2777 Count Color(线段树)

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

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

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

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

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

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

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

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

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

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

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

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

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

随机推荐

  1. 给vs2010安装上cocos2d-x的模版

    开发环境:OS(WINDOWS 8.1 X64 企业版) cocos2d-x 2.2.1  vs2010 想给vs安装上cocos的模版,执行InstallWizardForVS2010.js,老是提 ...

  2. Unity脚本生命周期

    前言 说到生命周期,影响最深刻的是,在接触Java的JSF组件时,JSF组件的五大生命周期,全要默写出来,嘿嘿…… 总结这两天在写小怪和掉落的糖葫芦时,老是遇到GameObject未销毁,一直存在场景 ...

  3. Android 手势识别类 ( 二 ) GestureDetector 源码浅析

    前言:Android 关于手势的操作提供两种形式:一种是针对用户手指在屏幕上划出的动作而进行移动的检测,这些手势的检测通过android提供的监听器来实现:另一种是用 户手指在屏幕上滑动而形成一定的不 ...

  4. Nginx采用https加密访问后出现的问题

    线上的一个网站运行了一段时间,应领导要求,将其访问方式更改为https加密方式.更改为https后,网站访问正常,但网站注册功能不能正常使用了! 经过排查,是nginx配置里结合php部分漏洞了一个参 ...

  5. 【夯实Mysql基础】MySQL在Linux系统下配置文件及日志详解

    本文地址 分享提纲: 1. 概述 2. 详解配置文件 3. 详解日志 1.概述 MySQL配置文件在Windows下叫my.ini,在MySQL的安装根目录下:在Linux下叫my.cnf,该文件位于 ...

  6. 墙国内新建Rails应用的要点(windows 7环境, Rails 4.2.0)

    1. 使用rails new 命令创建完的应用在自动执行bundle install不会成功,根据出错提示,判断原因有可能是被墙与https的证书的安全性问题. 作为开发环境,选用绕开的办法,在目录  ...

  7. StaticFileMiddleware中间件如何处理针对文件请求

    StaticFileMiddleware中间件如何处理针对文件请求 我们通过<以Web的形式发布静态文件>和<条件请求与区间请求>中的实例演示,以及上面针对条件请求和区间请求的 ...

  8. [TCPIP]代理arp

    一 理论概述 \ 二 实验 实验一:代理arp在nat中的作用(实验发现一下是错的)     实验二.代理arp pc访问服务器想让走路由器(写32bit静态路由),右边的R arp server的时 ...

  9. [CareerCup] 10.6 Find Duplicate URLs 找重复的URL链接

    10.6 You have 10 billion URLs. How do you detect the duplicate documents? In this case, assume that ...

  10. 实验二 Linux下C语言编程基础

    1. 熟悉Linux系统下的开发环境 2. 熟悉vi的基本操作 3. 熟悉gcc编译器的基本原理 4. 熟练使用gcc编译器的常用选项 5 .熟练使用gdb调试技术 6. 熟悉makefile基本原理 ...