ZOJ 1610 Count the Colors (线段树 成段更新)
题意:成段染色,初始为0,每次改变一个区间的颜色,求最后每种颜色分别有多少段。颜色按照从
小到大输出。
分析:改变了代码的风格,因为看了学长的博客。直接用数组,可以只是记录节点的编号,因为节点编号
确定了,则l,r也就确定了。
#include <iostream>
#include <cstdio>
#include <vector>
#include <cstring>
#include <cstdlib>
#include <algorithm>
#define LL __int64
#define lson l, mid, 2*rt
#define rson mid+1, r, 2*rt+1
const int maxn = +;
using namespace std;
int val[*maxn], num[maxn]; void pushdown(int rt) //如果没有更新,向下更新
{
if(val[rt]!=-)
{
val[*rt] = val[rt];
val[*rt+] = val[rt];
val[rt] = -;
}
}
void update(int ll, int rr, int c, int l, int r, int rt) //ll,rr代表要找的区间,不用减小了,一直都是这个
{
if(ll<=l && rr>=r) //如果当前节点的范围在 改变的节点的范围内就 改变
{
val[rt] = c;
return;
}
pushdown(rt);
int mid = (l+r)/;
if(ll<=mid) update(ll, rr, c, lson); //看这个左半区间内是否还有点
if(rr>mid) update(ll, rr, c, rson);
}
int query(int p, int l, int r, int rt)
{
if(l>p) return ;
if(r<p) return ;
if(p==l&&p==r) return val[rt];
if(p>=l && p<=r && val[rt]!=-) return val[rt];
int mid = (l+r)/;
return query(p, lson)+query(p, rson);
}
int main()
{
int i, n, l, r, c, x, pre;
while(~scanf("%d", &n))
{
memset(val, -, sizeof(val)); //相当于建树
memset(num, , sizeof(num));
while(n--)
{
scanf("%d%d%d", &l, &r, &c);
if(r- >= l)
update(l, r-, c, , , );
}
pre = -;
for(i = ; i <= ; i++) //查询的是区间00,11.。。
{
x = query(i, , , );
if(x!=pre) //如果不连续 就加上
{
pre = x;
if(x>=) num[x]++;
}
}
for(i = ; i <= ; i++)
{
if(num[i])
printf("%d %d\n", i, num[i]);
}
printf("\n");
}
return ;
}
ZOJ 1610 Count the Colors (线段树 成段更新)的更多相关文章
- ZOJ 1610 Count the Colors (线段树成段更新)
题意 : 给出 n 个染色操作,问你到最后区间上能看见的各个颜色所拥有的区间块有多少个 分析 : 使用线段树成段更新然后再暴力查询总区间的颜色信息即可,这里需要注意的是给区间染色,而不是给点染色,所以 ...
- 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个数.两种操作 ...
随机推荐
- 什么是XAML?
XAML类似于XML一样的一种标记语言,主要用来设计UI. 对于XAML的历史,哪些都是废话了,至于Microsoft怎么整出XAML,然后,又怎么让XAML来开发windows 8应用程序,这些,都 ...
- Notes of the scrum meeting(10/30)
meeting time:9:30~11:30p.m.,October 29th,2013 meeting place:20公寓楼前 attendees: 顾育豪 ...
- Liferay SDK 6.2与7.0中build.[$username].properties 文件的配置
这篇文章是针对刚开始开发Liferay的新手写的,希望能够帮到刚入门的开发者减少一些配置上的麻烦. 前提: 1. 下载了Liferay IDE(Liferay的官方开发工具) 2.下载了Liferay ...
- Careercup - Google面试题 - 5735304249999360
2014-05-03 23:18 题目链接 原题: Insert a element in a sorted circular linked list 题目:题意简单明了,向一个有序的循环单向链表中插 ...
- 单例模式(.NET)
问题描述: 单例模式 Singleton Pattern 问题解决: (1)单例模式简介: Singleton模式要求一个类有且仅有一个实例,并且提供了一个全局的访问点.这就提出了一个 ...
- 【POJ】【2096】Collecting Bugs
概率DP/数学期望 kuangbin总结中的第二题 大概题意:有n个子系统,s种bug,每次找出一个bug,这个bug属于第 i 个子系统的概率为1/n,是第 j 种bug的概率是1/s,问在每个子系 ...
- 【POJ】【3164】Commond Network
最小树形图 最小树形图模板题,朱-刘算法. 题解:http://blog.csdn.net/shuangde800/article/details/8039359 这位大神代码写的非常通俗易懂,而且这 ...
- struts文件上传拦截器maximumSize设置文件大小不起作用
<interceptor-ref name="fileUpload"> <param name="allowedTypes ...
- uva 991
卡特兰数 最后不输出空行... #include <cstdio> #include <cstdlib> #include <cmath> #include &l ...
- PHP中$_SERVER获取当前页面的完整URL地址
PHP中$_SERVER获取当前页面的完整URL地址,其实很简单,主要是通过$_SERVER超全局变量来实现的. 具体PHP中$_SERVER获取当前页面的完整URL地址如下. #测试网址: ...