Count the Colors---zoj1610线段树
题意:
求每种颜色有几段线段;
模拟数组:
#include<stdio.h>
#include<iostream>
#include<algorithm>
#include<string.h>
using namespace std;
#define N 8006
int cnt[N],a[N];
int main()
{
int n, p, q, c;
while(scanf("%d", &n) != EOF)
{
int Max = ;
memset(a, , sizeof(a));
memset(cnt, , sizeof(cnt));
for(int i=; i<n; i++)
{
scanf("%d%d%d", &p, &q, &c);
for(int j=p; j<q; j++)
a[j] = c+;
Max = max(Max, q);
}
for(int i=; i<Max; i++)
{
while(i!= && a[i]!= && a[i]==a[i-])
i++;
if(a[i] != )
cnt[ a[i]- ]++;
}
for(int i=; i<N; i++)
{
if(cnt[i]!=)
printf("%d %d\n", i, cnt[i]);
}
printf("\n");
}
return ;
}
#include<stdio.h>
#include<string.h>
#define N 8005 struct SegTree
{
int L,R,c;
int mid()
{
return (L+R)>>;
}
}a[N*]; int cnt[N], vis[N]; void BuildSegTree(int r, int L, int R)
{
a[r].L = L;
a[r].R = R;
a[r].c = -;
if(L+==R)//一条线段的两个端点相差1,
return ;
BuildSegTree(r*, L, a[r].mid());
BuildSegTree(r*+, a[r].mid(), R);//题目给的是端点值,对于线段不能用a[r].mid()+1;
}
void Update(int r, int L, int R, int c)
{
if(a[r].c == c)return ;//如果说这条线段的颜色和要涂的相同,返回;
if(a[r].L == L && a[r].R == R)
{
a[r].c = c;//指定范围内改成颜色c;
return ;
}
if(a[r].c != -)
{
a[*r].c = a[*r+].c = a[r].c;//更新左右子树;
a[r].c = -;
} if(R<=a[r].mid())
Update(*r, L, R, c);
else if(L>=a[r].mid())//等于号不能省;原因是这是线段不是点;
Update(*r+, L, R, c);
else
{
Update(*r, L, a[r].mid(), c);
Update(*r+, a[r].mid(), R, c);
}
}
void Query(int r, int L, int R)
{
if(a[r].c != -)//到达叶子节点;
{
if(vis[L] != a[r].c)
cnt[ a[r].c ]++;
vis[R] = a[r].c;
return ;
}
if(R-L == )
return ;
Query(*r, L, a[r].mid());
Query(*r+, a[r].mid(), R);
} int main()
{
int n, p, q, c;
while(scanf("%d", &n) != EOF)
{
memset(cnt, , sizeof(cnt));
memset(vis, -, sizeof(vis)); BuildSegTree(, , ); while(n--)
{
scanf("%d %d %d", &p, &q, &c);
if(p == q)continue;
Update(, p, q, c);
}
Query(, , ); for(int i=; i<N; i++)
{
if(cnt[i] != )
printf("%d %d\n", i, cnt[i]);
}
printf("\n");
}
return ;
}
Count the Colors---zoj1610线段树的更多相关文章
- Count the Colors(线段树染色)
Count the Colors Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Submit ...
- ZOJ 1610——Count the Colors——————【线段树区间替换、求不同颜色区间段数】
Count the Colors Time Limit:2000MS Memory Limit:65536KB 64bit IO Format:%lld & %llu Subm ...
- ZOJ 1610 Count the Colors (线段树区间更新与统计)
Painting some colored segments on a line, some previously painted segments may be covered by some th ...
- F - Count the Colors(线段树)
Painting some colored segments on a line, some previously painted segments may be covered by some th ...
- ZOJ - 1610 Count the Colors(线段树区间更新,单点查询)
1.给了每条线段的颜色,存在颜色覆盖,求表面上能够看到的颜色种类以及每种颜色的段数. 2.线段树区间更新,单点查询. 但是有点细节,比如: 输入: 2 0 1 1 2 3 1 输出: 1 2 这种情况 ...
- Zoj 1610 Count the Colors (线段树+区间更新+暴力计数)
题目大意: 有n次操作,每次都是对一根线中的一段区间进行染色(颜色并不相同),有时候后面的颜色有可能覆盖前面的颜色,问最后涂完色,能看到的颜色有几种,每种颜色有几部分? 解题思路: 这个题目建树的时候 ...
- ZOJ 1610 Count the Colors (线段树区间更新)
题目链接 题意 : 一根木棍,长8000,然后分别在不同的区间涂上不同的颜色,问你最后能够看到多少颜色,然后每个颜色有多少段,颜色大小从头到尾输出. 思路 :线段树区间更新一下,然后标记一下,最后从头 ...
- zoj 1610 Count the Colors(线段树延迟更新)
所谓的懒操作模板题. 学好acm,英语很重要.做题的时候看不明白题目的意思,我还拉着队友一块儿帮忙分析题意.最后确定了是线段树延迟更新果题.我就欣欣然上手敲了出来. 然后是漫长的段错误.... 第一次 ...
- ZOJ 1610 Count the Colors 【线段树】
<题目链接> 题目大意: 在[0,8000]这个区间内,不断进行一些操作,将其中的一些区间染成特定颜色,如果区间重复的话,后面染的色块会覆盖前面染的色块,问最终[0,8000]这个区间内每 ...
- ZOJ-1610 Count the Colors(线段树染色,求染色段)
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1610 https://vjudge.net/contest/318019# ...
随机推荐
- linux下jdk简单配置记录
记录哈,搭建环境的时候,copy使用方便. vim /etc/profile export JAVA_HOME=/usr/java/jdk1.7.0_79export PATH=$JAVA_HOME/ ...
- Robot Framework进行web ui自动化测试,浏览器配置说明
转载请注明出处,谢谢: chrome浏览器: 1.从如下地址下载与本地浏览器版本号一致的chromedriver.exe驱动文件: http://chromedriver.storage.google ...
- python cookie
http://www.jayconrod.com/posts/17/how-to-use-http-cookies-in-python
- PHP 搜索分词实现代码
<?php /** * @author: xiaojiang 2014-01-08 * php 建立分词树 * */ class Tree{ public $w = ''; public $su ...
- ASP.NET使用百度编辑器(UEditor)使用方法
ASP.NET使用百度编辑器(UEditor)方法如下 第一步到百度官网下载百度编辑器 http://ueditor.baidu.com/website/download.html 下载.net版本 ...
- jQuery事件处理(六)
1.通过一步步调试的的方法观察了一下存放到cache中的事件及其处理程序的数据格式: { events : { // 根据事件类型存放添加到该元素上的所有事件,下面以click为例 click : [ ...
- 第四步 使用 adt-eclipse 打包 Cordova (3.0及其以上版本) + sencha touch 项目
cordova最新中文api http://cordova.apache.org/docs/zh/3.1.0/ 1.将Cordova 生成的项目导入到adt-eclipse中,如下: 项目结构如下: ...
- 【CF662A】Gambling Nim 线性基
[CF662A]Gambling Nim 题意:n长卡牌,第i张卡牌正面的数字是$a_i$,反面的数字是$b_i$,每张卡牌等概率为正面朝上或反面朝上.现在Alice和Bob要用每张卡牌朝上的数字玩N ...
- [工具] Citavi – 文献管理软件(笔记记录)
https://www.citavi.com/en/ 如果你有论文写作的需求,特别是对于科研人员.研究生来说,文献管理软件是不可或缺的.目前流行的文献管理软件有老牌的 EndNote,本土化的 Not ...
- 9.12DjangoORM回顾和路由.
2018-9-12 13:44:41 周末继续整理一下博客!不知不觉记了好多! 越努力越幸运! 永远不要高估自己! 关于反射的复习 # /usr/bin/env python # -*- coding ...