【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单位之间的 ...
随机推荐
- 利用eclipse新建的java web项目没有部署描述符web.xml文件怎么办?
原文转自:http://blog.csdn.net/suyu_yuan/article/details/50947007 利用eclipse新建的Java Web项目没有部署描述符web.xml文件, ...
- 数据库版本管理工具Flyway(4.0.3)---介绍(译文)
Flyway Evolve your Database Schema easily and reliably across all your instances 简单的.可靠的升级(发展)你的数据库模 ...
- 修改http中的refer(转)
Referrer的重要性 HTTP请求中有一个referer的报文头,用来指明当前流量的来源参考页.例如在www.sina.com.cn/sports/上点击一个链接到达cctv.com首页,那么就r ...
- jQuery简单的Ajax调用示例
jQuery确实方便,下面做个简单的Ajax调用: 建立一个简单的html文件: <!DOCTYPE HTML> <html> <head> <script ...
- android后台截屏实现(3)--编译screencap
修改好之后就要编译了,screencap的编译是要在源码环境中进行的. 将修改后的screencap.cpp文件替换源码中的原始文件,然后修改screencap的Android.mk文件,修改后的文件 ...
- parcel write boolean值
http://stackoverflow.com/questions/6201311/how-to-read-write-a-boolean-when-implementing-the-parcela ...
- nexus4/5/6/7/9/10设备谷歌安卓5.1.1系统底包下载
https://developers.google.com/android/nexus/images http://www.inexus.co/thread-18488-1-1.html
- Mysql数据库里面的String类型依照数字来排序以及按时间排序的sql语句
今天做项目的时候,遇到个小小的问题,在数据库中查询的时候,要用String类型的ID进行一下排序!(注:ID字段为 varchar 类型) 解决的方法: 如: SELECT * FROM Stude ...
- REST、SOA、SOAP、RPC、ICE、ESB、BPM知识汇总及理解
转载自处blog.csdn.net/tantexian. SOA: 维基百科解释:SOA:面向服务的软件架构(Service Oriented Architecture),是一种计算机软件的设计模式, ...
- OD: Memory Attach Technology - Off by One, Virtual Function in C++ & Heap Spray
Off by One 根据 Halvar Flake 在“Third Generation Exploitation”中的描述,漏洞利用技术依攻击难度从小到大分为三类: . 基础的栈溢出利用,可以利用 ...