Count the Colors(线段树,找颜色段条数)
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
题解:各种错。。。最终发现还是要插a+1,b才可以,因为是颜色段,并不是点,还有,那个n是n条线段,所以查找和建树均要是8000内建,否则会wa;
思路:-1代笔多色,0代表无色;
代码:
#include<iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#include<cmath>
#include<vector>
using namespace std;
const int INF=0x3f3f3f3f;
#define mem(x,y) memset(x,y,sizeof(x))
#define SI(x) scanf("%d",&x)
#define PI(x) printf("%d",x)
#define SD(x,y) scanf("%lf%lf",&x,&y)
#define P_ printf(" ")
#define ll root<<1
#define rr root<<1|1
#define lson ll,l,mid
#define rson rr,mid+1,r
#define V(x) tree[x]
typedef long long LL;
const int MAXN=8010;
int color[MAXN];
int temp;
int tree[MAXN<<2];
void pushdown(int root){
if(V(root)>0){
V(ll)=V(root);
V(rr)=V(root);
V(root)=-1;
}
}
void build(int root,int l,int r){
int mid=(l+r)>>1;
V(root)=0;
if(l==r)return;
build(lson);build(rson);
}
void update(int root,int l,int r,int A,int B,int v){
if(l>=A&&r<=B){
V(root)=v;
return;
}
int mid=(l+r)>>1;
pushdown(root);
if(mid>=A)update(lson,A,B,v);
if(mid<B)update(rson,A,B,v);
V(root)=-1;
}
void query(int root,int l,int r){
int mid=(l+r)>>1;
if(temp==V(root))return;
if(!V(root)){
temp=0;return;
}
if(V(root)!=-1){
if(temp!=V(root)){
temp=V(root);
color[temp]++;
return;
}
return;
}
if(l==r)return;
query(lson);
query(rson);
}
int main(){
int n;
while(~scanf("%d",&n)){
mem(color,0);
build(1,1,8000);
int a,b,c;
int t=n;
while(t--){
scanf("%d%d%d",&a,&b,&c);
update(1,1,8000,a+1,b,c+1);
}
query(1,1,8000);
for(int i=1;i<=8001;i++){
if(color[i])printf("%d %d\n",i-1,color[i]);
}puts("");
}
return 0;
}
Count the Colors(线段树,找颜色段条数)的更多相关文章
- W同学的新画板 QDUOJ 线段树 区间颜色段数
		
W同学的新画板 QDUOJ 线段树 区间颜色段数 原题链接 题意 W同学在每天的刻苦学习完成功课之余,都会去找一些有趣的事情来放松自己:恰巧今天他收到了朋友送给他的一套画板,于是他立刻拆开了包装,拿出 ...
 - ZOJ 1610 Count the Colors (线段树成段更新)
		
题意 : 给出 n 个染色操作,问你到最后区间上能看见的各个颜色所拥有的区间块有多少个 分析 : 使用线段树成段更新然后再暴力查询总区间的颜色信息即可,这里需要注意的是给区间染色,而不是给点染色,所以 ...
 - [ZOJ1610]Count the Colors(线段树,区间染色,单点查询)
		
题目链接:http://www.icpc.moe/onlinejudge/showProblem.do?problemCode=1610 题意:给一个长8000的绳子,向上染色.一共有n段被染色,问染 ...
 - zoj 1610 Count the Colors 线段树区间更新/暴力
		
Count the Colors Time Limit: 1 Sec Memory Limit: 256 MB 题目连接 http://acm.zju.edu.cn/onlinejudge/show ...
 - ZOJ 1610 Count the Colors(线段树,区间覆盖,单点查询)
		
Count the Colors Time Limit: 2 Seconds Memory Limit: 65536 KB Painting some colored segments on ...
 - ZOJ-1610 Count the Colors ( 线段树 )
		
题目链接: http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1610 Description Painting some co ...
 - ZOJ1610 Count the Colors —— 线段树 区间染色
		
题目链接:https://vjudge.net/problem/ZOJ-1610 Painting some colored segments on a line, some previously p ...
 - Count the Colors 线段树
		
题目 参考博客地址 题意: n范围[1,8000] , li 和 ri 的范围[0,8000]. n个操作,每个操作是把 [li , ri]内的点修改成一个颜色c. n个操作过后,按颜色从小到大 ...
 - 【POJ 2777】 Count Color(线段树区间更新与查询)
		
[POJ 2777] Count Color(线段树区间更新与查询) Time Limit: 1000MS Memory Limit: 65536K Total Submissions: 4094 ...
 
随机推荐
- 数据结构-B树
			
1.前言: 动态查找树主要有:二叉查找树(Binary Search Tree),平衡二叉查找树(Balanced Binary Search Tree),红黑树(Red-Black Tree ) ...
 - Linux PCI网卡驱动的详细分析
			
学习应该是一个先把问题简单化,在把问题复杂化的过程.一开始就着手处理复杂的问题,难免让人有心惊胆颤,捉襟见肘的感觉.读Linux网卡驱动也是一 样.那长长的源码夹杂着那些我们陌生的变量和符号,望而生畏 ...
 - 习题3.4 & 3.5: 求两链表的交集和并集
			
#include<stdio.h> #include<stdlib.h> struct Node; typedef struct Node *PtrToNode; typede ...
 - oracle check if the display variable is set
 - 此证书的签发者无效Missing iOS Distribution signing identity问题解决
			
问题描述 今天准备打包上传AppStore,结果Xcode报以下错误:Missing iOS Distribution signing identity for XXXXXX 查看证书后发现,Deve ...
 - vmware虚拟机迁移导致的eth0消失问题
			
将原来的ubuntu虚拟机文件迁移到还有一台机子之后. ifconfig显示仅仅有一个lo网卡,网上找了一些文章.大多是改动/etc/network/interfaces 原来内容是 # ###### ...
 - HDU 4729 An Easy Problem for Elfness (主席树,树上第K大)
			
转载请注明出处,谢谢http://blog.csdn.net/ACM_cxlove?viewmode=contents by---cxlove 题意:给出一个带边权的图.对于每一个询问(S , ...
 - 自己动手写CPU之第五阶段(3)——MIPS指令集中的逻辑、移位与空指令
			
将陆续上传本人写的新书<自己动手写CPU>(尚未出版),今天是第17篇.我尽量每周四篇 5.4 逻辑.移位操作与空指令说明 MIPS32指令集架构中定义的逻辑操作指令有8条:and.and ...
 - python单/双下划线使用
			
在Python编程中经常会遇到函数(function),方法(method)及属性(attribute)以下划线'_'作为前缀,这里做个总结. 主要存在四种情形: 1. object # public ...
 - 从头开始-02.C语言基础
			
变量的内存分析: #include <stdio.h> int main() { //内存地址由大到小 int a=10; int b=20; //&是一个地址运算符,取得变量的地 ...