题意:

给一个固定长度为L的画板

有两个操作:

C A B C:区间AB内涂上颜色C。

P A B:查询区间AB内颜色种类数。

分析:显然是要用线段树来操作的,设定一个sum[]来维护一个区间内的颜色,若为-1,则说明不是叶子节点,还应在往底下找;

同时在查询时维护一个vis[]来记录在这个区间内哪个颜色被使用过了。

#include<cstdio>
#include<iostream>
#include<cstring>
#define lson l,m,now*2
#define rson m+1,r,now*2+1
#define M 400010
#define N 35
using namespace std;
int vis[N],sum[M];//当sum[i]=-1是,说明不是底下至少有2种颜色,否则sum[i]是i的颜色
void build(int l,int r,int now)
{
sum[now]=;
if(l==r)return;
int m=(l+r)/;
build(lson);
build(rson);
}
void push_down(int now)
{
sum[now*]=sum[now];
sum[now*+]=sum[now];
sum[now]=-;
}
void change(int x,int y,int v,int l,int r,int now)
{
if(l>=x&&r<=y)
{
sum[now]=v;
return;
}
if(sum[now]==v)return;
if(sum[now]!=-)push_down(now);
int m=(l+r)/;
if(x<=m)change(x,y,v,lson);
if(y>m)change(x,y,v,rson);
}
void query(int x,int y,int l,int r,int now)
{
if(sum[now]!=-)
{
vis[sum[now]]=;
return;
}
int m=(l+r)/;
if(x<=m)query(x,y,lson);
if(y>m)query(x,y,rson);
}
int main()
{
int n,m,p;
scanf("%d%d%d",&n,&m,&p);
build(,n,);
for(int i=;i<=p;i++)
{
char c;
cin>>c;
if(c=='C')
{
int a,b,v;
scanf("%d%d%d",&a,&b,&v);
if(a>b)swap(a,b);
change(a,b,v,,n,);
}
else
{
memset(vis,,sizeof(vis));
int a,b,tot=;
scanf("%d%d",&a,&b);
if(a>b)swap(a,b);
query(a,b,,n,);
for(int i=;i<=;i++)
if(vis[i])tot++;
printf("%d\n",tot);
}
}
return ;
}

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

  1. [POJ2777]Count Color(线段树)

    题目链接:http://poj.org/problem?id=2777 给你一个长为L想线段,向上面染色,颜色不超过30种,一共有O次操作,操作有两种: C a b c 在[a,b]上染上c颜色 P ...

  2. 【POJ2777】Count Color(线段树)

    以下是题目大意: 有水平方向上很多块板子拼成的墙,一开始每一块都被涂成了颜色1,有C和P两个操作,代表的意思是:C X Y Z —— 从X到Y将板子涂成颜色ZP X Y    —— 查询X到Y的板子共 ...

  3. 【POJ 2777】 Count Color(线段树区间更新与查询)

    [POJ 2777] Count Color(线段树区间更新与查询) Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 4094 ...

  4. PAT 甲级 1054 The Dominant Color (20 分)

    1054 The Dominant Color (20 分) Behind the scenes in the computer's memory, color is always talked ab ...

  5. pat 1054 The Dominant Color(20 分)

    1054 The Dominant Color(20 分) Behind the scenes in the computer's memory, color is always talked abo ...

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

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

  7. POJ - 2777——Count Color(懒标记线段树二进制)

    Count Color Time Limit: 1000MS   Memory Limit: 65536K Total Submissions: 53639   Accepted: 16153 Des ...

  8. POJ 2777 Count Color(线段树 + 染色问题)

    传送门:Count Color Description Chosen Problem Solving and Program design as an optional course, you are ...

  9. POJ-2777 Count Color(线段树,区间染色问题)

    Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 40510 Accepted: 12215 Descrip ...

随机推荐

  1. 分享一段Java搞笑的代码注释

    今天在群里看到有人分享了一段搞笑的注释代码,觉得挺好玩的,在这里收藏一下 // _ooOoo_ // o8888888o // 88" . "88 // (| -_- |) // ...

  2. MySQL里面的子查询实例

    一,子选择基本用法 1,子选择的定义 子迭择允许把一个查询嵌套在另一个查询当中.比如说:一个考试记分项目把考试事件分为考试(T)和测验(Q)两种情形.下面这个查询就能只找出学生们的考试成绩 selec ...

  3. yield return 和 yield break

    //yield return 返回类型必须为 IEnumerable.IEnumerable<T>.IEnumerator 或 IEnumerator<T>. static I ...

  4. linux(Debian)下安装与MySql的安装、卸载、配置及使用

    参考资料:http://www.cnblogs.com/xusir/p/3334217.html 以下是简要记录. 一.安装 安装:apt-get install mysql-server mysql ...

  5. Python socket编程之六:多窗口的应用

    import struct import sqlalchemy import pandas import matplotlib.pyplot as Plot from matplotlib.finan ...

  6. TCP/IP协议栈概述

    TCP/IP协议栈概述 这篇文章虽然只是很粗浅的介绍了ISO/OSI 网络模型,但确实把握住了关键点,某种意义上,简单回顾一下就可以加深对TCP/IP协议栈的理解. 原作者:阮一峰 链接: http: ...

  7. WIN 2003服务器终极安全及问题解决方案

    一.硬盘分区与操 作系统的安装硬盘分区 总的来讲在硬盘分区上面没什么值得深入剖析的地方,无非就是一个在分区前做好规划知道要去放些什么东西, 如果实在不知 道.那就只一个硬盘只分一个区,分区要一次性完成 ...

  8. JS 省,市,区

    // 纯JS省市区三级联动 // 2011-11-30 by http://www.cnblogs.com/zjfree var addressInit = function (_cmbProvinc ...

  9. eclipse-统计代码行数

    使用Eclipse可以方便的统计工程或文件的代码行数,方法如下: 1.点击要统计的项目或许文件夹,在菜单栏点击Search,然后点击File...  2.选中正则表达式(Regular express ...

  10. jquery基础总结

    什么是jQuery?        就是一个JavaScript函数库,开源的.jQuery能做什么        JavaScript是做什么的,jQuery就是做什么的,Jquery是对javas ...