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个数.两种操作 ...
随机推荐
- iOS10相册相机闪退bug-b
iOS10系统下调用系统相册.相机功能,遇到闪退的情况,描述如下: This app has crashed because it attempted to access privacy-sensit ...
- Windows 10 响应式设计和设备友好的开发
使用Effective pixels有效像素设计UI 什么是缩放像素和Effective有效像素: 当你的应用程序运行在Windows的设备,系统用一个算法控制的规范,字体,和其他UI元素显示在屏幕上 ...
- CoreLocation MKMapView 地图
系统自带地图 框架: CoreLocation MapKit CLLocationManager --> 定位管理者 CLGeocoder --> 地理编码器 MKMapView -- ...
- Luence简单实现2
上一篇是基于内存存储的,这次的例子是基于本地存储索引库. 上一次的代码稍微修改,代码如下: //创建词法分析器 Analyzer analyzer = new StandardAnalyzer(); ...
- Can't update table 'test_trigger' in stored function/trigger because it is already used by statement which invoked this stored function/trigger.
[Err] 1442 - Can't update table 'test_trigger' in stored function/trigger because it is already used ...
- linux进程管理之开机启动
下面用自启动apache为例;自启动脚本:/usr/local/apache2/bin:./apachectl start文件位于/etc/rc.d/init.d下,名为apached, 注意要可执行 ...
- 基于jQuery的TreeGrid组件详解
一.TreeGrid组件相关的类 1.TreeGrid(_config) _config:json格式的数据,组件所需要的数据都通过该参数提供. 2.TreeGridItem(_root, _rowI ...
- MSVC CRT运行库启动代码分析
原文链接:http://www.programlife.net/msvc-crt-startup.html 在程序进入main/WinMain函数之前,需要先进行C运行库的初始化操作,通过在Visua ...
- UVA 10523 Very Easy!!!(大数据加法、乘法)
题意:给出N,A,计算i*A^i(i=1~N)的和.1<=N<=30,0<=A<=15. 就是大数据运算,别的没什么,注意细节之处即可. 这题还要注意两个地方: 1.考虑A=0 ...
- 29. 栈的push,pop序列
题目:给定2个整数序列,其中1个是栈的push顺序,判断另一个有没有可能是对应的pop顺序 解:其实这题主要是判断进栈次数和出栈次数誓不是相等.我是用栈作的,效率不高,每一个元素最多出栈1次,进栈1此 ...