用线段树写真是累.....尤其是最近在用JAVA做项目 怎么NEW 指针和引用 都混了/........哭~~~~~~~~~~
先水一个可以AC的代码
int
main()
{
int n;
while(scanf("%d",&n)!=EOF)
{
int Color[8000];
int NumColor[8000];
memset(Color,-1,sizeof(Color));
memset(NumColor,0,sizeof(NumColor));
int i;
int left, right, color;
for(i = 0; i < n; i++)
{
scanf("%d%d%d", &left, &right, &color);
for(int j=left;j<right;j++)
{
Color[j]=color;
}
}
int hold=-1;
for(i = 0; i < 8000; i++)
{
if(Color[i]!=hold || hold == -1)
{
NumColor[Color[i]]++;
hold=Color[i];
}
}
for(i = 0; i < 8000; i++)
{
if(NumColor[i]!=0)
printf("%d %d\n",i,NumColor[i]);
}
printf("\n");
}
return 0;
}
再线段树
int
Color[
8000
];
int
NumColor[
8000
];
struct
TNode{
int
left, right;
int
col;
TNode
*
LeftChild,
*
RightChild;
TNode(){};
void
TNODE(
int
,
int
);
void
Insert(
int
,
int
,
int
);
void
Count();
};
void
TNode::TNODE(
int
left,
int
right)
{
this
->
left
=
left;
this
->
right
=
right;
if
(right
-
left
==
1
)
{
LeftChild
=
NULL;
RightChild
=
NULL;
}
else
{
int
mid
=
(left
+
right)
/
2
;
TNode Tl;
TNode Tr;
LeftChild
=&
Tl;
RightChild
=&
Tr;
LeftChild
->
TNODE(left,mid);
RightChild
->
TNODE(mid,right);
}
}
void
TNode::Insert(
int
left,
int
right,
int
color)
{
if
(
this
->
col
==
color)
{}
else
{
if
( left
==
this
->
left
&&
right
==
this
->
right )
this
->
col
=
color;
else
{
int
mid
=
(
this
->
left
+
this
->
right)
/
2
;
if
(
this
->
col
!=-
2
)
{
LeftChild
->
col
=
color;
RightChild
->
col
=
color;
}
this
->
col
=-
2
;
if
(left
<=
mid)
{
LeftChild
->
Insert(left,right,color);
}
if
(right
>=
mid)
{
RightChild
->
Insert(left,right,color);
}
LeftChild
->
Insert(left,mid,color);
RightChild
->
Insert(mid,right,color);
}
}
}
void
TNode::Count()
{
if
(col
!=-
2
&&
col
!=-
1
)
{
int
i;
for
(i
=
left;i
<
right;i
++
)
Color[i]
=
col;
}
if
(col
==-
2
)
{
LeftChild
->
Count();
RightChild
->
Count();
}
}
int
main()
{
int
n;
while
(scanf(
"
%d
"
,
&
n)
!=
EOF)
{
int
i;
int
left, right, color;
TNode T;
T.TNODE(
0
,
8000
);
memset(Color,
0
,
sizeof
(Color));
memset(NumColor,
0
,
sizeof
(NumColor));
for
(i
=
0
; i
<
n; i
++
)
{
scanf(
"
%d%d%d
"
,
&
left,
&
right,
&
color);
T.Insert(left,right,color);
}
int
hold
=-
1
;
for
(i
=
0
;i
<
8000
;i
++
)
{
if
(Color[i]
!=
hold
||
hold
==-
1
)
{
NumColor[hold]
++
;
hold
=
Color[i];
}
}
for
(i
=
0
;i
<
8000
;i
++
)
{
if
(NumColor[i]
!=
0
)
printf(
"
%d %d\n
"
,i,NumColor[i]);
}
}
return
0
;
}