任意门: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

题意概括:

多测试样例,每个有 N 次操作,对区间 [a, b] 涂色,是涂区间是涂区间,不是涂区间的点!

是很别扭,举个栗子:涂 1-2 和 3-4,如果是涂点那么 1、2、3、4都涂了,如果是涂区间那么 2-3 区间没有涂。

解题思路:

线段树更新修改区间(把原来的数组看成一个点,两点之间的差是区间),暴力单点查询每点的颜色值,统计每种可以看得见的颜色有几段。

AC code:

 #include <cstdio>
#include <iostream>
#include <algorithm>
#include <cstring>
#define INF 0x3f3f3f3f
#define LL long long int
#define lson l, mid, root<<1
#define rson mid+1, r, root<<1|1
using namespace std;
const int MAXN = ;
struct date{
int L, R, val;
}node[MAXN];
int lyt[MAXN<<];
int cnt[MAXN];
int N, max_R, max_C; void PushDown(int root)
{
if(lyt[root] >= ){
lyt[root<<] = lyt[root<<|] = lyt[root];
lyt[root] = -;
}
}
void Update(int L, int R, int l, int r, int root, int val)
{
if(L <= l && r <= R){lyt[root] = val;return;}
int mid = (l+r)>>;
PushDown(root);
if(L <= mid) Update(L, R, lson, val);
if(R > mid) Update(L, R, rson, val);
}
int Query(int pos, int l, int r, int root)
{
if(l == r) return lyt[root];
int ret;
PushDown(root);
int mid = (l+r)>>;
if(pos <= mid) ret = Query(pos, lson);
if(pos > mid) ret = Query(pos, rson);
return ret;
}
void init()
{
max_R = ; max_C = ;
memset(cnt, , sizeof(cnt));
memset(lyt, -, sizeof(lyt));
}
int main()
{
while(~scanf("%d", &N)){
init(); //初始化
for(int i = ; i < N; i++){ //输入
scanf("%d%d%d", &node[i].L, &node[i].R, &node[i].val);
node[i].L++;
max_R = max(node[i].R, max_R);
max_C = max(max_C, node[i].val);
}
for(int i = ; i < N; i++){ //建树
if(node[i].R >= node[i].L)
Update(node[i].L, node[i].R, , max_R, , node[i].val);
}
int last_color=-, now_color;
for(int i = ; i <= max_R; i++)
{
now_color = Query(i, , max_R, );
if(now_color == -){last_color = -; continue;} ///没有染色
if(now_color != last_color) cnt[now_color]++; ///该颜色断层
last_color = now_color;
}
for(int i = ; i <= max_C; i++){
if(cnt[i]>) printf("%d %d\n", i, cnt[i]);
}
puts("");
}
return ;
}

最新文章

  1. MVC控制器总结
  2. 【转】【Java】利用反射技术,实现对类的私有方法、变量访问
  3. POJ1351 Number of Locks(数学)
  4. wpf,CollectionViewSource,使用数据过滤 筛选 功能。
  5. zw版【转发&#183;台湾nvp系列Delphi例程】HALCON TestObjDef
  6. Swift中UITableView的简单使用
  7. JS 事件冒泡整理 浏览器的事件流
  8. EasyUI DataGrid及Pagination
  9. phpexcel用法(转)
  10. 从分治算法到 Hadoop MapReduce
  11. jenkins git ftp 发布.net 项目
  12. maven 工程依赖了某个jar包 但还是报java.lang.ClassNotFoundException的问题
  13. selenium:断言
  14. ios开发之--NSURL的用法
  15. 分页插件 PageHelper
  16. 十五、springboot集成定时任务(Scheduling Tasks)(二)之(线程配置)
  17. [转载]如何使用eclipse 生成runnable jar包
  18. 聊聊编程开发的数据库批量插入(sql)
  19. android 远程Service以及AIDL的跨进程通信
  20. Python中的import和from import

热门文章

  1. web服务器架构演化及所其技术知识体系(分布式的由来)
  2. (转) CentOS 7添加开机启动服务/脚本
  3. 【JAVA】重载和重写的区别
  4. 面试题-Java设计模式举例
  5. Scala 知识点掌握2
  6. C++中 结构体和类的异同
  7. SQL Server 2008 R2如何开启数据库的远程连接(转)
  8. CocoaPods管理的项目移植到别人电脑后找不到头文件
  9. 02_SimpleTrigger
  10. Jmeter(一)http接口添加header和cookie --转载