(线段树) Count the Colors --ZOJ --1610
链接:
http://acm.hust.edu.cn/vjudge/contest/view.action?cid=82832#problem/F
http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1610
Count the Colors
Time Limit: 2 Seconds Memory Limit: 65536 KB
Painting some colored segments on a line, some previously painted segments may be covered by some the subsequent ones.
Your task is counting the segments of different colors you can see at last.
Input
The first line of each data set contains exactly one integer n, 1 <= n <= 8000, equal to the number of colored segments.
Each of the following n lines consists of exactly 3 nonnegative integers separated by single spaces:
x1 x2 c
x1 and x2 indicate the left endpoint and right endpoint of the segment, c indicates the color of the segment.
All the numbers are in the range [0, 8000], and they are all integers.
Input may contain several data set, process to the end of file.
Output
Each line of the output should contain a color index that can be seen from the top, following the count of the segments of this color, they should be printed according to the color index.
If some color can't be seen, you shouldn't print it.
Print a blank line after every dataset.
Sample Input
5
0 4 4
0 3 1
3 4 2
0 2 2
0 2 3
4
0 1 1
3 4 1
1 3 2
1 3 1
6
0 1 0
1 2 1
2 3 1
1 2 0
2 3 0
1 2 1
Sample Output
1 1
2 1
3 1
1 1
0 2
1 1
一个涂色问题,我看到了好几种不同的解法, 感觉太神奇,太奇妙了,先附上自己的代码,再学习一下别人的,写了好几题了,不能追速度,我需要好好琢磨琢磨
代码:
#include<stdio.h>
#include<algorithm>
#include<stdlib.h>
#include<string.h>
using namespace std; #define Lson r<<1
#define Rson r<<1|1 const int N = ; struct Node
{
int l, r, c;
} s[N<<]; struct node
{
int L, R;
int color;
int Mid()
{
return (L+R)>>;
}
} a[N<<]; int Color[N]; void CoverColor(int L, int R, int e)
{
for(int i=L; i<=R; i++)
Color[i]=e;
} void UpDate(int r)
{
if(a[r].L != a[r].R)
if(a[Lson].color== && a[Rson].color==)
a[r].color = ;
} void BuildTree(int r, int L, int R)
{
a[r].L = L, a[r].R = R;
a[r].color = ; // 0代表没有颜色覆盖, 1代表未覆盖, 2代表子树有被别的颜色覆盖 if(L==R) return ; BuildTree(Lson, L, a[r].Mid());
BuildTree(Rson, a[r].Mid()+, R);
} void Insert(int r, int L, int R, int e)
{
if(a[r].color == ) return ; if(a[r].L==L && a[r].R==R && !a[r].color)
{
a[r].color=;
CoverColor(L, R, e);
return ;
} a[r].color = ; if(R<=a[r].Mid())
Insert(Lson, L, R, e);
else if(L>a[r].Mid())
Insert(Rson, L, R, e);
else
{
Insert(Lson, L, a[r].Mid(), e);
Insert(Rson, a[r].Mid()+, R, e);
} UpDate(r);
} int main()
{
int n; while(scanf("%d", &n)!=EOF)
{
int i, ans[N]={}; memset(s, , sizeof(s));
for(i=; i<=n; i++)
scanf("%d%d%d", &s[i].l, &s[i].r, &s[i].c); BuildTree(, , N-);
memset(Color, -, sizeof(Color)); for(i=n; i>; i--)
Insert(, s[i].l+, s[i].r, s[i].c); for(i=; i<N; i++)
{
if(Color[i]!=- && (!i || Color[i]!=Color[i-]))
ans[Color[i]]++;
} for(i=; i<N; i++)
if(ans[i]) printf("%d %d\n", i, ans[i]); printf("\n");
}
return ;
}
队友暴力过的代码, 没用线段树
#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 ;
}
(线段树) Count the Colors --ZOJ --1610的更多相关文章
- F - Count the Colors ZOJ - 1610 线段树染色(染区间映射)
题意:给一段0-8000的线段染色 问最后 颜色x 有几段 题解:标准线段树 但是没有push_up 最后查询是单点按顺序查询每一个点 考虑过使用区间来维护不同的线段有多少种各色的线段 思路是 ...
- F - Count the Colors - zoj 1610(区间覆盖)
有一块很长的画布,现在想在这块画布上画一些颜色,不过后面画的颜色会把前面画的颜色覆盖掉,现在想知道画完后这块画布的颜色分布,比如 1号颜色有几块,2号颜色有几块.... *************** ...
- Count the Colors ZOJ - 1610 区间颜色覆盖
#include<stdio.h> #include<iostream> #include<string.h> #include<algorithm> ...
- 线段树区间染色 ZOJ 1610
Count the Colors ZOJ - 1610 传送门 线段树区间染色求染色的片段数 #include <cstdio> #include <iostream> #in ...
- F - Count the Colors
F - Count the Colors ZOJ - 1610 思路:调了一个小时,但是发现自己线段树木有写错,颜色统计出了错误.但是不明白自己颜色统计为什么错了. 求大佬指点迷津.思路很简单,就 ...
- Count Colour_poj2777(线段树+位)
POJ 2777 Count Color (线段树) Count Color Time Limit: 1000MS Memory Limit: 65536K Total Submissions ...
- 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【题意+线段树区间更新&&单点查询】
任意门:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1610 Count the Colors Time Limit: 2 ...
- ZOJ 1610 Count the Colors (线段树区间更新与统计)
Painting some colored segments on a line, some previously painted segments may be covered by some th ...
随机推荐
- Haskell语言学习笔记(27)Endo, Dual, Foldable
Endo Monoid newtype Endo a = Endo { appEndo :: a -> a } instance Monoid (Endo a) where mempty = E ...
- 数据文件导入mysql时出现中文乱码问题
http://www.ynpxrz.com/n773257c2024.aspx 从shell进入mysql, mysql> show variables like ‘%char%'; +---- ...
- Mysql count(1) group_concat 高级用法(count 过滤条件,group_concat过滤条件)
1.官方文档: count:COUNT(expr) [over_clause] https://dev.mysql.com/doc/refman/8.0/en/group-by-functions.h ...
- mysql连接数据库存报下面错误:ERROR 2002 (HY000): Can't connect to local MySQL server through socket '/tmp/mysql.sock' (2)
输入 mysql -u root 登录 mysql 的时候出现以下错误: ERROR 2002 (HY000): Can't connect to local MySQL server through ...
- phpStudy1——PHP文件获取html提交的参数
示例代码: submit.html <!DOCTYPE html> <html> <head> <meta charset="UTF-8" ...
- Extjs面板和布局初探
面板相当于一张干净的白纸,如果直接在上面添加内容,将很难控制面板中内容的显示位置,面板元素越多就越显得凌乱,所以需要在面板上划分不同的区域,将面板内容展示到希望的位置上.ExtJS通过提供多种布局类来 ...
- Spring框架的配置文件分开管理(了解)
1. 例如:在src的目录下又多创建了一个配置文件,现在是两个核心的配置文件,那么加载这两个配置文件的方式有两种! * 主配置文件中包含其他的配置文件: <import resource=&qu ...
- install package
http://www.michael-noll.com/blog/2014/03/17/wirbelsturm-one-click-deploy-storm-kafka-clusters-with-v ...
- Problem B. Market(market.c/cpp/pas)
Problem B. Market(market.c/cpp/pas)Time limit: 1 secondsMemory limit: 128 megabytes在比特镇一共有 n 家商店,编号依 ...
- PS抠发丝技巧 「选择并遮住…」
PS抠发丝技巧 「选择并遮住…」 现在的海报设计,大多数都有模特MM,然而MM的头发实用太多了,有的还飘起来…… 对于设计师(特别是淘宝美工)没有一个强大.快速.实用的抠发丝技巧真的混不去哦.而PS ...