Question:
Count the Colors
Time Limit: 2000 msMemory 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
Analysis:
这道题的题意是求多次叠加涂颜色后有多少段不同的颜色。核心思想是线段树的key设为颜色,然后进行赋值后数色。每次初始化只需要初始节点的key为无色即可。
Code:
#include <iostream>
#include<string.h>
using namespace std;
#define maxn 8000
#define NOCOLOR -1
#define DIVCOLOR -2
//线段树节点
struct node
{
int left;
int right;
int key; //存储颜色
} Tree[maxn * 3];
//初始化线段树
void Init(int l, int r, int index)
{
Tree[index].left = l;
Tree[index].right = r;
if (Tree[index].left + 1 == Tree[index].right)
return;
int mid = (l + r) / 2;
Init(l, mid, 2 * index);
Init(mid, r, 2 * index + 1);
}
void Insert(int l, int r, int index, int key)
{
//相同颜色则不做插入
if (Tree[index].key == key)
return;
//可以完全覆盖
if (Tree[index].left == l && Tree[index].right == r)
{
Tree[index].key = key;
return;
}
//不能完全覆盖
//如果不是已经分好的,要么就是这一个段全都是一个颜色,要么就是没颜色
if (Tree[index].key != DIVCOLOR)
{
Tree[2 * index].key = Tree[index].key;
Tree[2 * index + 1].key = Tree[index].key;
}
Tree[index].key = DIVCOLOR;
int mid = (Tree[index].left + Tree[index].right) / 2;
if (r <= mid)
{
Insert(l, r, 2 * index, key);
return;
}
if (l >= mid)
{
Insert(l, r, 2 * index + 1, key);
return;
}
Insert(l, mid, 2 * index, key);
Insert(mid, r, 2 * index + 1, key);
}
int colorsave = -1;
int m[maxn + 5];
void countcolor(int index) //搜索Tree[index]有多少种颜色
{
if (Tree[index].key == DIVCOLOR) //里面有多种颜色
{//递归遍历子树
countcolor(2 * index);
countcolor(2 * index + 1);
}
if (Tree[index].key == NOCOLOR) //没有颜色
{
colorsave = NOCOLOR;
}
if (Tree[index].key >= 0) //有同一种颜色
{
if (Tree[index].key != colorsave) //如果和前面的颜色不同,则要记录
{
m[Tree[index].key]++;
colorsave = Tree[index].key;
}
return; //和前面的颜色相同,则直接return
}
}
int main()
{
int n;
int l, r, k;
//建树
Init(0, maxn, 1);
while (cin >> n)
{
//初始树
Tree[1].key = NOCOLOR;
while (n--)
{
cin >> l >> r >> k;
Insert(l, r, 1, k); //插入树
}
memset(m, 0, sizeof(m)); //初始化m列表
//计算颜色数量
countcolor(1);
//输出结果
for (int i = 0; i <= maxn; i++)
{
if (m[i])
{
cout << i << " " << m[i] << endl;
}
}
cout << endl;
}
return 0;
}