nbut线段树专题A - Count the Colors
来源:互联网 发布:完美告白知乎 编辑:程序博客网 时间:2024/06/05 15:19
Description
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;
}
- nbut线段树专题A - Count the Colors
- nbut线段树专题A - Count the Colors
- 线段树专题—ZOJ1610 Count the Colors
- ZOJ1610 Count the Colors,线段树
- ZOJ 1610Count the Colors//线段树
- zoj 1610 Count the Colors【线段树】
- zoj 1610 Count the Colors[线段树]
- 【线段树】Count the Colors (zoj 1610)
- zoj 1610 Count the Colors(线段树)
- zoj1610 Count the Colors(线段树)
- zoj 1610 Count the Colors 线段树
- 线段树之ZOJ1610 Count the Colors
- ZOJ - 1610 Count the Colors(线段树)
- ZOJ 1610 Count the Colors【线段树】
- Count the Colors(线段树染色)
- ZOJ----1610-Count the Colors(线段树)
- zoj1610( 线段树)(Count the Colors)
- ZJU 1610 Count the Colors 线段树
- nbut线段树专题C - Mayor\'s posters
- nbut线段树专题W - Frequent values
- nbut线段树专题M - Buy Tickets
- nbut线段树专题 J - Atlantis
- 我对离散化的一些感悟
- nbut线段树专题A - Count the Colors
- nbut线段树专题A - Count the Colors
- nbut线段树线段树专题S - Black And White
- NBUT-2014暑期集训专题练习1 -> 二分法 N - N
- nbut线段树专题P - Hotel
- nbut线段树专题Q - Tunnel Warfare
- 如何观看Netflix的美国
- WhatsApp发布安卓手表客户端 手表聊天时代来了
- apt命令