解题报告

题意:

对线段染色。询问线段区间的颜色种数。

思路:

本来直接在线段树上染色,lz标记颜色。每次查询的话訪问线段树,求出颜色种数。结果超时了,最坏的情况下,染色能够染到叶子节点。

换成存下区间的颜色种数,这样每次查询就不用找到叶子节点了。用按位或来处理颜色种数。

#include <iostream>
#include <cstdio>
#include <cstring>
#include <algorithm>
using namespace std;
int sum[500000],lz[500000],ans;
void push_down(int rt,int l,int r)
{
if(lz[rt])
{
lz[rt<<1]=lz[rt<<1|1]=lz[rt];
sum[rt<<1]=lz[rt];
sum[rt<<1|1]=lz[rt];
lz[rt]=0;
}
}
void push_up(int rt,int l,int r)
{
sum[rt]=sum[rt<<1]|sum[rt<<1|1];
}
void cbtree(int rt,int l,int r)
{
if(l==r)
{
sum[rt]=1;
return ;
}
int mid=(l+r)>>1;
cbtree(rt<<1,l,mid);
cbtree(rt<<1|1,mid+1,r);
push_up(rt,l,r);
}
void update(int rt,int l,int r,int ql,int qr,int v)
{
if(ql>r||qr<l)return ;
if(ql<=l&&r<=qr)
{
lz[rt]=v;
sum[rt]=v;
return ;
}
int mid=(l+r)>>1;
push_down(rt,l,r);
update(rt<<1,l,mid,ql,qr,v);
update(rt<<1|1,mid+1,r,ql,qr,v);
push_up(rt,l,r);
}
int _q(int rt,int l,int r,int ql,int qr)
{
if(ql>r||qr<l)return 0;
if(ql<=l&&r<=qr)
{
return sum[rt];
}
push_down(rt,l,r);
int mid=(l+r)>>1;
return _q(rt<<1,l,mid,ql,qr) | _q(rt<<1|1,mid+1,r,ql,qr); }
int main()
{
int n,t,q,ql,qr,i,j,a,k;
char str[10];
scanf("%d%d%d",&n,&t,&q);
cbtree(1,1,n);
while(q--)
{
scanf("%s",str);
if(str[0]=='C')
{
scanf("%d%d%d",&ql,&qr,&a);
if(ql>qr)swap(ql,qr);
update(1,1,n,ql,qr,1<<(a-1));
}
else
{
scanf("%d%d",&ql,&qr);
if(ql>qr)swap(ql,qr);
ans=_q(1,1,n,ql,qr);
int cnt=0;
while(ans)
{
if(ans%2)
cnt++;
ans/=2;
}
printf("%d\n",cnt);
}
}
return 0;
}
Count Color
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 35143   Accepted: 10591

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

POJ训练计划2777_Count Color(线段树/成段更新/区间染色)的更多相关文章

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

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

  2. HDU 3577 Fast Arrangement ( 线段树 成段更新 区间最值 区间最大覆盖次数 )

    线段树成段更新+区间最值. 注意某人的乘车区间是[a, b-1],因为他在b站就下车了. #include <cstdio> #include <cstring> #inclu ...

  3. poj 3669 线段树成段更新+区间合并

    添加 lsum[ ] , rsum[ ] , msum[ ] 来记录从左到右的区间,从右到左的区间和最大的区间: #include<stdio.h> #define lson l,m,rt ...

  4. poj 3468 A Simple Problem with Integers 【线段树-成段更新】

    题目:id=3468" target="_blank">poj 3468 A Simple Problem with Integers 题意:给出n个数.两种操作 ...

  5. 线段树(成段更新) POJ 3468 A Simple Problem with Integers

    题目传送门 /* 线段树-成段更新:裸题,成段增减,区间求和 注意:开long long:) */ #include <cstdio> #include <iostream> ...

  6. POJ 3468 A Simple Problem with Integers(线段树 成段增减+区间求和)

    A Simple Problem with Integers [题目链接]A Simple Problem with Integers [题目类型]线段树 成段增减+区间求和 &题解: 线段树 ...

  7. poj 3648 线段树成段更新

    线段树成段更新需要用到延迟标记(或者说懒惰标记),简单来说就是每次更新的时候不要更新到底,用延迟标记使得更新延迟到下次需要更新or询问到的时候.延迟标记的意思是:这个区间的左右儿子都需要被更新,但是当 ...

  8. ZOJ 1610 Count the Colors (线段树成段更新)

    题意 : 给出 n 个染色操作,问你到最后区间上能看见的各个颜色所拥有的区间块有多少个 分析 : 使用线段树成段更新然后再暴力查询总区间的颜色信息即可,这里需要注意的是给区间染色,而不是给点染色,所以 ...

  9. ACM: Copying Data 线段树-成段更新-解题报告

    Copying Data Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Description W ...

随机推荐

  1. c++11 : range-based for loop

    0. 形式 for ( declaration : expression ) statement 0.1 根据标准将会扩展成这样的形式: 1   { 2     auto&& __ra ...

  2. MVC5移除不常用Nuget命令

    ---移除JQuery.* 和bootstartp Uninstall-Package bootstrap Uninstall-Package Microsoft.jQuery.Unobtrusive ...

  3. webrtc之视频显示模块--video_render

    在上一篇博文中,简单介绍了webrtc为我们提供了跨平台的视频采集模块,这篇博文也简单介绍下webrtc为我们提供的跨平台的视频显示模块:video_render. 该模块的源码结构如下: 如上图,我 ...

  4. 交叉编译安装ARM平台上的Qt

    一.宿主机环境搭建: 编译需要x11库的支持,在Ubuntu下安装命令: sudo apt-get install libx11-dev libxext-dev libxtst-dev 二.下载源码包 ...

  5. Jquery函数实现时间显示模式为更新于+被当前时间减去后剩余的时间值(例如:更新于三小时前)的处理。

    var time_eles = $(".time_tranfer"); for(var i =0; i < time_eles.length;i++){ var time_e ...

  6. DNF(一.YUM已死,DNF代之)

    Yum还没学好呢,突然听到已经要被抛弃了.恐慌至极.. 在最新版的Fedora 22 抛弃了Yum包管理器,取而代之的是DNF.. 那么搜搜 Fedora 22 Release Note.. 官方给出 ...

  7. 阿里云Centos7使用yum安装MySQL5.6的正确姿势

    阿里云Centos7使用yum安装MySQL5.6 阿里云Centos7使用yum安装MySQL5.6 前言:由于某些不可抗力,我要在自己的阿里云服务器上搭建hadoop+hive+mysql+tom ...

  8. CSS实现背景透明/半透明效果的方法

    全透明代码:{background:transparent} 半透明代码:{filter:alpha(opacity=80);-moz-opacity:0.8;width:auto !importan ...

  9. python之简单入门01

     python简单的介绍使用: 一.个人感觉写Python程序,最好用的工具就是pycharm了,自动补全功能可以满足大多数不太喜欢记忆的人群: 安装pycharm之前应该先安装python解释器,目 ...

  10. [模拟炉石](一)让游戏过程显示到cocos2d中

    在上篇中,如果运行了fireplace的tests/full_game.py,这个程序将一个游戏过程在终端上运行完成,可以看到整个过程,那么第一步要做的就是将这个过程显示到cocos2d创建的场景中去 ...