题目链接:https://vjudge.net/problem/ZOJ-1610

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.

<b< dd="">

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.

<b< dd="">

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

<b< dd="">

Sample Output

1 1
2 1
3 1

1 1

0 2
1 1

题解:

问最终用多少段颜色相同的区域。经典的区间染色问题。

写法一:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const double EPS = 1e-;
const int INF = 2e9;
const LL LNF = 2e18;
const int MAXN = 1e4+; int val[MAXN<<];
int num[MAXN], color[MAXN]; void push_down(int u)
{
if(val[u]>=)
{
val[u*] = val[u*+] = val[u];
val[u] = -;
}
} void set_val(int u, int l, int r, int x, int y, int _val)
{
if(x<=l && r<=y)
{
val[u] = _val;
return;
} push_down(u);
int mid = (l+r)>>;
if(x<=mid) set_val(u*, l, mid, x, y, _val);
if(y>=mid+) set_val(u*+, mid+, r, x, y, _val);
} void query(int u, int l, int r)
{
if(l==r)
{
color[l] = val[u];
return;
} push_down(u);
int mid = (l+r)>>;
query(u*, l, mid);
query(u*+, mid+, r);
} int main()
{
int m;
while(scanf("%d", &m)!=EOF)
{
memset(val, -, sizeof(val));
for(int i = ; i<=m; i++)
{
int x, y, z;
scanf("%d%d%d", &x, &y, &z);
if(x<y) set_val(, , , x+, y, z);
} query(, , );
memset(num, , sizeof(num));
for(int i = ; i<=; i++)
if(color[i]!=- && (i== || color[i]!=color[i-]))
num[color[i]]++; for(int i = ; i<=; i++)
if(num[i])
printf("%d %d\n", i, num[i]);
printf("\n");
}
}

写法二:

 #include <iostream>
#include <cstdio>
#include <cstring>
#include <cmath>
#include <algorithm>
#include <vector>
#include <queue>
#include <stack>
#include <map>
#include <string>
#include <set>
using namespace std;
typedef long long LL;
const double EPS = 1e-;
const int INF = 2e9;
const LL LNF = 2e18;
const int MAXN = 1e4+; int val[MAXN<<];
int num[MAXN]; void push_down(int u)
{
if(val[u]>=)
{
val[u*] = val[u*+] = val[u];
val[u] = -;
}
} void set_val(int u, int l, int r, int x, int y, int _val)
{
if(x<=l && r<=y)
{
val[u] = _val;
return;
} push_down(u);
int mid = (l+r)>>;
if(x<=mid) set_val(u*, l, mid, x, y, _val);
if(y>=mid+) set_val(u*+, mid+, r, x, y, _val);
} int pre;
void query(int u, int l, int r)
{
if(l==r)
{
if(val[u]>= && val[u]!=pre)
num[val[u]]++;
pre = val[u];
return;
} push_down(u);
int mid = (l+r)>>;
query(u*, l, mid);
query(u*+, mid+, r);
} int main()
{
int m;
while(scanf("%d", &m)!=EOF)
{
memset(val, -, sizeof(val));
for(int i = ; i<=m; i++)
{
int x, y, z;
scanf("%d%d%d", &x, &y, &z);
if(x<y) set_val(, , , x+, y, z);
} memset(num, , sizeof(num));
pre = -;
query(, , );
for(int i = ; i<=; i++)
if(num[i])
printf("%d %d\n", i, num[i]);
printf("\n");
}
}

最新文章

  1. Emeditor批量修改文件编码格式(UTF-8)
  2. 游标、动态sql、异常
  3. Tornado
  4. case when遇到空串转成0
  5. ajax局部刷新分页
  6. 20151210--MVC
  7. 设计模式 ( 二十 ) 访问者模式Visitor(对象行为型)
  8. hdu 5090 Game with Pearls
  9. mybatis入门-mapper代理原理
  10. Css3颜色值RGBA得表示方式
  11. [PHP] Phalcon操作示范
  12. jdk和tomcat配置
  13. Python--day14(迭代器)
  14. 如何在python脚本下启动django程序
  15. vue动态class——实现tag的选中状态
  16. Asp.Net 初级 高级 学习笔记
  17. promise用法详解
  18. gcc常用命令使用
  19. [C#]async和await刨根问底
  20. 搭建安卓开发环境 hello world andriod

热门文章

  1. 1. node.js环境搭建 第一行代码
  2. asp网页无法打开
  3. 省市区名称code
  4. Python变量及数据类型
  5. zoj 2947 Abbreviation
  6. zoj 1383 Binary Numbers
  7. var声明的成员变量和函数内声明的变量区别
  8. 从零到一,使用实时音视频 SDK 一起开发一款 Zoom 吧
  9. bzoj2190 [SDOI2008]仪仗队 - 筛法 - 欧拉函数
  10. python学习之-- IO多路复用 select模块