【POJ2777】Count Color(线段树)
以下是题目大意:
有水平方向上很多块板子拼成的墙,一开始每一块都被涂成了颜色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(线段树)的更多相关文章
- [poj2777] Count Color (线段树 + 位运算) (水题)
发现自己越来越傻逼了.一道傻逼题搞了一晚上一直超时,凭啥子就我不能过??? 然后发现cin没关stdio同步... Description Chosen Problem Solving and Pro ...
- POJ2777 Count Color 线段树区间更新
题目描写叙述: 长度为L个单位的画板,有T种不同的颜料.现要求按序做O个操作,操作分两种: 1."C A B C",即将A到B之间的区域涂上颜色C 2."P A B&qu ...
- Count Color(线段树+位运算 POJ2777)
Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 39917 Accepted: 12037 Descrip ...
- POJ 2777 Count Color(线段树之成段更新)
Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 33311 Accepted: 10058 Descrip ...
- poj 2777 Count Color(线段树)
题目地址:http://poj.org/problem?id=2777 Count Color Time Limit: 1000MS Memory Limit: 65536K Total Subm ...
- poj 2777 Count Color(线段树区区+染色问题)
题目链接: poj 2777 Count Color 题目大意: 给出一块长度为n的板,区间范围[1,n],和m种染料 k次操作,C a b c 把区间[a,b]涂为c色,P a b 查 ...
- poj 2777 Count Color(线段树、状态压缩、位运算)
Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 38921 Accepted: 11696 Des ...
- poj 2777 Count Color - 线段树 - 位运算优化
Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 42472 Accepted: 12850 Description Cho ...
- POJ P2777 Count Color——线段树状态压缩
Description Chosen Problem Solving and Program design as an optional course, you are required to sol ...
- POJ 2777 Count Color (线段树成段更新+二进制思维)
题目链接:http://poj.org/problem?id=2777 题意是有L个单位长的画板,T种颜色,O个操作.画板初始化为颜色1.操作C讲l到r单位之间的颜色变为c,操作P查询l到r单位之间的 ...
随机推荐
- 【转】ASP.NET MVC框架下使用MVVM模式-KnockOutJS+JQ模板例子
KnockOutJS学习系列----(一) 好几个月没去写博客了,最近也是因为项目紧张,不过这个不是借口,J. 很多时候可能是因为事情一多,然后没法静下来心来去写点东西,学点东西. 也很抱歉,突然看到 ...
- (转)Maven实战(一)安装与配置
1. 简介 Maven是基于项目对象模型(POM),可以通过一小段描述信息来管理项目的构建,报告和文档的软件项目管理工具. 如果你已经有十次输入同样的Ant targets来编译你的代码.jar或者w ...
- 关于ionic的一些坑(1)
既然来了,总要留下点什么证明自己来过不是,今天就扒一扒自己在ionic上面遇到的坑,因为在项目中2还没出来,所以现在所遇到的都是1中的,关于2的,待老夫以后详细摸索之后在与君细细道来. 1.ionic ...
- Gulp:静态资源(css,js)版本控制
为了防止客户端的静态资源缓存,我们需要每次更新css或js的时候,通过md5或时间戳等方式重新命名静态资源: 然后涉及到的html模板里的src也要做相应的修改,静态资源需要优化(压缩合并) 文件目录 ...
- js判断上传文件大小
下面提供三款网页特效判断上传文件大小哦,这三种方法是现在限制文件上传大小比较好的方法,可以在客户上传文件时限制上传文件大小判断处理<!doctype html public "-//w ...
- IOS 判断设备屏幕尺寸、分辨率
根据屏幕尺寸和分辨率,ios现在数起来有6个版本. iOS 设备现有的分辨率如下: iPhone/iPod Touch 普通屏 320像素 x 480像素 iPhone .3G.3GS,iPod To ...
- 中国大推力矢量发动机WS15 跨入 世界先进水平!
"太行"WS-15让俄闭嘴令美叹服 歼20试飞向世界证明,中国军工世界一流,并有望与美英法争夺新一代航空发动机桂冠.笔者请教解放军专家证实:中国四代机所配套的两台18吨推力的WS- ...
- C#winform检测电脑安装的.netframework版本和是否安装了某软件
代码如下: //C#获取已安装 .NET Framework 版本 private static string[] GetDotNetVersions() { DirectoryInfo[] dire ...
- prototype vs __proto__ 之间关系
__proto__ is the actual object that is used in the lookup chain to resolve methods, etc. __proto__是解 ...
- SQLServer2008/2012 删除所有表视图存储过程
SQLServer2008/2012 删除所有表视图存储过程 -------------------删除所有的表-------------------use xuwenbin111--/第1步**** ...