链接:

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. 2017.1.9版给信息源新增:max_len、max_db字段

    2017.1.8a版程序给信息源增加max_len.max_db字段,分别用于控制:获取条数.数据库保留条数. max_len的说明见此图: max_db的说明见此图: 当max_len和max_db ...

  2. 201671010127 2016—2017—2 Java怎样解决Java程序中中文乱码的问题。

    这是本次第二次分享新手在编程中遇到的问题,相信很多Java新手和我一样,在Java编程中会遇到中文乱码的情况,下面我就给大家分享我遇到问题和解决问题的具体过程. 我先用Notepad++写了一个如下的 ...

  3. Gradle with Android

    [Gradle with Android] The Android Studio build system is based on Gradle, and the Android plugin for ...

  4. JsonConvert.SerializeObject 空值处理

    var settings = new JsonSerializerSettings() { ContractResolver= new NullToEmptyStringResolver() }; v ...

  5. c++ 自动对象

    转自: https://www.cnblogs.com/geloutingyu/p/8034904.html 1.自动对象默认情况下,局部变量的生命期局限于所在函数的每次执行期间.只有当定义它的函数被 ...

  6. 详解JMeter正则表达式

    详解JMeter正则表达式(1) 1.概览 JMeter中包含范本匹配软件Apache Jakarta ORO .在Jakarta网站上有一些关于它的文档,例如a summary of the pat ...

  7. oracle两个客户端路径记录

    32 C:\WINDOWS\assembly\GAC_64\Oracle.DataAccess\2.112.3.0__89b483f429c4734264 C:\WINDOWS\Microsoft.N ...

  8. 使用JMX监控Storm的nimbus、supervisor、woker

    可以通过在storm.yaml中增加如下样例的配置, 启动JMX来监控storm的各个角色. 其中对于Worker的监控,因为一个节点上可以有多个work,为了防止端口号重复导致启动失败,所以用动态代 ...

  9. cuda使用

    import torchx = torch.randn(2, 3)x = x.cuda() print (x)#最简单的cuda使用,但是感觉好慢啊

  10. android的 Base64

    byte[] key=Base64.decode("YWJjZGVmZ2hpamtsbW5vcHFyc3R1dnd4".getBytes(), Base64.DEFAULT);   ...