Count Color
Time Limit: 1000MS   Memory Limit: 65536K
Total Submissions: 41202   Accepted: 12458

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

题目意思:给三个数n,t,m n代表区间大小,t代表颜色的种类(没什么用) m代表询问次数 操作为'C'时代表更新区间值,操作为'Q'时代表询问区间内的颜色数目并输出。 运用了lazy思想和位运算 。。看了别人的思想打出来的代码。。对理解线段树真的十分有用
lazy:只要插入的区间完全覆盖了当前结点所管理的区间就不再往下做了,在当前结点上打上一个lazy标记,然后直接返回。
下次如果遇到当前结点有lazy标记的话,直接传递给两个儿子,自己的标记清空。好处就是满足条件时就不用更新到子节点,节约时间。
位运算:用二进制来表示每一位的颜色(能够想出来的人真的要对算法非常了解,,,渣渣膜拜)比如说左边叶子节点被染成了3,右边的被染成了4
则 父亲节点就被两种颜色染过了 00...0100 | 00...1000 = 00...1100 代表此区间被 3和4所染过了 ,这题颜色最多30种,所以能够用 int表示
更多解释下面见代码:
#include<stdio.h>
#include<algorithm>
#include<string.h>
#include<iostream>
#define N 1100005
using namespace std; const int MAXSIZE = ;
int sum;
struct Tree{
int color; ///用二进制表示30种颜色,所以求每两个子区间的位或的结果就是父亲结点
int cover ;///表示某区间的颜色是否相同
}tree[MAXSIZE<<];
void pushUp(int i){
tree[i].color = tree[i<<].color|tree[i<<|].color;
}
void pushDown(int i){
if(tree[i].cover==){
tree[i<<].color = tree[i].color;
tree[i<<|].color = tree[i].color;
tree[i<<].cover = tree[i<<|].cover = ;
tree[i].cover = ;
}
}
void build(int l,int r,int idx){
tree[idx].color = ; ///刚开始颜色都为1
tree[idx].cover = ; ///刚开始区间颜色都是相同的
if(l==r) return ;
int mid = (l+r)>>;
build(l,mid,idx<<);
build(mid+,r,idx<<|);
pushUp(idx);
} void update(int l,int r,int L,int R,int idx,int val){
if(l>=L&&R>=r){
tree[idx].cover = ;
tree[idx].color=val;
return;
}
pushDown(idx);
int mid = (l+r)>>;
if(mid>=L) update(l,mid,L,R,idx<<,val);
if(mid<R) update(mid+,r,L,R,idx<<|,val);
pushUp(idx);
}
void query(int l,int r,int L,int R,int idx){
if(l>=L&&R>=r){
sum|=tree[idx].color;
return;
}
pushDown(idx);
int mid = (l+r)>>;
if(mid>=L) query(l,mid,L,R,idx<<);
if(mid<R) query(mid+,r,L,R,idx<<|);
}
int solve(){
int ans = ;
while(sum){
if(sum&) ///如果sum的最低位是1则证明已经被染色
ans++;
sum = sum>>;
}
return ans;
} int main()
{
int n,t,m;
while(scanf("%d%d%d",&n,&t,&m)!=EOF){
build(,n,);
while(m--){
char s[];
scanf("%s",s);
if(s[]=='C'){
int a,b,c;
scanf("%d%d%d",&a,&b,&c);
if(a>b) swap(a,b);
update(,n,a,b,,<<(c-)); ///以二进制来表示颜色,比如染色成2 则二进制为00..010 即 2^1(^代表次方)
}else{
int a,b;
sum = ;
scanf("%d%d",&a,&b);
if(a>b) swap(a,b);
query(,n,a,b,);
printf("%d\n",solve());
}
}
}
return ;
}
												

pku 2777(经典线段树染色问题)的更多相关文章

  1. ZOJ - 1610 经典线段树染色问题

    这个是一个经典线段树染色问题,不过题目给的是左右左右坐标,即[0,3]包含0-1这一段 1-2这一段 2-3这一段,和传统的染色不太一样,不过其实也不用太着急. 我们把左边的坐标+1,即可,那么[0, ...

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

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

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

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

  4. POJ2528 Mayor's posters —— 线段树染色 + 离散化

    题目链接:https://vjudge.net/problem/POJ-2528 The citizens of Bytetown, AB, could not stand that the cand ...

  5. Count the Colors(线段树染色)

    Count the Colors Time Limit:2000MS    Memory Limit:65536KB    64bit IO Format:%lld & %llu Submit ...

  6. F - Count the Colors ZOJ - 1610 线段树染色(染区间映射)

    题意:给一段0-8000的线段染色 问最后 颜色x 有几段 题解:标准线段树  但是没有push_up  最后查询是单点按顺序查询每一个点 考虑过使用区间来维护不同的线段有多少种各色的线段  思路是 ...

  7. PKU 3667 Hotel(线段树)

    Hotel The cows are journeying north to Thunder Bay in Canada to gain cultural enrichment and enjoy a ...

  8. 2243: [SDOI2011]染色 树链剖分+线段树染色

    给定一棵有n个节点的无根树和m个操作,操作有2类: 1.将节点a到节点b路径上所有点都染成颜色c: 2.询问节点a到节点b路径上的颜色段数量(连续相同颜色被认为是同一段), 如“112221”由3段组 ...

  9. 经典线段树 UVALive 3938/UVA 1400

    题意:就是相当于动规里面的求最大连续子串,不同的是,这里需要读入一个区间x,y,输出的区间 a,b 且x<=a<=b<=y,使得a b的连续子串最长,而且询问次数达到了10的五次方. ...

随机推荐

  1. NOIP系列(续)

    马上就要告别noip了呢. 这道题大家都说dfs可过. 但是数据范围一眼状压啊. 首先假设点是有序的(选取有先后顺序),其实这并不影响什么,但是却省下大量的时间和代码长度. 然后状压,dp[i]表示状 ...

  2. 1143: [CTSC2008]祭祀river(最长反链)

    1143: [CTSC2008]祭祀river 题目链接:https://www.lydsy.com/JudgeOnline/problem.php?id=1143 Description: 在遥远的 ...

  3. SPOJ - HIGH :Highways (生成树计数)

    Highways 题目链接:https://vjudge.net/problem/SPOJ-HIGH Description: In some countries building highways ...

  4. idea 创建多模块时模块类无法引入

    我的原因是类的位置方的不对,由于刚搭建的项目,本来只想做个测试,就直接在java下创建类,然而这居然是个深坑,模块引入了也无法引入这个模块的类. 解决方法:创建com.***.***包后的类可以正常引 ...

  5. CMDB资产管理系统开发【day26】:CMDB上节回顾

    一.上节知识点回顾 服务器设计了一个表结构 开发了一个客户端 二.后台创建缓存区表 客户端连接服务器,在服务器的下面看报错信息 因为URL都没有写,所以我找不到呀 1.在MadKing\url.py ...

  6. git代码冲突

    如果系统中有一些配置文件在服务器上做了配置修改,然后后续开发又新添加一些配置项的时候, 在发布这个配置文件的时候,会发生代码冲突: error: Your local changes to the f ...

  7. c# “XXX::Invoke”类型的已垃圾回收委托进行了回调。这可能会导致应用程序崩溃、损坏和数据丢失。向非托管代码传递委托时,托管应用程序必须让这些委托保持活动状态,直到确信不会再次调用它们。

    症状描述如下: 如果将一个委托作为函数指针从托管代码封送到非托管代码,并且在对该委托进行垃圾回收后对该函数指针发出了一个回调,则将激活 callbackOnCollectedDelegate 托管调试 ...

  8. 【HDU】3068 最长回文

    [算法]manacher [题解][算法]字符串 #include<cstdio> #include<algorithm> #include<cstring> us ...

  9. js 作用域链&内存回收&变量&闭包

    闭包主要涉及到js的几个其他的特性:作用域链,垃圾(内存)回收机制,函数嵌套,等等 一.作用域链:函数在定义的时候创建的,用于寻找使用到的变量的值的一个索引,而他内部的规则是,把函数自身的本地变量放在 ...

  10. Python 语言使用中遇到的问题汇总

    1.python中的传值和传引用 和其他语言不一样,传递参数的时候,python不允许程序员选择采用传值还是传引用.Python参数传递采用的肯定是“传对象引用”的方式.实际上,这种方式相当于传值和传 ...