题目链接

题意:

求每种颜色有几段线段;

模拟数组:

#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. Nginx(十二)-- Nginx+keepalived实现高可用

    1.前提 两台Linux服务器,IP分别为192.168.80.128 和 192.168.80.129,都安装Nginx和keepalived,并启动. 2.配置双机热备 1.将192.168.80 ...

  2. [Command] sync - 同步内存与硬盘数据

    sync - 同步内存与硬盘之间的数据. sync [--help] [--version] sync 命令将内存中缓存的数据写入磁盘.这包括但不限于修改过的 superblock, inode 和延 ...

  3. 用Python编写一个简单的Http Server

    用Python编写一个简单的Http Server Python内置了支持HTTP协议的模块,我们可以用来开发单机版功能较少的Web服务器.Python支持该功能的实现模块是BaseFTTPServe ...

  4. pom.xml文件错误

    刚创建的maven项目,马上pom.xml的第一行就报错这是第一行:<project xmlns="http://maven.apache.org/POM/4.0.0" xm ...

  5. Eclipse安装php插件phpeclipse(转)

    1.PHPEclipse是Eclipse的一个用于开发PHP的插件.官网下载地址:http://sourceforge.net/projects/phpeclipse/下载解压后,将features和 ...

  6. 【大数据系列】apache hive 官方文档翻译

    GettingStarted 开始 Created by Confluence Administrator, last modified by Lefty Leverenz on Jun 15, 20 ...

  7. 关于SQL优化(转载,格式有调整)

    一.问题的提出 在应用系统开发初期,由于开发数据库数据比较少,对于查询SQL语句,复杂视图的的编写等体会不出SQL语句各种写法的性能优劣,但是如果将应用 系统提交实际应用后,随着数据库中数据的增加,系 ...

  8. * 和-> 优先级

    (Apple *)pf->peel();      则报错说     ct.cpp: In function ‘int main()’:ct.cpp:48: 错误:void 值未如预期地被忽略 ...

  9. nginx 启动重启脚本

    #! /bin/sh # Default-Start:     2 3 4 5 # Default-Stop:      0 1 6 # Short-Description: starts the n ...

  10. [SCOI2011]飞镖[数学模拟]

    2335: [SCOI2011]飞镖 Time Limit: 10 Sec  Memory Limit: 128 MBSubmit: 482  Solved: 152[Submit][Status][ ...