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.
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
题意:输入 区间和颜色
新的颜色会替换掉旧颜色,经过变换
问在所有区间内,颜色占了多少段。
思路:线段树,或者数组
1.线段树,更新查找:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=8100;
int color[maxn],su[maxn];
struct node
{
int l,r,cover;
}tree[4*maxn];
void build(int u,int l,int r)
{
tree[u].l=l;
tree[u].r=r;
tree[u].cover=-1;
if(l==r) return ;
int mid=(l+r)/2;
build(u<<1,l,mid);
build((u<<1)+1,mid+1,r);
}
void get_down(int u)
{
tree[u<<1].cover=tree[u].cover;
tree[(u<<1)+1].cover=tree[u].cover;
tree[u].cover=-1;
}
void update(int u,int l,int r,int co)
{
if(tree[u].cover==co) return ;
if(l<=tree[u].l&&tree[u].r<=r)
{
tree[u].cover=co;
return ;
}
if(tree[u].cover!=-1)
get_down(u);
int mid=(tree[u].l+tree[u].r)>>1;
if(r<=mid) update(u<<1,l,r,co);
else if(l>=mid+1) update((u<<1)+1,l,r,co);
else
{
update(u<<1,l,mid,co);
update((u<<1)+1,mid+1,r,co);
}
if((tree[u<<1].cover==tree[(u<<1)+1].cover)&&(tree[u<<1].cover!=-1))
tree[u].cover=tree[u<<1].cover;
}
void qerty(int u)
{
if(tree[u].cover!=-1)
{
for(int i=tree[u].l;i<=tree[u].r;i++)
{ color[i]=tree[u].cover;
}
return ;
}
if(tree[u].l==tree[u].r) return ;
qerty(u<<1);
qerty((u<<1)+1);
}
int main()
{
int t,x,y,co;
while(scanf("%d",&t)!=EOF)
{
build(1,0,maxn);
memset(color,-1,sizeof(color));
memset(su,0,sizeof(su));
while(t--)
{
scanf("%d%d%d",&x,&y,&co);
update(1,x,y-1,co);
}
qerty(1);
int px=-1;
for(int i=0;i<maxn;i++)
{
if(px!=color[i])
{
px=color[i];
su[px]++;
}
}
for(int i=0;i<maxn;i++)
if(su[i]) printf("%d %d\n",i,su[i]);
printf("\n");
}
return 0;
}
2.数组:
#include<iostream>
#include<algorithm>
#include<cstdio>
#include<cstring>
using namespace std;
const int maxn=8100;
int color[maxn],cot[maxn];
int main()
{
int t,i,n,x,y,c;
while(scanf("%d",&t)!=EOF)
{
memset(color,-1,sizeof(color));
memset(cot,0,sizeof(cot));
int max=-1;
while(t--)
{
scanf("%d%d%d",&x,&y,&c);
for(i=x;i<y;i++)
color[i]=c;
if(max<y) max=y;
}
for(i=0;i<max;i++)
{
while(i!=0&&color[i]!=-1&&color[i]==color[i-1]) i++;
if(color[i]!=-1) cot[color[i]]++;
}
for(i=0;i<maxn;i++)
if(cot[i]) printf("%d %d\n",i,cot[i]);
printf("\n");
}
return 0;
}