链接:

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的更多相关文章

  1. F - Count the Colors ZOJ - 1610 线段树染色(染区间映射)

    题意:给一段0-8000的线段染色 问最后 颜色x 有几段 题解:标准线段树  但是没有push_up  最后查询是单点按顺序查询每一个点 考虑过使用区间来维护不同的线段有多少种各色的线段  思路是 ...

  2. F - Count the Colors - zoj 1610(区间覆盖)

    有一块很长的画布,现在想在这块画布上画一些颜色,不过后面画的颜色会把前面画的颜色覆盖掉,现在想知道画完后这块画布的颜色分布,比如 1号颜色有几块,2号颜色有几块.... *************** ...

  3. Count the Colors ZOJ - 1610 区间颜色覆盖

    #include<stdio.h> #include<iostream> #include<string.h> #include<algorithm> ...

  4. 线段树区间染色 ZOJ 1610

    Count the Colors ZOJ - 1610 传送门 线段树区间染色求染色的片段数 #include <cstdio> #include <iostream> #in ...

  5. F - Count the Colors

    F - Count the Colors ZOJ - 1610   思路:调了一个小时,但是发现自己线段树木有写错,颜色统计出了错误.但是不明白自己颜色统计为什么错了. 求大佬指点迷津.思路很简单,就 ...

  6. Count Colour_poj2777(线段树+位)

    POJ 2777 Count Color (线段树)   Count Color Time Limit: 1000MS   Memory Limit: 65536K Total Submissions ...

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

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

  8. ZOJ 1610 Count the Colors【题意+线段树区间更新&&单点查询】

    任意门:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1610 Count the Colors Time Limit: 2 ...

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

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

随机推荐

  1. mysql 存储过程分页 转载

    /* --名称:MYSQL版查询分页存储过程 by peace 2013-8-14 --输入参数:@fields -- 要查询的字段用逗号隔开 --输入参数:@tables -- 要查询的表 --输入 ...

  2. ant使用备忘

    ant是一个脚本构建工具,可能就是持续集成里面所需要的构建工具. 如果使用eclipse,里面会自带有ant工具,不需要再安装了,创建一个build.xml(或者其他的名字都可以),使用ant来运行就 ...

  3. apache commons pool

    apache commons下的pool 其中的borrowObject函数源代码显示其产生可用对象的过程: 如果stack中有空闲的对象,则pop对象,激活对象(activate函数),验证对象(v ...

  4. 前后端分离 开发环境通过CORS实现跨域联调

    通过JSONP实现跨域已是老生常谈,JSONP跨域限制多,最近了解了一下CORS. 参考: https://developer.mozilla.org/zh-CN/docs/Web/HTTP/Acce ...

  5. MySql ERROR 1045 (28000): Access denied 错误

    [MySql ERROR 1045 (28000): Access denied 错误] 即登陆错误,使用MySql登陆时,需使正确的username/passwd.通过 -u -p 参数即可. My ...

  6. 70. Climbing Stairs (Array; DP)

    You are climbing a stair case. It takes n steps to reach to the top. Each time you can either climb ...

  7. Ubuntu cd

    查看目录文件内容 ./  or filename/file.* cd 返回用户主目录 ~,,,,,/home/user cd ..不管用 cd / 返回用户根目录 root

  8. oracle基本查询入门(二) 子查询

    一.子查询语法 SELECT select_list FROM table WHERE expr operator (SELECT select_list FROM table); 子查询在主查询之前 ...

  9. Castle ActiveRecord学习(五)使用HQL语句查询

    来源:http://www.cnblogs.com/Terrylee/archive/2006/04/12/372823.html 一.HQL简单介绍HQL全名是Hibernate Query Lan ...

  10. Halcon小函数的封装和代码导出

    一.Halcon小函数的封装和修改 1.名词解释: 算子:指Halcon中最基础.最底层的函数(即你看不到它的代码实现),一个算子只有一句话,例如threshold算子. 小函数:由多个算子组合成的函 ...