博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
(线段树) Count the Colors --ZOJ --1610
阅读量:4979 次
发布时间:2019-06-12

本文共 3227 字,大约阅读时间需要 10 分钟。

链接:

 

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
#include
#include
#include
using namespace std;#define Lson r<<1#define Rson r<<1|1const int N = 8500;struct Node{ int l, r, c;} s[N<<2];struct node{ int L, R; int color; int Mid() { return (L+R)>>1; }} a[N<<2];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==1 && a[Rson].color==1) a[r].color = 1;}void BuildTree(int r, int L, int R){ a[r].L = L, a[r].R = R; a[r].color = 0; // 0代表没有颜色覆盖, 1代表未覆盖, 2代表子树有被别的颜色覆盖 if(L==R) return ; BuildTree(Lson, L, a[r].Mid()); BuildTree(Rson, a[r].Mid()+1, R);}void Insert(int r, int L, int R, int e){ if(a[r].color == 1) return ; if(a[r].L==L && a[r].R==R && !a[r].color) { a[r].color=1; CoverColor(L, R, e); return ; } a[r].color = 2; 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()+1, R, e); } UpDate(r);}int main(){ int n; while(scanf("%d", &n)!=EOF) { int i, ans[N]={
0}; memset(s, 0, sizeof(s)); for(i=1; i<=n; i++) scanf("%d%d%d", &s[i].l, &s[i].r, &s[i].c); BuildTree(1, 0, N-1); memset(Color, -1, sizeof(Color)); for(i=n; i>0; i--) Insert(1, s[i].l+1, s[i].r, s[i].c); for(i=0; i

队友暴力过的代码, 没用线段树

 

#include
#include
#include
#include
using namespace std;#define N 8006int cnt[N],a[N];int main(){ int n, p, q, c; while(scanf("%d", &n) != EOF) { int Max = 0; memset(a, 0, sizeof(a)); memset(cnt, 0, sizeof(cnt)); for(int i=0; i
View Code

 

转载于:https://www.cnblogs.com/YY56/p/4692599.html

你可能感兴趣的文章
jz1074 【基础】寻找2的幂
查看>>
Wannafly模拟赛5 A 思维 D 暴力
查看>>
【Linux开发】CCS远程调试ARM,AM4378
查看>>
Linux之ssh服务介绍
查看>>
排序:冒泡排序
查看>>
Java中instanceof关键字的用法总结
查看>>
引用类型-Function类型
查看>>
(转)Android 仿订单出票效果 (附DEMO)
查看>>
数据库多张表导出到excel
查看>>
微信小程序去除button默认样式
查看>>
Where does Visual Studio look for C++ Header files?
查看>>
Java打包可执行jar包 包含外部文件
查看>>
Windows Phone开发(37):动画之ColorAnimation
查看>>
js中escape,encodeURI,encodeURIComponent 区别(转)
查看>>
sass学习笔记-安装
查看>>
Flask (二) cookie 与 session 模型
查看>>
修改添加网址的教程文件名
查看>>
[BZOJ 1017][JSOI2008]魔兽地图DotR(树形Dp)
查看>>
裁剪图片
查看>>
数据结构实习 problem L 由二叉树的中序层序重建二叉树
查看>>