题目链接:http://acm.zju.edu.cn/onlinejudge/showProblem.do?problemCode=1610点击打开链接
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
有个坑点就是有0颜色 每次判断与前一个颜色是否相同 用一个数组记录每个颜色出现的段数 特别注意一下第一个的判断
另外不能偷懒直接用flag当颜色 还是因为那个0。。
第二遍写的时候偷懒结果错了好几次 还是得踏实做事
#include <bits/stdc++.h>
using namespace std;
#define maxn 80000+5
struct xjy
{
int left;
int right;
int target;
int lazy;
int sum;
};
xjy tree[maxn<<2];
int a[maxn];
int ans=0;
void build(int i,int left ,int right)
{
if(left==right)
{
tree[i].left=left;
tree[i].right=right;
tree[i].sum=-1;
return ;
}
int mid=(left+right)>>1;
build(i<<1,left,mid);
build (i<<1|1,mid+1,right);
tree[i].sum=-1;
tree[i].left=left;
tree[i].right=right;
}
void updateregion(int i,int left,int right,int val)
{
if(tree[i].lazy)
{
if(tree[i].left==tree[i].right)
{
tree[i].lazy=0;
tree[i].target=0;
}
else
{
tree[i].lazy=0;
tree[i<<1].lazy=tree[i<<1|1].lazy=1;
tree[i<<1].target=tree[i<<1|1].target=tree[i].target;
tree[i<<1].sum=tree[i].target;
tree[i<<1|1].sum=tree[i].target;
tree[i].target=0;
}
}
if(tree[i].left==left&&tree[i].right==right)
{
tree[i].lazy=1;
tree[i].target=val;
tree[i].sum=val;
return;
}
int mid=(tree[i].left+tree[i].right)>>1;
if(right<=mid)
updateregion(i<<1,left,right,val);
else if(left>mid)
updateregion(i<<1|1,left,right,val);
else
{
updateregion(i<<1,left,mid,val);
updateregion(i<<1|1,mid+1,right,val);
}
}
int midmid;
void query(int i)
{
if(tree[i].lazy)
{
if(tree[i].left==tree[i].right)
{
tree[i].lazy=0;
tree[i].target=0;
}
else
{
tree[i].lazy=0;
tree[i<<1].lazy=tree[i<<1|1].lazy=1;
tree[i<<1].target=tree[i<<1|1].target=tree[i].target;
tree[i<<1].sum=tree[i].target;
tree[i<<1|1].sum=tree[i].target;
tree[i].target=0;
}
}
if(tree[i].left==tree[i].right)
{
if(tree[i].sum!=midmid&&tree[i].sum!=-1)
a[tree[i].sum]++;
midmid=tree[i].sum;
return;
}
int mid=(tree[i].left+tree[i].right)>>1;
query(i<<1);
query(i<<1|1);
}
int main()
{
int n,m;
int t;
while(~scanf("%d",&t))
{
for(int i=0;i<maxn;i++)
{
tree[i].lazy=0;
tree[i].left=0;
tree[i].right=0;
tree[i].sum=0;
tree[i].target=0;
a[i]=0;
}
build(1,1,maxn);
int l,r,col;
for(int i=1;i<=t;i++)
{
scanf("%d%d%d",&l,&r,&col);
updateregion(1,l+1,r,col);
}
midmid=-1;
query(1);
for(int i=0;i<maxn;i++)
if(a[i])
printf("%d %d\n",i,a[i]);
printf("\n");
}
}