nbut线段树专题A - Count the Colors

Description

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.


刚开始没发现区间是左开右闭的,样例怎么都不对。。。

对于这题的话,我们用颜色来做标记,初始化建树时赋值为-1,建一棵空树,每一次找到要更新的区间,更新颜色。访问的时候,判断如果此区间是有颜色的,那么把颜色和区间左右端点记录下来,最后去判断有几种颜色几个段。。

#include<stdio.h>
#include<string.h>
#include<algorithm>
#include<iostream>
using namespace std;
const int maxn=8010;

struct node
{
int l,r;
int color;
}tree[maxn<<2];

struct kind
{
int l,r,color;
}temp[maxn<<1];

int hash_color[maxn];
void build(int p,int l,int r)
{
tree[p].l=l;
tree[p].r=r;
tree[p].color=-1;
if(l==r)
return ;
int mid=(l+r)>>1;
build(p<<1,l,mid);
build(p<<1|1,mid+1,r);
}

void update(int p,int l,int r,int val)
{
if(l==tree[p].l && r==tree[p].r)
{
tree[p].color=val;
return ;
}
if(tree[p].color!=-1)//注意,如果父区间有色,那么得把子区间更新
{
tree[p<<1].color=tree[p<<1|1].color=tree[p].color;
tree[p].color=-1;//下次访问到这种颜色可以直接用其子节点来代替
}
int mid=(tree[p].l +tree[p].r)>>1;
if(r<=mid)
update(p<<1,l,r,val);
else if(l>mid)
update(p<<1|1,l,r,val);
else
{
update(p<<1,l,mid,val);
update(p<<1|1,mid+1,r,val);
}
}
int cnt;
void query(int p,int l,int r)
{
if(tree[p].color!=-1)
{
if(tree[p].l==l && tree[p].r==r)
{
temp[cnt].l=l;
temp[cnt].r=r;
temp[cnt++].color=tree[p].color;
return ;
}
}
if(l==r)
return ;
int mid=(tree[p].l + tree[p].r)>>1;
if(r<=mid)
query(p<<1,l,r);
else if(l>mid)
query(p<<1|1,l,r);
else
{
query(p<<1,l,mid);
query(p<<1|1,mid+1,r);
}
}

int main()
{
int n;
while(~scanf("%d",&n))
{
build(1,1,maxn);
cnt=0;
int x,y,c;
for(int i=0;i<n;i++)
{
scanf("%d%d%d",&x,&y,&c);
x++;
update(1,x,y,c);
}
query(1,1,maxn);
memset(hash_color,0,sizeof(hash_color));
hash_color[temp[0].color]++;
for(int i=1;i<cnt;i++)
{
if(temp[i].color!=temp[i-1].color)
hash_color[temp[i].color]++;
else
if(temp[i].l>temp[i-1].r+1)
hash_color[temp[i].color]++;
}
for(int i=0;i<maxn;i++)
if(hash_color[i])
printf("%d %d\n",i,hash_color[i]);
printf("\n");
}
return 0;
}


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值