Time Limit: 2000MS | Memory Limit: 65536KB | 64bit IO Format: %lld & %llu |
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
#include <iostream>
#include<cstdio>
#include<cstring>
#include<algorithm>
#define lson rt<<1,L,M
#define rson rt<<1|1,M+1,R
using namespace std;
const int maxn=8010;
int C[maxn*8+10];///区间标记,lazy标记
int Set[2*maxn];///记录每个点的颜色
int seg[2*maxn];///记录每个颜色的段数
void push_down(int rt)
{
if(C[rt]!=-1)
{
C[rt<<1]=C[rt<<1|1]=C[rt];
C[rt]=-1;
}
}
void push_up(int rt)
{
C[rt]=(C[rt<<1]==C[rt<<1|1]?C[rt<<1]:-1);
}
void update(int rt,int L,int R,int sl,int sr,int v)
{
if(sl<=L&&sr>=R)
{
C[rt]=v;
return ;
}
else
{
int M=(L+R)>>1;
push_down(rt);
if(sl<=M) update(lson,sl,sr,v);
if(sr>M) update(rson,sl,sr,v);
push_up(rt);
}
}
void query(int rt,int L,int R)
{
if(L==R)
{
Set[L]=C[rt];
}
else
{
int M=(L+R)>>1;
push_down(rt);
query(lson);
query(rson);
}
}
int main()
{
int n;
while(~scanf("%d",&n))
{
memset(C,-1,sizeof(C));
memset(seg,0,sizeof(seg));
memset(Set,-1,sizeof(Set));
int s=0;
for(int i=0; i<n; i++)
{
int st,en,c;
scanf("%d%d%d",&st,&en,&c);
st=st*2,en=en*2-1;
update(1,0,2*maxn,st,en,c);
}
query(1,0,2*maxn);
for(int i=0; i<2*maxn; i++)
{
if(Set[i]!=Set[i+1]&&Set[i]>=0)
{
seg[Set[i]]++;
}
}
for(int i=0; i<maxn; i++)
{
if(seg[i])
printf("%d %d\n",i,seg[i]);
}
printf("\n");
}
return 0;
}
#include <iostream>
#include<cstdio>
#include<cstring>
#define lson rt<<1,L,M
#define rson rt<<1|1,M,R
using namespace std;
const int maxn=8010;
int C[maxn<<2];
int seg[maxn+10];
int Set[maxn+10];
int k;
void push_down(int rt)
{
if(C[rt]>=0)
{
C[rt<<1]=C[rt<<1|1]=C[rt];
C[rt]=-1;
}
}
void update(int rt,int L,int R,int sl,int sr,int v)
{
if(sl<=L&&sr>=R)
{
C[rt]=v;
}
else
{
int M=(L+R)>>1;
push_down(rt);
if(sl<M)
update(lson,sl,sr,v);
if(sr>M)
update(rson,sl,sr,v);
}
}
void query(int rt,int L,int R)
{
if(L+1==R)
{
Set[k++]=C[rt];
}
else
{
int M=(L+R)>>1;
push_down(rt);
query(lson);
query(rson);
}
}
int main()
{
int n;
while(~scanf("%d",&n)){
memset(C,-1,sizeof(C));
memset(seg,0,sizeof(seg));
memset(Set,-1,sizeof(Set));
for(int i=0;i<n;i++){
int st,en,v;
scanf("%d%d%d",&st,&en,&v);
st++,en++;
update(1,1,maxn,st,en,v);
}
k=0;
query(1,1,maxn);
for(int i=0;i<k;i++){
if(Set[i]!=Set[i+1]&&Set[i]>=0){
seg[Set[i]]++;
}
}
for(int i=0;i<maxn;i++){
if(seg[i]){
printf("%d %d\n",i,seg[i]);
}
}
printf("\n");
}
return 0;
}