ZOJ 1610 Count the Colors (线段树成段更新)
题意 : 给出 n 个染色操作,问你到最后区间上能看见的各个颜色所拥有的区间块有多少个
分析 : 使用线段树成段更新然后再暴力查询总区间的颜色信息即可,这里需要注意的是给区间染色,而不是给点染色,所以对于区间(L, R)我们只要让左端点+1即可按照正常的线段树操作来做。
#include<bits/stdc++.h>
#define lson l, m, rt<<1
#define rson m+1, r, rt<<1|1
using namespace std;
+ ;
const int INF = 0x3f3f3f3f;
struct Query{ int L, R, val; };
], cnt[maxn], Rmax, ColorMax;
Query Q[maxn];
inline void PutDown(int rt)
{
){
col[rt<<] = col[rt<<|] = col[rt];
col[rt] = -;
}
}
inline void update(int L, int R, int val, int l, int r, int rt)
{
if(L <= l && r <= R){
col[rt] = val;
return ;
}
PutDown(rt);
;
if(L <= m) update(L, R, val, lson);
if(R > m) update(L, R, val, rson);
}
int query(int pos, int l, int r, int rt)
{
if(l == r) return col[rt];
PutDown(rt);
;
int ret;
if(pos <= m) ret = query(pos, lson);
if(pos > m) ret = query(pos, rson);
return ret;
}
int main(void)
{
int paint;
while(~scanf("%d", &paint)){
Rmax = ColorMax = -INF;
; i<=paint; i++){
scanf("%d %d %d", &Q[i].L, &Q[i].R, &Q[i].val);
Q[i].L++;
Rmax = max(Rmax, Q[i].R);///记录区间右端可以多大
ColorMax = max(ColorMax, Q[i].val);///记录颜色的最大值
}
memset(col, -, sizeof(col));
; i<=paint; i++){
< Q[i].R)///说明给出的是一个点,没有覆盖掉哪一段,不用更新
update(Q[i].L, Q[i].R, Q[i].val, , Rmax, );
}
memset(cnt, , sizeof(cnt));
;
; i<=Rmax; i++){///查询区间内每个点的信息,用cnt[]数组来记录拥有的这些颜色占的段数
, Rmax, );
) {last = -; continue;}///注意如果没有被染色,last要赋值成-1,不能直接continue
if(color != last){
cnt[color]++;
}
last = color;
}
; i<=ColorMax; i++){
){
printf("%d %d\n", i, cnt[i]);
}
}
puts("");
}
;
}
瞎 : query操作的时候由于有lazy tag所以需要PutDown,此题虽然不难但是如果独立写一个线段树并且AC估计能够发现自身的一些问题,注意实现细节....
ZOJ 1610 Count the Colors (线段树成段更新)的更多相关文章
- zoj 1610 Count the Colors 线段树区间更新/暴力
Count the Colors Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.zju.edu.cn/onlinejudge/show ...
- ZOJ 1610 Count the Colors(线段树,区间覆盖,单点查询)
Count the Colors Time Limit: 2 Seconds Memory Limit: 65536 KB Painting some colored segments on ...
- POJ 2777 Count Color (线段树成段更新+二进制思维)
题目链接:http://poj.org/problem?id=2777 题意是有L个单位长的画板,T种颜色,O个操作.画板初始化为颜色1.操作C讲l到r单位之间的颜色变为c,操作P查询l到r单位之间的 ...
- ACM: Copying Data 线段树-成段更新-解题报告
Copying Data Time Limit:2000MS Memory Limit:262144KB 64bit IO Format:%I64d & %I64u Description W ...
- Codeforces Round #149 (Div. 2) E. XOR on Segment (线段树成段更新+二进制)
题目链接:http://codeforces.com/problemset/problem/242/E 给你n个数,m个操作,操作1是查询l到r之间的和,操作2是将l到r之间的每个数xor与x. 这题 ...
- hdu 4747【线段树-成段更新】.cpp
题意: 给出一个有n个数的数列,并定义mex(l, r)表示数列中第l个元素到第r个元素中第一个没有出现的最小非负整数. 求出这个数列中所有mex的值. 思路: 可以看出对于一个数列,mex(r, r ...
- HDU1698_Just a Hook(线段树/成段更新)
解题报告 题意: 原本区间1到n都是1,区间成段改变成一个值,求最后区间1到n的和. 思路: 线段树成段更新,区间去和. #include <iostream> #include < ...
- HDU 3577 Fast Arrangement ( 线段树 成段更新 区间最值 区间最大覆盖次数 )
线段树成段更新+区间最值. 注意某人的乘车区间是[a, b-1],因为他在b站就下车了. #include <cstdio> #include <cstring> #inclu ...
- poj 3468 A Simple Problem with Integers 【线段树-成段更新】
题目:id=3468" target="_blank">poj 3468 A Simple Problem with Integers 题意:给出n个数.两种操作 ...
随机推荐
- gym102215题解
A Rooms and Passages 题意 给n个数,从起点出发,一直往右走,遇到一个前面出现过其相反数的正数就停下,问对于每个起点都能走多少步. 分析 倒着递推,如果起点是正数,那么肯定可以走, ...
- Voting CodeForces - 749C (set,模拟)
大意: n个人, 两个党派, 轮流投票, 两种操作(1)ban掉一个人 (2)投票, 每轮一个未被ban的人可以进行一次操作(1)或操作(2), 求最终哪个党派得票最多. 显然先ban人会更优, 所以 ...
- Jdbc Driver驱动和ServerTimeZone时区的的问题
一.JDBC驱动的版本号以及名称问题 区别: com.mysql.jdbc.Driver 是 mysql-connector-java 5中的 com.mysql.cj.jdbc.Driver 是 m ...
- Json解析报错: Error is : Unescaped control character...的解决方法
在利用json-framework来实现json解析的过程时,会出现"-JSONValue Failed. Error is : Unescaped control character&qu ...
- java 返回输入中出现次数最多的字符串
举例输入: abc abc de de de fghi fghi 应该返回: de 代码: static List<String> func(String str) { String[] ...
- nodejs fs copy本地文件src dst
1. // fs.writeFileSync(pathNewFile, fs.readFileSync(fileName)); 2. fs.createReadStream(fileName).p ...
- AIX系统的备份和恢复
1.AIX备份命令
- 解决deepin没有ll等命令的办法
编辑~/.bashrc, 添加alias 如下 vim ~/.bashrc 设置别名. 添加如下行 alias ll='ls -alF' alias la='ls -A' alias vi='vim' ...
- linux误卸载openssl后的恢复
一.原因 由于在编译mysql时,报ssl错误,于是想卸载openssl再重新安装 rpm -qa|grep openssl rpm -e openssl-.el7_6..x86_64 rpm -e ...
- 22_3mybaits——连接池
1.连接池 我们在实际开发中都会使用连接池. 因为它可以减少我们获取连接所消耗的时间. 2.mybatis中的连接池 mybatis连接池提供了3种方式的配置: 配置的位置: 主配置文件SqlMapC ...