题目地址:http://poj.org/problem?id=2777

Count Color
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 30995   Accepted: 9285

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

【题解】:典型线段树区间更新

【code】:

 /**
result:Accepted memory:4264K
time:375MS language:C++
code lenght: 2147B Acmer:cj
*/ #include<iostream>
#include<stdio.h>
#include<string.h>
#include<algorithm> #define N 100010
#define lson p<<1
#define rson p<<1|1
using namespace std; struct Nod
{
int l,r;
int color; //颜色
int flag; //[l,r]区间是否同色
}node[N<<]; int mark[]; //颜色统计标记 void building(int l,int r,int p) //建树
{
node[p].l = l;
node[p].r = r;
node[p].color = ;
node[p].flag = ;
if(l==r) return;
int mid = (l+r)>>;
building(l,mid,lson);
building(mid+,r,rson);
} void update(int l,int r,int p,int c)
{
if(node[p].l==l&&node[p].r==r)
{
node[p].flag = ;
node[p].color = c;
return;
}
if(node[p].color!=c&&node[p].flag) //向下更新
{
node[p].flag = ;
node[lson].flag=node[rson].flag = ;
node[lson].color=node[rson].color = node[p].color;
}
int mid = (node[p].l+node[p].r)>>;
if(r<=mid) update(l,r,lson,c);
else if(l>mid) update(l,r,rson,c);
else
{
update(l,mid,lson,c);
update(mid+,r,rson,c);
}
if(node[lson].flag&&node[rson].flag&&node[lson].color==node[rson].color) //向上更新
{
node[p].flag = ;
node[p].color = node[lson].color;
}
} void query(int l,int r,int p)
{
if(node[p].flag) //区间同色
{
mark[node[p].color]=; //标记颜色出现过
return;
}
int mid = (node[p].l+node[p].r)>>;
if(r<=mid) query(l,r,lson);
else if(l>mid) query(l,r,rson);
else
{
query(l,mid,lson);
query(mid+,r,rson);
}
} int main()
{
int L,T,O;
scanf("%d%d%d",&L,&T,&O);
building(,L,);
while(O--)
{
char op[];
int a,b,c;
scanf("%s",op);
if(op[]=='C')
{
scanf("%d%d%d",&a,&b,&c);
update(a,b,,c);
}
else if(op[]=='P')
{
int i;
scanf("%d%d",&a,&b);
memset(mark,,sizeof(mark)); //初始化颜色标记
query(a,b,);
int ans = ;
for(i=;i<=T;i++) if(mark[i]) ans++;
printf("%d\n",ans);
}
}
return ;
}

poj 2777 Count Color(线段树)的更多相关文章

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

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

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

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

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

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

  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 题意是有L个单位长的画板,T种颜色,O个操作.画板初始化为颜色1.操作C讲l到r单位之间的颜色变为c,操作P查询l到r单位之间的 ...

  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(段树)

    职务地址:id=2777">POJ 2777 我去.. 延迟标记写错了.标记到了叶子节点上.. . . 这根本就没延迟嘛.. .怪不得一直TLE... 这题就是利用二进制来标记颜色的种 ...

  8. poj 2777 Count Color

    题目连接 http://poj.org/problem?id=2777 Count Color Description Chosen Problem Solving and Program desig ...

  9. POJ 2777 Count Color(线段树染色,二进制优化)

    Count Color Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 42940   Accepted: 13011 Des ...

随机推荐

  1. hdu 4605 线段树与二叉树遍历

    思路: 首先将所有的查询有一个vector保存起来.我们从1号点开始dfs这颗二叉树,用线段树记录到当前节点时,走左节点的有多少比要查询该节点的X值小的,有多少大的, 同样要记录走右节点的有多少比X小 ...

  2. 通过Bresenham算法实现完成矢量线性多边形向栅格数据的转化

    1.实验目的与要求 目的:通过本次实验,完成矢量线性多边形向栅格数据的转化过程: 要求:采用VC++6.0实现. 2.实验方法 采用Bresenham算法实现 3.实验材料 直线的定义:y = x/3 ...

  3. 核心概念 —— 服务提供者

    1.简介 服务提供者是Laravel应用启动的中心,你自己的应用以及所有Laravel的核心服务都是通过服务提供者启动. 但是,我们所谓的"启动"指的是什么?通常,这意味着注册事物 ...

  4. HDOJ2021发工资咯:)

    发工资咯:) Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 65536/32768 K (Java/Others)Total Subm ...

  5. Objective-C中一个方法如何传递多个参数的理解

    原来如此 Objective-C语法中多参数传递方法经常是初学者最容易犯困的地方.我自己也是刚刚悟出来与大家分享. 分析 由于我们已有的语言经验告诉我们定义方法都是: 一个类型匹配一个参数(动态语言甚 ...

  6. Android代码内存优化建议-OnTrimMemory优化

    原文  http://androidperformance.com/2015/07/20/Android代码内存优化建议-OnTrimMemory优化/ OnTrimMemory 回调是 Androi ...

  7. Setup Project 安装项目

    从vs2012起,微软已经不支持setup project了.以此纪念一下setup project.   在新建Setup Project   增加安装内容,通常是直接Oupput一个项目,或者直接 ...

  8. mouseover,mouseout,mouseenter,mouseleave的区别

    相信做前端开发的都听说过“冒泡型事件”吧,<JavaScript高级程序设计>第九章有详细的讲述,但是,在学习的时候一知半解,也没详细去理解,导致最近在工作中碰到了问题:有许多 li 标签 ...

  9. jquery实现视觉滚动--fullpage

    用fullpage.js实现视觉滚动效果 1.Html页面 <!DOCTYPE html> <html> <head> <meta charset=" ...

  10. 小米2s换了屏幕后不能近距离对焦,拆过后无法对焦?

    主要问题就是小米2/2S手机中壳套后摄像头的位置,里面还有一个正方形的黑色塑胶垫片,一般拆机后这个垫片是不会掉出来的,所以一般上盖时也是直接把中壳合上后上螺丝. 这样安装基本会导致塑胶垫片把摄像头顶住 ...