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. 剑桥offer(31~40)

    31.题目描述 统计一个数字在排序数组中出现的次数. 思路:找到最低和最高,相减 class Solution { public: int GetNumberOfK(vector<int> ...

  2. mysql的select的五子句

    转: http://www.cnblogs.com/billyu/p/5033167.html http://www.cnblogs.com/xiadong90-2015/p/4222965.html ...

  3. 动态切换input的 disables 属性

    $("input[type='text']").each(function(){ if($(this).data('parent_id')){ var _each_this_par ...

  4. [技巧篇]03.关于MyBatis的简单批量处理

  5. Http字段含义

    转载自:http://blog.csdn.net/sand_ant/article/details/10503579 一.request请求Header简介 Accept:--客户机支持的类型 Acc ...

  6. [洛谷P2596] [ZJOI2006]书架

    洛谷题目链接:书架 题目描述 小T有一个很大的书柜.这个书柜的构造有些独特,即书柜里的书是从上至下堆放成一列.她用1到n的正整数给每本书都编了号. 小T在看书的时候,每次取出一本书,看完后放回书柜然后 ...

  7. 局部性原理的点滴应用场景 use of localityprinciple

    话说九月份博士入学面试的时候被问到了一个问题:请说明一下局部性原理在计算机科学中的应用场景?(哈哈,不记得怎么问的了,大概是这个意思)但是巴拉巴拉整半天却也只说出了一个Cache,后来补充的也都是跟C ...

  8. 【BZOJ】1577: [Usaco2009 Feb]庙会捷运Fair Shuttle

    [题意]公车从1开到n,有k群牛想从一个点到达另一个点,公车最多乘坐c个人,牛群可以拆散,问最多载多少牛到达目的地. [算法]贪心+堆 [题解]线段和点的贪心,一般有按左端点排序和按右端点排序两种方法 ...

  9. 【51NOD】斜率最大

    [题解]通过画图易得结论:最大斜率一定出现在相邻两点之间. #include<cstdio> #include<algorithm> #include<cstring&g ...

  10. 【BZOJ】1014 [JSOI2008]火星人prefix

    [算法]splay [题解]对于每个结点维护其子树串的hash值,前面为高位,后面为低位. sum[x]=sum[L]*base[s[R]+1]+A[x]*base[s[R]]+sum[R],其中su ...