题目大意:要求完成以下两个操作:1.将一个区间刷上一种颜色2.询问一段区间上有多少种颜色

思路:这两个操作线段树都可以很迅速的完成,具体做法是:线段树上每个节点存这个线段上的颜色数量,由于颜色数很少,因此可以用二进制存颜色,如果二进制的第N位是1,则该区间存在颜色N,因此一个节点等于其两个子节点颜色的或。最后一个问题就是修改操作是对区间修改,因此需要用lazy_tag的思想(虽然这里不完全是),不然一个一个插节点会TLE

#include<cstdio>

#include<string.h>

#include<iostream>

using namespace std;

int tree[500000]={0};

bool lazy[500000]={0};

void swap(int &x,int &y){int temp=x;x=y;y=temp;}

int search(int node,int l,int r,int lq,int rq)

{

int mid=(l+r)>>1,temp=0;

if (lazy[node]==true){return tree[node];}

if (lq<=l && r<=rq){return tree[node];}

else

{

if (lazy[node]==1){lazy[node<<1]=1;tree[node<<1]=tree[node];

tree[(node<<1)+1]=tree[node];lazy[(node<<1)+1]=1;}

if (lq<=mid)temp=search(node<<1,l,mid,lq,rq);

if (mid<rq)temp=temp | search((node<<1)+1,mid+1,r,lq,rq);

}

return temp;

}

void insert(int node,int l,int r,int lq,int rq,int color)

{

int mid=(l+r)>>1;

if (lq<=l && r<=rq){tree[node]=1<<(color-1);lazy[node]=true;return;}

else

{

if (lazy[node]==1)

{

lazy[node]=0;lazy[node<<1]=1;

tree[node<<1]=tree[node];tree[(node<<1)+1]=tree[node];

lazy[(node<<1)+1]=1;

}

if (lq<=mid)insert(node<<1,l,mid,lq,rq,color);

if (rq>mid)insert((node<<1)+1,mid+1,r,lq,rq,color);

}

tree[node]=tree[node<<1] | tree[(node<<1)+1];

}

int f(int u)

{

int t=0;while(u!=0){if ((u & 1)==1)t++;u>>=1;}

return t;

}

int main()

{

int t,o,l,left,right,color;

char ch[2];

scanf("%d%d%d",&l,&t,&o);

for(int i=1;i<=4*l;i++)tree[i]=1;

for(int v=1;v<=o;v++)

{

scanf("%s",ch);

if (ch[0]=='C')

{

scanf("%d%d%d",&left,&right,&color);

if (right<left)swap(right,left);

insert(1,1,l,left,right,color);

}

else

{

scanf("%d%d",&left,&right);

if (right<left)swap(right,left);

int u=search(1,1,l,left,right);

printf("%d\n",f(u));

}

}

return 0;

}

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

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

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

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

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

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

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

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

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

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

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

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

  8. POJ 2777 Count Color(段树)

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

  9. poj 2777 Count Color

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

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

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

随机推荐

  1. qconbeijing2016

    http://2016.qconbeijing.com/schedule 大会日程 2016年04月21日 星期四 09:15 开场致辞 地点 1号厅   主题演讲 工程效率提升 业务核心架构 容器集 ...

  2. 【转】Android中实现IPC的几种方式详细分析及比较

    1.使用Bundle   ----> 用于android四大组件间的进程间通信android的四大组件都可使用Bundle传递数据  所以如果要实现四大组件间的进程间通信 完全可以使用Bundl ...

  3. CMSIS的简介

    Cortex微控制器软件接口标准(Cortex Microcontroller Software Interface Standard)是ARM和一些编译器厂家以及半导体厂家共同遵循的一套标准,是由A ...

  4. windows 查看某端口被占用情况

    百度经验 http://jingyan.baidu.com/article/3c48dd34491d47e10be358b8.html 基本命令 netstat -ano

  5. php中session实现机制

    一.默认机制,用磁盘文件来实现PHP会话.php.ini配置:session.save_handler = files 1.session_start() A. session_start()是ses ...

  6. std::map插入已存在的key时,key对应的内容不会被更新

    std::map插入已存在的key时,key对应的内容不会被更新,如果不知道这一点,可能会造成运行结果与预期的不一致 “Because element keys in a map are unique ...

  7. 【HEVC简介】ALF-Adative Loop Filter

    由于HEVC在HM4.0之后,就把ALF去掉,所以ALF的介绍是基于AVS2. <HEVC标准介绍.HEVC帧间预测论文笔记>系列博客,目录见:http://www.cnblogs.com ...

  8. nginx,php-fpm的安装配置

    在centos7.2的系统下安装nginx和php-fpm nginx 安装 yum install -y nginx 即可完成安装 配置 由于之前项目使用的是apache,所以项目目录在var/ww ...

  9. qt sql多重条件查询简便方法

    转载请注明出处:http://www.cnblogs.com/dachen408/p/7457312.html 程序设计过程中,经常要涉及到查询,并且有很多条件,且条件可为空,如果逐个判断,会有很多情 ...

  10. hdu5739Fantasia(多校第二场1006) 割点+逆元

    Fantasia Time Limit: 10000/5000 MS (Java/Others)    Memory Limit: 65536/65536 K (Java/Others) Proble ...