以下是题目大意:

有水平方向上很多块板子拼成的墙,一开始每一块都被涂成了颜色1,有C和P两个操作,代表的意思是:
C X Y Z —— 从X到Y将板子涂成颜色Z
P X Y    —— 查询X到Y的板子共有多少种颜色

            //有2块板子   两2颜色   4个询问
C
P
C
P

自己AC后上网查阅了许多别人的题解,看很多人用的什么“状态压缩”、“位运算”等等方法。感觉自己不会的知识还有很多。我用的方法是类似于hash的思想,每次将颜色查询时标记,由于这题的数据范围很小,不需要用离散化处理,所以比较好写。下面附上代码。

 /*********************************************/
/* author: Desgard_Duan */
/* motto : Everything is surprise for you! */
/*********************************************/ #include <iostream>
#include <cstring>
#include <cstdlib>
#include <cstdio>
#include <cctype>
#include <cmath>
#include <algorithm>
#include <numeric>
#include <string>
#include <limits.h>
#include <vector>
#include <set>
#include <map> using namespace std; const int maxn = ;
int col[maxn << ], ans = ;
int Hash[]; void pushDown (int rt) {
if (col[rt]) {
col[rt * ] = col[rt * + ] = col[rt];
col[rt] = ;
}
} void build (int l, int r, int rt) {
col[rt] = ;
if (l == r) return ;
int m = (l + r) / ;
build (l, m, rt * );
build (m + , r, rt * + );
} void update (int L, int R, int c, int l, int r, int rt) {
if (L <= l && r <= R) {
col[rt] = c;
return ;
}
pushDown (rt);
int m = (l + r) / ;
if (L <= m) update (L, R, c, l, m, rt * );
if (m < R) update (L, R, c, m + , r, rt * + );
} void query (int L, int R, int l, int r, int rt) {
if (col[rt]) {
if (Hash[col[rt]] == ) {
ans ++;
Hash[col[rt]] = ;
}
return ;
}
int m = (l + r) / ;
if (m >= L) query (L, R, l, m, rt * );
if (m < R) query (L, R, m + , r, rt * + );
} int main () {
int L, T, O, x, y, z;
char op[];
while (~scanf ("%d%d%d", &L, &T, &O)) {
memset (col, , sizeof (col));
build (, L, );
while (O --) {
scanf ("%s%d%d", op, &x, &y);
if (x > y) swap (x, y);
if (op[] == 'C') {
scanf ("%d", &z);
update (x, y, z, , L, );
} else {
memset (Hash, , sizeof (Hash));
ans = ;
query (x, y, , L, );
printf ("%d\n", ans);
}
}
}
return ;
}

【POJ2777】Count Color(线段树)的更多相关文章

  1. [poj2777] Count Color (线段树 + 位运算) (水题)

    发现自己越来越傻逼了.一道傻逼题搞了一晚上一直超时,凭啥子就我不能过??? 然后发现cin没关stdio同步... Description Chosen Problem Solving and Pro ...

  2. POJ2777 Count Color 线段树区间更新

    题目描写叙述: 长度为L个单位的画板,有T种不同的颜料.现要求按序做O个操作,操作分两种: 1."C A B C",即将A到B之间的区域涂上颜色C 2."P A B&qu ...

  3. Count Color(线段树+位运算 POJ2777)

    Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 39917 Accepted: 12037 Descrip ...

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

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

  5. poj 2777 Count Color(线段树)

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

  6. poj 2777 Count Color(线段树区区+染色问题)

    题目链接:  poj 2777 Count Color 题目大意:  给出一块长度为n的板,区间范围[1,n],和m种染料 k次操作,C  a  b  c 把区间[a,b]涂为c色,P  a  b 查 ...

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

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

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

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

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

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

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

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

随机推荐

  1. 关于IE6幽灵字体

    前言:今天做项目的时候在IE6下出现了这样的一种现像,这种情况只在IE6下出现,最后在网友的帮助下这个问题最终得到了解决.所以马上作了下笔记! 情况如下图: 我在网上找了点资料出现IE6下幽灵字体的情 ...

  2. SUN-LDAP6.3_RHEL 5.0-卸载LDAP

    卸载LDAP 1.注销服务器 到目录/ldap/ldapinstance/dscc6/bin下 # ./dsccreg remove-server -h 主机名 /ldap/ldapinstance/ ...

  3. 服务 Service 基本介绍

    Activity public class MainActivity extends ListActivity {     private boolean flag;//是否开启线程     publ ...

  4. C#调用cmd 脚本实例

    1.实例1 public static void TestOne() { Process p = new Process(); p.StartInfo.FileName = "cmd.exe ...

  5. (转)ASP.NET版本的Kindeditor插件的使用(同步)

    昨天老大让我自己下载一个kindeditor说要放到“描述”功能中,并且不能提交(一边在textarea中写一边在label控件中将数据显示出来),由于从来没弄过,实在费了一翻劲.所以将此记录下来,一 ...

  6. .NET中常见的内存泄露问题——GC、委托事件和弱引用

    一.什么是内存泄露(memory leak)? 内存泄露不是指内存坏了,也不是指内存没插稳漏出来了,简单来说,内存泄露就是在你期待的时间内你程序所占用的内存没有按照你想象中的那样被释放. 因此什么是你 ...

  7. 如何改写WebApi部分默认规则

    为什么要改 最近公司在推广SOA框架,第一次正经接触这种技术(之前也有但还是忽略掉吧),感觉挺好,就想自己也折腾一下,实现一个简单的SOA框架 用过mvc进行开发,印象之中WebApi和Mvc好像是一 ...

  8. 翻译:《What can I hold you with?》—— 博尔赫斯《英文诗两首》之一。

    What can I hold you with? 我拿什么才能留住你? I offer you lean streets, desperate sunsets, the moon of the ja ...

  9. Centos 6.x 系统下用yum安装Memcache

    我们的第一步就是导入第三方软件仓库RPMForge ,首页进行centos 官网找到RPMForge下载地址 http://wiki.centos.org/AdditionalResources/Re ...

  10. jquery1.9学习笔记 之层级选择器(二)

    子孙选择器(“祖先 子孙”) 描述:选择所有给出祖先选择器的子孙选择器. 例子: 用蓝色虚线边框标记所有表单子孙元素的输入.表单里的输入框用黄色背景. <!doctype html>< ...