链接:

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. ADO数据库编程入门

    ADO 是目前在Windows环境中比较流行的客户端数据库编程技术. ADO是建立在OLE DB底层技术之上的高级编程接口,因而它兼具有强大的数据处理功能(处理各种不同类型的数据源.分布式的数据处理等 ...

  2. 因为链接服务器 "SQLEHR" 的 OLE DB 访问接口 "SQLNCLI10" 无法启动分布式事务

    1.运行 regedt32,浏览至 HKEY_LOCAL_MACHINE\Software\Microsoft\MSDTC. 添加一个 DWORD 值 TurnOffRpcSecurity,值数据为 ...

  3. servlet第三篇

    Servlet是一个供其他Java程序(Servlet引擎)调用的Java类,它不能独立运行,它的运行完全由Servlet引擎来控制和调度. 针对客户端的多次Servlet请求,通常情况下,服务器只会 ...

  4. Group by 内部排序

    1.right join #  update_time  gid=>sid, group_status => s_table select a.* from comment as a ri ...

  5. R画散点图、线型图、箱型图、直方图基本知识

    1.导入数据 2.散点图 plot(iris[,1]~iris[,4],xlab='Length',ylab='Width',col='red',main='Length VS Width')

  6. clusterProfiler包

    1)enrichGO:(GO富集分析) 描述:GO Enrichment Analysis of a gene set. Given a vector of genes, this function ...

  7. The Hard Thing About Hard Things

    1.大多数的管理书籍都是告诉你如何做正确的事,不把事情搞砸.而好书是告诉你,当事情已经搞砸时,你该怎么办. 2.这是个个真实的世界,他们偷走了你的梦想,可你却不知道是谁偷的.

  8. count++线程安全与 synchronized对性能影响的测试

    一个计时器,同时开启100个线程,每个线程休眠1ms钟后,将全局静态变量count加1,这100个线程创建完之后,休眠500ms,计算总耗时,程序如下: public class Counter { ...

  9. 分享至微信、QQ、微博、复制链接

    var share = { "tit": '您的朋友分享了文章', "desc": '分享来自百度文库,包含...', "pic": 'ht ...

  10. 87. Scramble String (String; DP)

    Given a string s1, we may represent it as a binary tree by partitioning it to two non-empty substrin ...