题目链接

题意:

求每种颜色有几段线段;

模拟数组:

#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线段树的更多相关文章

  1. Count the Colors(线段树染色)

    Count the Colors Time Limit:2000MS    Memory Limit:65536KB    64bit IO Format:%lld & %llu Submit ...

  2. ZOJ 1610——Count the Colors——————【线段树区间替换、求不同颜色区间段数】

    Count the Colors Time Limit:2000MS     Memory Limit:65536KB     64bit IO Format:%lld & %llu Subm ...

  3. ZOJ 1610 Count the Colors (线段树区间更新与统计)

    Painting some colored segments on a line, some previously painted segments may be covered by some th ...

  4. F - Count the Colors(线段树)

    Painting some colored segments on a line, some previously painted segments may be covered by some th ...

  5. ZOJ - 1610 Count the Colors(线段树区间更新,单点查询)

    1.给了每条线段的颜色,存在颜色覆盖,求表面上能够看到的颜色种类以及每种颜色的段数. 2.线段树区间更新,单点查询. 但是有点细节,比如: 输入: 2 0 1 1 2 3 1 输出: 1 2 这种情况 ...

  6. Zoj 1610 Count the Colors (线段树+区间更新+暴力计数)

    题目大意: 有n次操作,每次都是对一根线中的一段区间进行染色(颜色并不相同),有时候后面的颜色有可能覆盖前面的颜色,问最后涂完色,能看到的颜色有几种,每种颜色有几部分? 解题思路: 这个题目建树的时候 ...

  7. ZOJ 1610 Count the Colors (线段树区间更新)

    题目链接 题意 : 一根木棍,长8000,然后分别在不同的区间涂上不同的颜色,问你最后能够看到多少颜色,然后每个颜色有多少段,颜色大小从头到尾输出. 思路 :线段树区间更新一下,然后标记一下,最后从头 ...

  8. zoj 1610 Count the Colors(线段树延迟更新)

    所谓的懒操作模板题. 学好acm,英语很重要.做题的时候看不明白题目的意思,我还拉着队友一块儿帮忙分析题意.最后确定了是线段树延迟更新果题.我就欣欣然上手敲了出来. 然后是漫长的段错误.... 第一次 ...

  9. ZOJ 1610 Count the Colors 【线段树】

    <题目链接> 题目大意: 在[0,8000]这个区间内,不断进行一些操作,将其中的一些区间染成特定颜色,如果区间重复的话,后面染的色块会覆盖前面染的色块,问最终[0,8000]这个区间内每 ...

  10. ZOJ-1610 Count the Colors(线段树染色,求染色段)

    http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1610 https://vjudge.net/contest/318019# ...

随机推荐

  1. Selenium 查找节点

    Selenium 可以驱动浏览器完成各种操作,比如填充表单.模拟点击等.比如,我们想要完成向某个输入框输入文字的操作,总需要知道这个输入框在哪里吧?而 Selenium 提供了一系列查找节点的方法,我 ...

  2. java里面的getAttribute和findAttribute的区别

    findAttribute: abstract Object findAttribute(String name) Searches for the named attribute in page, ...

  3. php 原生文件下载

    1.整个网页的html界面源码下载: xiazai.php <html> <head> <meta charset ="utf-8"> < ...

  4. PowerDesigner 同步Name到Comment 及 同步 Comment 到Name

    PowerDesigner中使用方法为:     PowerDesigner->Tools->Execute Commands->Edit/Run Scripts 代码一:将Name ...

  5. SqlServer 连接远程服务器数据库 实现跨服务器联合查询

    第一种: exec sp_addlinkedserver 'ITSV ', ' ', 'SQLOLEDB ', '远程服务器名或ip地址 ' exec sp_addlinkedsrvlogin 'IT ...

  6. Android开发训练之第五章第四节——Syncing to the Cloud

    Syncing to the Cloud GET STARTED DEPENDENCIES AND PREREQUISITES Android 2.2 (API level 8) and higher ...

  7. sencha touch 视图(view) activate与deactivate事件探讨

    在sencha touch2.2中采用card布局 之前的需求是考虑show,hide事件发现不可取 http://www.cnblogs.com/mlzs/archive/2013/06/13/31 ...

  8. telnet命令的使用方法

    大家都知道,Telnet协议是TCP/IP协议族中的一员,是Internet远程登陆服务的标准协议和主要方式.它为用户提供了在本地计算机上完成远程主机工作的能力.在终端使用者的电脑上使用telnet程 ...

  9. CSS 让 fontawesome 图标字体变细

    一句 CSS 让 fontawesome 图标字体变细 自从 iOS 某个版本发布之后,前端的流行趋势是什么都越来越细…字体越来越细…图标线条也越来越细.而老物 fontawesome 粗壮的线条风格 ...

  10. 关于javaagent拦截不到File类的问题

    main类: Java code   ? 1 2 3 4 public static void main(String[] args) {         File file = new File(& ...