zoukankan
html css js c++ java
线段树521
最近线段树狂写中 争取以后啥都不看就能写出来
#include
<
stdio.h
>
#include
<
string
.h
>
struct
node
{
node
*
pl,
*
pr;
int
left, right;
int
color;
int
colCount;
int
cl, cr;
}
mem[
200020
];
int
memCount;
int
Col[
31
];
int
ColCnt;
int
result;
node
*
newNode()
{
node
*
pt
=
&
mem[memCount
++
];
pt
->
color
=
pt
->
colCount
=
pt
->
cr
=
pt
->
cl
=
1
;
return
pt;
}
node
*
buildTree(
int
l,
int
r)
{
node
*
root
=
newNode();
root
->
left
=
l;
root
->
right
=
r;
if
(r
-
l
>
1
)
{
int
mid
=
(l
+
r)
/
2
;
root
->
pl
=
buildTree(l, mid);
root
->
pr
=
buildTree(mid, r);
}
return
root;
}
void
color(node
*
root,
int
c)
{
root
->
colCount
=
1
;
root
->
color
=
root
->
cl
=
root
->
cr
=
c;
}
void
update(node
*
root,
int
l,
int
r,
int
c)
{
if
(root
->
left
==
l
&&
root
->
right
==
r)
{
color(root, c);
return
;
}
if
(root
->
colCount
==
1
)
{
color(root
->
pl,root
->
color);
color(root
->
pr,root
->
color);
}
if
(root
->
colCount
==
1
&&
root
->
color
==
c)
return
;
root
->
colCount
=
2
;
if
(root
->
pl
->
right
>
l)
{
if
(root
->
pl
->
right
>=
r)
update(root
->
pl,l,r,c);
else
{
int
mid
=
(root
->
left
+
root
->
right)
/
2
;
update(root
->
pl,l,mid,c);
update(root
->
pr,mid,r,c);
}
}
else
{
update(root
->
pr,l,r,c);
}
root
->
cl
=
root
->
pl
->
cl;
root
->
cr
=
root
->
pr
->
cr;
}
void
ques(node
*
root,
int
l,
int
r)
{
if
(root
->
colCount
==
1
)
{
Col[ColCnt
++
]
=
root
->
color;
return
;
}
if
(root
->
right
-
root
->
left
<=
1
)
return
;
if
(root
->
pl
->
right
>
l)
{
if
(root
->
pl
->
right
>=
r)
ques(root
->
pl,l,r);
else
{
int
mid
=
(root
->
left
+
root
->
right)
/
2
;
ques(root
->
pl,l,mid);
ques(root
->
pr,mid,r);
}
}
else
{
ques(root
->
pr,l,r);
}
}
void
clean()
{
int
i, j;
for
(i
=
1
; i
<=
30
; i
++
)
for
(j
=
0
; j
<
ColCnt; j
++
)
{
if
(i
==
Col[j])
{
result
++
;
break
;
}
}
}
int
main()
{
int
l, t, o;
while
(scanf(
"
%d%d%d
"
,
&
l,
&
t,
&
o)
==
3
)
{
memCount
=
0
;
node
*
root
=
buildTree(
0
, l);
int
i;
char
op;
int
l, r, c;
for
(i
=
0
; i
<
o; i
++
)
{
scanf(
"
%s
"
,
&
op);
if
(op
==
'
C
'
)
{
scanf(
"
%d%d%d
"
,
&
l,
&
r,
&
c);
if
(l
<=
r)
update(root,l
-
1
,r,c);
else
update(root,r
-
1
,l,c);
}
else
{
ColCnt
=
0
;
result
=
0
;
memset(Col,
0
,
sizeof
(Col));
scanf(
"
%d%d
"
,
&
l,
&
r);
if
(l
<=
r)
ques(root,l
-
1
, r);
else
ques(root,r
-
1
,l);
clean();
printf(
"
%d\n
"
, result);
}
}
}
return
0
;
}
查看全文
相关阅读:
[二叉查找树] 1115. Counting Nodes in a BST (30)
[最小生成树] 继续畅通工程
[最小生成树] 畅通工程
[最小生成树] 还是畅通工程
[图算法] 1030. Travel Plan (30)
[图算法] 1003. Emergency (25)
[并查集] More is Better
[并查集] How Many Tables
[并查集] 畅通工程
[并查集] 通信系统
原文地址:https://www.cnblogs.com/SQL/p/926666.html
最新文章
Linux入门——文件系统目录结构
ubuntu16。04LST配置nfs实现服务器和客户端共享文件
#include<bits/stdc++.h>
笔试编程---快手实习题目
分治法之快速排序
poj 3321 Apple Tree
poj 1990 MooFest
poj 2115 Matrix
spoj 1029 Matrix Summation
poj 2029 Get Many Persimmon Trees
热门文章
poj 2481 Cows
codeforces 985E Pencils and Boxes
codeforces 985C Liebig's Barrels
codeforces 985B Switches and Lamps
codeforces 985A Chess Placing
poj 1195 Mobile phones
poj 2352 Stars
poj 2299 Ultra-QuickSort
codeforces 982D Shark
sparse-table模板
Copyright © 2011-2022 走看看