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. Java CPU占用率高分析

    首先,通过top命令找出CPU占用率高的进程: 然后,通过ps -o THREAD,tid,time -mp 2066命令找出执行时间最长的线程的TID 将有问题的TID转为16进制格式: print ...

  2. HDU 5538 (水不水?)

    House Building Time Limit: 2000/1000 MS (Java/Others)    Memory Limit: 262144/262144 K (Java/Others) ...

  3. zoj 2369 Two Cylinders

    zoj 2369 Two Cylinders 链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=2369 题意:已知两个无 ...

  4. UVA 11922 Splay tree

    UVA 11922 题意: 有n个数1~n 操作a,b表示取出第a~b个数,翻转后添加到数列的尾部 输入n,m 输入m条指令a,b 输出最终的序列 代码: #include<iostream&g ...

  5. Redis(3) 配置文件 redis.conf

    Redis.conf 配置详解: # Redis configuration file example. # # Note that in order to read the configuratio ...

  6. 2015/8/30 Python基础(4):序列操作符

    序列是指成员有序排列,可以通过下标偏移量访问的类型.Python序列包括:字符串.列表和元组.序列的每个元素可以指定一个偏移量得到,多个元素是通过切片操作得到的.下标偏移量从0开始计数到总数-1结束. ...

  7. java 8新特性 instant

    Java 8目前已经开始进入大众的视线,其中笔者在写本文之前,留意到其中Java 8预览版中将会出现新的关于日期和时间的API(遵守JSR310规范).在本系列文章中,将对这些新的API进行举例说明. ...

  8. CAS 逻辑流程图

  9. 「6月雅礼集训 2017 Day4」暴力大神hxx

    [题目大意] 给出一个n重循环,每重循环有范围$[l, r]$,其中$l$,$r$可能是之前的变量,也可能是常数.求循环最底层被执行了多少次. 其中,保证每个循环的$l$,$r$最多有一个是之前的变量 ...

  10. 51Nod 1228 序列求和

    T(n) = n^k,S(n) = T(1) + T(2) + ...... T(n).给出n和k,求S(n).   例如k = 2,n = 5,S(n) = 1^2 + 2^2 + 3^2 + 4^ ...