题目链接

题意:

求每种颜色有几段线段;

模拟数组:

#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. 系统日志:/var/log/messages

    /var/log/messages 存放的是系统的日志信息,它记录了各种事件,基本上什么应用都能往里写日志,在做故障诊断时可以首先查看该文件内容 [root@mirh5_center1_111.231 ...

  2. 如何构建日均千万PV Web站点(二) 之~缓存为王~

    随着网站业务的不断发展,用户的规模越来越大:介于中国无比蹩脚复杂的网路环境:南电信:北联通:中间竟然只用一条链路进行互联通信!有研究表明,网站访问延迟和用户流失率正相关,网站访问速度越慢,用户越容易失 ...

  3. PyQt4程序图标

    程序图标就是一个小图片,通常显示在程序图标的左上角(ubuntu gnome在最上侧). #!/usr/bin/python # -*- coding:utf-8 -*- import sys fro ...

  4. KindEditor上传图片无法使用绝对路径

    之前百度,一直查到的都是urlType使用domain,但是根本没有效果.想着去插件代码里面看,但是实在看不下去了. 最后还是百度去了.然后查到下面的一个方法.直接将其中的某部分代码注释到就好了.具体 ...

  5. TCP端口号范围及分类

    https://blog.csdn.net/my_heart_/article/details/52601924 端口号的范围是从1-65535 端口的概念:  在网络技术中,端口(Port)大致有两 ...

  6. std::u32string conversion to/from std::string and std::u16string

    I need to convert between UTF-8, UTF-16 and UTF-32 for different API's/modules and since I know have ...

  7. 怎么使用jstack精确找到异常代码

    1.代码demo //一个CPU密集型线程的demo: package chapter1; public class FindJavaThreadInTaskManager { public stat ...

  8. mysql explain分析

    通过explain可以知道mysql是如何处理语句,分析出查询或是表结构的性能瓶颈.通过expalin可以得到: 1. 表的读取顺序 2.表的读取操作的操作类型 3.哪些索引可以使用 4. 哪些索引被 ...

  9. 用ChrootDirectory限制SFTP登录的用户只能访问指定目录且不能进行ssh登录

    创建不能ssh登录的用户sftpuser1,密码用于sftp登录: sudo adduser sftpuser1 --home /sftp/sftpuser1 --shell /bin/false s ...

  10. MongoDB安装、CURD操作、使用场景分析总结(1)

    NoSQL(NoSQL = Not Only SQL ),意即"不仅仅是SQL".非关系型的数据存储 MongoDB 是一个基于分布式文件存储的数据库.由 C++ 语言编写.旨在为 ...